diff --git a/railties/lib/rails/application.rb b/railties/lib/rails/application.rb index ed7bca4e3ca8f78e4a3a2e973375c44da938fe76..93524ae1479232fee88636a81e8a40962bc72f82 100644 --- a/railties/lib/rails/application.rb +++ b/railties/lib/rails/application.rb @@ -497,6 +497,15 @@ def migration_railties # :nodoc: 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 d7dc9869b0c9d44399095f59e61c5454b8f47394..93940e5a088c4be6cbc3d3aa62ed339de2d58fc2 100644 --- a/railties/lib/rails/engine.rb +++ b/railties/lib/rails/engine.rb @@ -471,7 +471,8 @@ def load_generators(app = self) 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 ab8dbe64d34dc2991e3dc4db07fe39157d53a00f..af230f73c386cc7950735c7218735df9482a1cf6 100644 --- a/railties/test/application/zeitwerk_integration_test.rb +++ b/railties/test/application/zeitwerk_integration_test.rb @@ -192,6 +192,26 @@ def user.to_path; "user"; end 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