From a3e061def14b50b86d9366d45565a4ad2efb0b08 Mon Sep 17 00:00:00 2001 From: Xavier Noria Date: Sat, 30 Mar 2019 12:20:00 +0100 Subject: [PATCH] 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. --- railties/lib/rails/engine.rb | 31 ++++++------------- .../application/zeitwerk_integration_test.rb | 14 +++++++++ 2 files changed, 23 insertions(+), 22 deletions(-) diff --git a/railties/lib/rails/engine.rb b/railties/lib/rails/engine.rb index 2b5afd51b8..9b3698aa5e 100644 --- a/railties/lib/rails/engine.rb +++ b/railties/lib/rails/engine.rb @@ -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) diff --git a/railties/test/application/zeitwerk_integration_test.rb b/railties/test/application/zeitwerk_integration_test.rb index e4a17e5a31..a4320bbdc3 100644 --- a/railties/test/application/zeitwerk_integration_test.rb +++ b/railties/test/application/zeitwerk_integration_test.rb @@ -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"