提交 c0d91a4f 编写于 作者: X Xavier Noria

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.
上级 11781d06
......@@ -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
......
......@@ -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|
......
......@@ -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
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册