aboutsummaryrefslogtreecommitdiffstats
path: root/railties/lib/rails
diff options
context:
space:
mode:
authorXavier Noria <fxn@hashref.com>2019-04-14 11:37:50 +0200
committerXavier Noria <fxn@hashref.com>2019-04-14 12:09:09 +0200
commit3e66ba91d511158e22f90ff96b594d61f40eda01 (patch)
tree97257467b80f6d7d087d198c5edac8f9651b3875 /railties/lib/rails
parentf8944ed22dc9453dea5814676fa49ce119a58726 (diff)
downloadrails-3e66ba91d511158e22f90ff96b594d61f40eda01.tar.gz
rails-3e66ba91d511158e22f90ff96b594d61f40eda01.tar.bz2
rails-3e66ba91d511158e22f90ff96b594d61f40eda01.zip
deprecates autoloading constants during initialization [closes #35745]
See rationale in the warning message included in the patch.
Diffstat (limited to 'railties/lib/rails')
-rw-r--r--railties/lib/rails/application/finisher.rb37
1 files changed, 37 insertions, 0 deletions
diff --git a/railties/lib/rails/application/finisher.rb b/railties/lib/rails/application/finisher.rb
index 1cf44a480c..109c560c80 100644
--- a/railties/lib/rails/application/finisher.rb
+++ b/railties/lib/rails/application/finisher.rb
@@ -1,5 +1,8 @@
# frozen_string_literal: true
+require "active_support/core_ext/string/inflections"
+require "active_support/core_ext/array/conversions"
+
module Rails
class Application
module Finisher
@@ -21,6 +24,40 @@ module Rails
end
end
+ # This will become an error if/when we remove classic mode. The plan is
+ # autoloaders won't be configured up to this point in the finisher, so
+ # constants just won't be found, raising regular NameError exceptions.
+ initializer :warn_if_autoloaded, before: :let_zeitwerk_take_over do
+ next if config.cache_classes
+ next if ActiveSupport::Dependencies.autoloaded_constants.empty?
+
+ autoloaded = ActiveSupport::Dependencies.autoloaded_constants
+ constants = "constant".pluralize(autoloaded.size)
+ enum = autoloaded.to_sentence
+ have = autoloaded.size == 1 ? "has" : "have"
+ these = autoloaded.size == 1 ? "This" : "These"
+ example = autoloaded.first
+ example_klass = example.constantize.class
+
+ ActiveSupport::DescendantsTracker.clear
+ ActiveSupport::Dependencies.clear
+
+ ActiveSupport::Deprecation.warn(<<~WARNING)
+ Initialization autoloaded the #{constants} #{enum}.
+
+ Being able to do this is deprecated. Autoloading during initialization is going
+ to be an error condition in future versions of Rails.
+
+ Reloading does not reboot the application, and therefore code executed during
+ 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.
+
+ Please, check the "Autoloading and Reloading Constants" guide for solutions.
+ WARNING
+ end
+
initializer :let_zeitwerk_take_over do
if config.autoloader == :zeitwerk
require "active_support/dependencies/zeitwerk_integration"