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

optimizes eager loading in :zeitwerk mode

During initialization, the eager load paths of engines are unshifted
into AS::Dependencies.autoload_paths. After that, the collection is
frozen. (See the initializers in railties/lib/rails/engine.rb.)

Hence, there is no eager load path that is not an autoload path too, and
so the array difference in the deleted code is always an empty array.

Just do nothing.
This commit is contained in:
Xavier Noria 2019-03-30 12:20:00 +01:00
parent 278f0dfeac
commit a3e061def1
2 changed files with 23 additions and 22 deletions

View file

@ -469,13 +469,16 @@ module Rails
self
end
# Eager load the application by loading all ruby
# files inside eager_load paths.
def eager_load!
if Rails.autoloaders.zeitwerk_enabled?
eager_load_with_zeitwerk!
else
eager_load_with_dependencies!
# Already done by Zeitwerk::Loader.eager_load_all in the finisher.
return if Rails.autoloaders.zeitwerk_enabled?
config.eager_load_paths.each do |load_path|
# Starts after load_path plus a slash, ends before ".rb".
relname_range = (load_path.to_s.length + 1)...-3
Dir.glob("#{load_path}/**/*.rb").sort.each do |file|
require_dependency file[relname_range]
end
end
end
@ -654,22 +657,6 @@ module Rails
private
def eager_load_with_zeitwerk!
(config.eager_load_paths - Zeitwerk::Loader.all_dirs).each do |path|
Dir.glob("#{path}/**/*.rb").sort.each { |file| require file }
end
end
def eager_load_with_dependencies!
config.eager_load_paths.each do |load_path|
# Starts after load_path plus a slash, ends before ".rb".
relname_range = (load_path.to_s.length + 1)...-3
Dir.glob("#{load_path}/**/*.rb").sort.each do |file|
require_dependency file[relname_range]
end
end
end
def load_config_initializer(initializer) # :doc:
ActiveSupport::Notifications.instrument("load_config_initializer.railties", initializer: initializer) do
load(initializer)

View file

@ -124,12 +124,26 @@ class ZeitwerkIntegrationTest < ActiveSupport::TestCase
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("production")
assert $zeitwerk_integration_test_user
assert $zeitwerk_integration_test_post
end
test "eager loading loads code in engines" do
$test_blog_engine_eager_loaded = false
engine("blog") do |bukkit|
bukkit.write("lib/blog.rb", "class BlogEngine < Rails::Engine; end")
bukkit.write("app/models/post.rb", "Post = $test_blog_engine_eager_loaded = true")
end
boot("production")
assert $test_blog_engine_eager_loaded
end
test "eager loading loads anything managed by Zeitwerk" do
$zeitwerk_integration_test_user = false
app_file "app/models/user.rb", "class User; end; $zeitwerk_integration_test_user = true"