Skip to content

Commit

Permalink
Drops support for classic mode
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
fxn committed Mar 8, 2021
1 parent 7762770 commit 0d523d8
Show file tree
Hide file tree
Showing 8 changed files with 12 additions and 94 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

require "set"
require "active_support/core_ext/string/inflections"
require "zeitwerk"

module ActiveSupport
module Dependencies
Expand Down
22 changes: 0 additions & 22 deletions guides/source/autoloading_and_reloading_constants.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
7 changes: 0 additions & 7 deletions guides/source/configuring.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
19 changes: 1 addition & 18 deletions railties/lib/rails/application/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class Configuration < ::Rails::Engine::Configuration
: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
Expand Down Expand Up @@ -69,7 +69,6 @@ def initialize(*)
@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
Expand Down Expand Up @@ -128,8 +127,6 @@ def load_defaults(target_version)
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
Expand All @@ -155,8 +152,6 @@ def load_defaults(target_version)
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
Expand Down Expand Up @@ -365,18 +360,6 @@ def permissions_policy(&block)
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
Expand Down
22 changes: 7 additions & 15 deletions railties/lib/rails/application/finisher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

require "active_support/core_ext/string/inflections"
require "active_support/core_ext/array/conversions"
require "zeitwerk"

module Rails
class Application
Expand Down Expand Up @@ -39,14 +40,10 @@ module Finisher
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}.
Expand Down Expand Up @@ -76,10 +73,8 @@ module Finisher
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
Expand Down Expand Up @@ -113,10 +108,7 @@ module Finisher
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
Expand Down
3 changes: 2 additions & 1 deletion railties/lib/rails/autoloaders.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# frozen_string_literal: true

require "active_support/dependencies/zeitwerk_integration"
require "zeitwerk"

module Rails
module Autoloaders # :nodoc:
Expand Down Expand Up @@ -41,7 +42,7 @@ def log!
end

def zeitwerk_enabled?
Rails.configuration.autoloader == :zeitwerk
true
end
end
end
Expand Down
19 changes: 0 additions & 19 deletions railties/test/application/configuration_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1219,7 +1219,6 @@ def index
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
Expand All @@ -1228,24 +1227,6 @@ def index
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
Expand Down
13 changes: 1 addition & 12 deletions railties/test/application/zeitwerk_integration_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def decorated?
deps.singleton_class < deps::ZeitwerkIntegration::Decorations
end

test "ActiveSupport::Dependencies is decorated by default" do
test "ActiveSupport::Dependencies is decorated" do
boot

assert decorated?
Expand All @@ -36,17 +36,6 @@ def decorated?
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|
Expand Down

0 comments on commit 0d523d8

Please sign in to comment.