diff --git a/railties/lib/rails/application.rb b/railties/lib/rails/application.rb index ed7bca4e3c..93524ae147 100644 --- a/railties/lib/rails/application.rb +++ b/railties/lib/rails/application.rb @@ -497,6 +497,15 @@ module Rails ordered_railties.flatten - [self] end + # Eager loads the application code. + def eager_load! + if Rails.autoloaders.zeitwerk_enabled? + Rails.autoloaders.each(&:eager_load) + else + super + end + end + protected alias :build_middleware_stack :app diff --git a/railties/lib/rails/engine.rb b/railties/lib/rails/engine.rb index d7dc9869b0..93940e5a08 100644 --- a/railties/lib/rails/engine.rb +++ b/railties/lib/rails/engine.rb @@ -471,7 +471,8 @@ module Rails end def eager_load! - # Already done by Zeitwerk::Loader.eager_load_all in the finisher. + # Already done by Zeitwerk::Loader.eager_load_all. We need this guard to + # easily provide a compatible API for both zeitwerk and classic modes. return if Rails.autoloaders.zeitwerk_enabled? config.eager_load_paths.each do |load_path| diff --git a/railties/test/application/zeitwerk_integration_test.rb b/railties/test/application/zeitwerk_integration_test.rb index ab8dbe64d3..af230f73c3 100644 --- a/railties/test/application/zeitwerk_integration_test.rb +++ b/railties/test/application/zeitwerk_integration_test.rb @@ -192,6 +192,26 @@ class ZeitwerkIntegrationTest < ActiveSupport::TestCase assert $zeitwerk_integration_test_post end + test "eager loading loads the application code if invoked manually too (regression test)" do + $zeitwerk_integration_test_user = false + $zeitwerk_integration_test_post = false + + app_file "app/models/user.rb", "class User; end; $zeitwerk_integration_test_user = true" + app_file "app/models/post.rb", "class Post; end; $zeitwerk_integration_test_post = true" + + boot + + # Preconditions. + assert !$zeitwerk_integration_test_user + assert !$zeitwerk_integration_test_post + + Rails.application.eager_load! + + # Postconditions. + assert $zeitwerk_integration_test_user + assert $zeitwerk_integration_test_post + end + test "reloading is enabled if config.cache_classes is false" do boot