From 0d523d83657ce7066f25d87f6f094e804590e1e8 Mon Sep 17 00:00:00 2001 From: Xavier Noria Date: Mon, 1 Mar 2021 22:28:35 +0100 Subject: [PATCH] Drops support for classic mode This starts a series of patches in which we drop classic mode. The final result no longer has a const_missing callback, there is no hook/unhook, and so on. So, in this patch we remove the ability of configuring classic, but some of the code that remains will be further refactored. --- .../dependencies/zeitwerk_integration.rb | 1 + .../autoloading_and_reloading_constants.md | 22 ------------------- guides/source/configuring.md | 7 ------ .../lib/rails/application/configuration.rb | 19 +--------------- railties/lib/rails/application/finisher.rb | 22 ++++++------------- railties/lib/rails/autoloaders.rb | 3 ++- .../test/application/configuration_test.rb | 19 ---------------- .../application/zeitwerk_integration_test.rb | 13 +---------- 8 files changed, 12 insertions(+), 94 deletions(-) diff --git a/activesupport/lib/active_support/dependencies/zeitwerk_integration.rb b/activesupport/lib/active_support/dependencies/zeitwerk_integration.rb index 2155fba0a9..f7aec0ffb3 100644 --- a/activesupport/lib/active_support/dependencies/zeitwerk_integration.rb +++ b/activesupport/lib/active_support/dependencies/zeitwerk_integration.rb @@ -2,6 +2,7 @@ require "set" require "active_support/core_ext/string/inflections" +require "zeitwerk" module ActiveSupport module Dependencies diff --git a/guides/source/autoloading_and_reloading_constants.md b/guides/source/autoloading_and_reloading_constants.md index bdfd39861d..04e5829749 100644 --- a/guides/source/autoloading_and_reloading_constants.md +++ b/guides/source/autoloading_and_reloading_constants.md @@ -438,25 +438,3 @@ While in common names these operations match, if acronyms or custom inflection r ### More Differences There are some other subtle differences. Please check the [autoloading section](upgrading_ruby_on_rails.html#autoloading) of the _Upgrading Ruby on Rails_] guide for details. - -Classic Mode is Deprecated --------------------------- - -Currently, it is still possible to use `classic` mode. However, `classic` is deprecated and will be eventually removed. - -New applications should use `zeitwerk` mode (which is the default), and applications being upgraded are strongly encouraged to migrate to `zeitwerk` mode. Please check the [_Upgrading Ruby on Rails_](upgrading_ruby_on_rails.html#autoloading) guide for details. - -Opting Out ----------- - -Applications can load Rails 6 defaults and still use the `classic` autoloader this way: - -```ruby -# config/application.rb -config.load_defaults 6.0 -config.autoloader = :classic -``` - -That may be handy if upgrading to Rails 6 in different phases, but `classic` mode is discouraged for new applications. - -`zeitwerk` mode is not available in versions of Rails prior to 6.0. diff --git a/guides/source/configuring.md b/guides/source/configuring.md index 6ffe699532..842dbe57a1 100644 --- a/guides/source/configuring.md +++ b/guides/source/configuring.md @@ -159,13 +159,6 @@ numbers. It also filters out sensitive values of database columns when call `#in * `config.time_zone` sets the default time zone for the application and enables time zone awareness for Active Record. -* `config.autoloader` sets the autoloading mode. This option defaults to `:zeitwerk` when `config.load_defaults` is called with `6.0` or greater. Applications can still use the classic autoloader by setting this value to `:classic` after loading the framework defaults: - - ```ruby - config.load_defaults 6.0 - config.autoloader = :classic - ``` - ### Configuring Assets * `config.assets.enabled` a flag that controls whether the asset diff --git a/railties/lib/rails/application/configuration.rb b/railties/lib/rails/application/configuration.rb index 32e1776bb4..5c97f4ec73 100644 --- a/railties/lib/rails/application/configuration.rb +++ b/railties/lib/rails/application/configuration.rb @@ -23,7 +23,7 @@ module Rails :require_master_key, :credentials, :disable_sandbox, :add_autoload_paths_to_load_path, :rake_eager_load - attr_reader :encoding, :api_only, :loaded_config_version, :autoloader + attr_reader :encoding, :api_only, :loaded_config_version def initialize(*) super @@ -69,7 +69,6 @@ module Rails @credentials = ActiveSupport::OrderedOptions.new @credentials.content_path = default_credentials_content_path @credentials.key_path = default_credentials_key_path - @autoloader = :classic @disable_sandbox = false @add_autoload_paths_to_load_path = true @permissions_policy = nil @@ -128,8 +127,6 @@ module Rails when "6.0" load_defaults "5.2" - self.autoloader = :zeitwerk if RUBY_ENGINE == "ruby" - if respond_to?(:action_view) action_view.default_enforce_utf8 = false end @@ -155,8 +152,6 @@ module Rails when "6.1" load_defaults "6.0" - self.autoloader = :zeitwerk if %w[ruby truffleruby].include?(RUBY_ENGINE) - if respond_to?(:active_record) active_record.has_many_inversing = true active_record.legacy_connection_handling = false @@ -365,18 +360,6 @@ module Rails end end - def autoloader=(autoloader) - case autoloader - when :classic - @autoloader = autoloader - when :zeitwerk - require "zeitwerk" - @autoloader = autoloader - else - raise ArgumentError, "config.autoloader may be :classic or :zeitwerk, got #{autoloader.inspect} instead" - end - end - def default_log_file path = paths["log"].first unless File.exist? File.dirname path diff --git a/railties/lib/rails/application/finisher.rb b/railties/lib/rails/application/finisher.rb index e4efebb615..900eb8c6e0 100644 --- a/railties/lib/rails/application/finisher.rb +++ b/railties/lib/rails/application/finisher.rb @@ -2,6 +2,7 @@ require "active_support/core_ext/string/inflections" require "active_support/core_ext/array/conversions" +require "zeitwerk" module Rails class Application @@ -39,14 +40,10 @@ module Rails example = autoloaded.first example_klass = example.constantize.class - if config.autoloader == :zeitwerk - ActiveSupport::DescendantsTracker.clear - ActiveSupport::Dependencies.clear + ActiveSupport::DescendantsTracker.clear + ActiveSupport::Dependencies.clear - unload_message = "#{these} autoloaded #{constants} #{have} been unloaded." - else - unload_message = "`config.autoloader` is set to `#{config.autoloader}`. #{these} autoloaded #{constants} would have been unloaded if `config.autoloader` had been set to `:zeitwerk`." - end + unload_message = "#{these} autoloaded #{constants} #{have} been unloaded." ActiveSupport::Deprecation.warn(<<~WARNING) Initialization autoloaded the #{constants} #{enum}. @@ -76,10 +73,8 @@ module Rails end initializer :let_zeitwerk_take_over do - if config.autoloader == :zeitwerk - require "active_support/dependencies/zeitwerk_integration" - ActiveSupport::Dependencies::ZeitwerkIntegration.take_over(enable_reloading: !config.cache_classes) - end + require "active_support/dependencies/zeitwerk_integration" + ActiveSupport::Dependencies::ZeitwerkIntegration.take_over(enable_reloading: !config.cache_classes) end # Setup default session store if not already set in config/application.rb @@ -113,10 +108,7 @@ module Rails initializer :eager_load! do if config.eager_load ActiveSupport.run_load_hooks(:before_eager_load, self) - # Checks defined?(Zeitwerk) instead of zeitwerk_enabled? because we - # want to eager load any dependency managed by Zeitwerk regardless of - # the autoloading mode of the application. - Zeitwerk::Loader.eager_load_all if defined?(Zeitwerk) + Zeitwerk::Loader.eager_load_all config.eager_load_namespaces.each(&:eager_load!) end end diff --git a/railties/lib/rails/autoloaders.rb b/railties/lib/rails/autoloaders.rb index ce3ba269fe..773d2e58e0 100644 --- a/railties/lib/rails/autoloaders.rb +++ b/railties/lib/rails/autoloaders.rb @@ -1,6 +1,7 @@ # frozen_string_literal: true require "active_support/dependencies/zeitwerk_integration" +require "zeitwerk" module Rails module Autoloaders # :nodoc: @@ -41,7 +42,7 @@ module Rails end def zeitwerk_enabled? - Rails.configuration.autoloader == :zeitwerk + true end end end diff --git a/railties/test/application/configuration_test.rb b/railties/test/application/configuration_test.rb index 5aa4196180..473c671df3 100644 --- a/railties/test/application/configuration_test.rb +++ b/railties/test/application/configuration_test.rb @@ -1219,7 +1219,6 @@ module ApplicationTests test "autoloaders" do app "development" - config = Rails.application.config assert Rails.autoloaders.zeitwerk_enabled? assert_instance_of Zeitwerk::Loader, Rails.autoloaders.main assert_equal "rails.main", Rails.autoloaders.main.tag @@ -1228,24 +1227,6 @@ module ApplicationTests assert_equal [Rails.autoloaders.main, Rails.autoloaders.once], Rails.autoloaders.to_a assert_equal ActiveSupport::Dependencies::ZeitwerkIntegration::Inflector, Rails.autoloaders.main.inflector assert_equal ActiveSupport::Dependencies::ZeitwerkIntegration::Inflector, Rails.autoloaders.once.inflector - - config.autoloader = :classic - assert_not Rails.autoloaders.zeitwerk_enabled? - assert_nil Rails.autoloaders.main - assert_nil Rails.autoloaders.once - assert_equal 0, Rails.autoloaders.count - - config.autoloader = :zeitwerk - assert Rails.autoloaders.zeitwerk_enabled? - assert_instance_of Zeitwerk::Loader, Rails.autoloaders.main - assert_equal "rails.main", Rails.autoloaders.main.tag - assert_instance_of Zeitwerk::Loader, Rails.autoloaders.once - assert_equal "rails.once", Rails.autoloaders.once.tag - assert_equal [Rails.autoloaders.main, Rails.autoloaders.once], Rails.autoloaders.to_a - assert_equal ActiveSupport::Dependencies::ZeitwerkIntegration::Inflector, Rails.autoloaders.main.inflector - assert_equal ActiveSupport::Dependencies::ZeitwerkIntegration::Inflector, Rails.autoloaders.once.inflector - - assert_raises(ArgumentError) { config.autoloader = :unknown } end test "config.action_view.cache_template_loading with cache_classes default" do diff --git a/railties/test/application/zeitwerk_integration_test.rb b/railties/test/application/zeitwerk_integration_test.rb index 6ceb2be010..6bef0be0da 100644 --- a/railties/test/application/zeitwerk_integration_test.rb +++ b/railties/test/application/zeitwerk_integration_test.rb @@ -26,7 +26,7 @@ class ZeitwerkIntegrationTest < ActiveSupport::TestCase deps.singleton_class < deps::ZeitwerkIntegration::Decorations end - test "ActiveSupport::Dependencies is decorated by default" do + test "ActiveSupport::Dependencies is decorated" do boot assert decorated? @@ -36,17 +36,6 @@ class ZeitwerkIntegrationTest < ActiveSupport::TestCase assert_equal [Rails.autoloaders.main, Rails.autoloaders.once], Rails.autoloaders.to_a end - test "ActiveSupport::Dependencies is not decorated in classic mode" do - add_to_config "config.autoloader = :classic" - boot - - assert_not decorated? - assert_not Rails.autoloaders.zeitwerk_enabled? - assert_nil Rails.autoloaders.main - assert_nil Rails.autoloaders.once - assert_equal 0, Rails.autoloaders.count - end - test "autoloaders inflect with Active Support" do app_file "config/initializers/inflections.rb", <<-RUBY ActiveSupport::Inflector.inflections(:en) do |inflect|