提交 bb3f24af 编写于 作者: C Cliff Pruitt

Do not clear deprecated initializer dependencies if using classic autoloader

`Rails::Application::Finisher` defines a `:let_zeitwerk_take_over` initializer. This initializer is always run but it's statements are wrapped in a guard: `config.autoloader == :zeitwerk`.

`Finisher` also defines a `initializer :warn_if_autoloaded` initializer with a `before: :let_zeitwerk_take_over` option which also always runs. This initializer unloads any constants autoloaded during initialization and displays a deprecation warning. This initializer does not account for `config.autoloader` being set to `:classic`.

The problem is that this initializer changes the behavior of the classic autoloader. Constant autoloading from initializers is deprecated but the deprecation should not break existing applications which currently depend on autoloaded constants in initializers.

This commit prevents the dependencies from being unloaded if the autoloader is not Zeitwerk. It also updates the deprecation warning, if the classic autoloader is enabled, to indicate that the constants would have been unloaded if Zeitwerk had been used.
上级 c5f11438
......@@ -39,8 +39,14 @@ module Finisher
example = autoloaded.first
example_klass = example.constantize.class
ActiveSupport::DescendantsTracker.clear
ActiveSupport::Dependencies.clear
if config.autoloader == :zeitwerk
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
ActiveSupport::Deprecation.warn(<<~WARNING)
Initialization autoloaded the #{constants} #{enum}.
......@@ -52,7 +58,7 @@ module Finisher
initialization does not run again. So, if you reload #{example}, for example,
the expected changes won't be reflected in that stale #{example_klass} object.
#{these} autoloaded #{constants} #{have} been unloaded.
#{unload_message}
Please, check the "Autoloading and Reloading Constants" guide for solutions.
WARNING
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册