1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

restores the ability to manually eager load applications

The main interface to eager loading is config.eager_load. The logic that
implies happens during the boot process.

With the introduction of Zeitwerk, application code is loaded in the
finisher as everything else, but in previous versions of Rails users
could eager load the application code regardless of config.eager_load.

Use cases:

   * Some gems like indexers need to have everything in memory and would
   be a bad user experience to ask users to conditionally set the eager
   load flag.

   * Some tests may need to have everything in memory and would be a bad
   experience to have the flag enabled globally in the test environment.

I personally feel that the contract between this method and the entire
eager loading process is ill-defined. I believe this method is
essentially internal. The purpose of this patch is simply to restore this
functionality emulating what it did before because rethinking the design
of this interface may need time.
This commit is contained in:
Xavier Noria 2020-01-07 15:13:02 +01:00
parent 11781d0632
commit c0d91a4f9d
3 changed files with 31 additions and 1 deletions

View file

@ -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

View file

@ -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|

View file

@ -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