diff --git a/Gemfile.lock b/Gemfile.lock index 92dcee9542..b1f9991adc 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -71,7 +71,7 @@ PATH i18n (>= 0.7, < 2) minitest (~> 5.1) tzinfo (~> 1.1) - zeitwerk (~> 1.4, >= 1.4.3) + zeitwerk (~> 2.0) rails (6.0.0.beta3) actioncable (= 6.0.0.beta3) actionmailbox (= 6.0.0.beta3) @@ -526,7 +526,7 @@ GEM websocket-extensions (0.1.3) xpath (3.2.0) nokogiri (~> 1.8) - zeitwerk (1.4.3) + zeitwerk (2.0.0) PLATFORMS java diff --git a/activesupport/CHANGELOG.md b/activesupport/CHANGELOG.md index db9c7e96f3..cf72673dbb 100644 --- a/activesupport/CHANGELOG.md +++ b/activesupport/CHANGELOG.md @@ -1,3 +1,9 @@ +* Depends on Zeitwerk 2, which stores less metadata if reloading is disabled + and hence uses less memory when `config.cache_classes` is `true`, a standard + setup in production. + + *Xavier Noria* + * In `:zeitwerk` mode, eager load directories in engines and applications only if present in their respective `config.eager_load_paths`. diff --git a/activesupport/activesupport.gemspec b/activesupport/activesupport.gemspec index bf0fe0f76d..e719fc6176 100644 --- a/activesupport/activesupport.gemspec +++ b/activesupport/activesupport.gemspec @@ -34,5 +34,5 @@ Gem::Specification.new do |s| s.add_dependency "tzinfo", "~> 1.1" s.add_dependency "minitest", "~> 5.1" s.add_dependency "concurrent-ruby", "~> 1.0", ">= 1.0.2" - s.add_dependency "zeitwerk", "~> 1.4", ">= 1.4.3" + s.add_dependency "zeitwerk", "~> 2.0" end diff --git a/activesupport/lib/active_support/dependencies/zeitwerk_integration.rb b/activesupport/lib/active_support/dependencies/zeitwerk_integration.rb index a43d03cf09..faf9edef27 100644 --- a/activesupport/lib/active_support/dependencies/zeitwerk_integration.rb +++ b/activesupport/lib/active_support/dependencies/zeitwerk_integration.rb @@ -51,15 +51,15 @@ module ActiveSupport end class << self - def take_over - setup_autoloaders + def take_over(enable_reloading:) + setup_autoloaders(enable_reloading) freeze_paths decorate_dependencies end private - def setup_autoloaders + def setup_autoloaders(enable_reloading) Dependencies.autoload_paths.each do |autoload_path| # Zeitwerk only accepts existing directories in `push_dir` to # prevent misconfigurations. @@ -72,6 +72,7 @@ module ActiveSupport autoloader.do_not_eager_load(autoload_path) unless eager_load?(autoload_path) end + Rails.autoloaders.main.enable_reloading if enable_reloading Rails.autoloaders.each(&:setup) end diff --git a/railties/lib/rails/application/finisher.rb b/railties/lib/rails/application/finisher.rb index 8d2c13d2a8..1cf44a480c 100644 --- a/railties/lib/rails/application/finisher.rb +++ b/railties/lib/rails/application/finisher.rb @@ -24,7 +24,7 @@ module Rails initializer :let_zeitwerk_take_over do if config.autoloader == :zeitwerk require "active_support/dependencies/zeitwerk_integration" - ActiveSupport::Dependencies::ZeitwerkIntegration.take_over + ActiveSupport::Dependencies::ZeitwerkIntegration.take_over(enable_reloading: !config.cache_classes) end end diff --git a/railties/test/application/zeitwerk_integration_test.rb b/railties/test/application/zeitwerk_integration_test.rb index dc6db429a9..a9da060347 100644 --- a/railties/test/application/zeitwerk_integration_test.rb +++ b/railties/test/application/zeitwerk_integration_test.rb @@ -131,6 +131,20 @@ class ZeitwerkIntegrationTest < ActiveSupport::TestCase assert $zeitwerk_integration_test_post end + test "reloading is enabled if config.cache_classes is false" do + boot + + assert Rails.autoloaders.main.reloading_enabled? + assert_not Rails.autoloaders.once.reloading_enabled? + end + + test "reloading is disabled if config.cache_classes is true" do + boot("production") + + assert_not Rails.autoloaders.main.reloading_enabled? + assert_not Rails.autoloaders.once.reloading_enabled? + end + test "eager loading loads code in engines" do $test_blog_engine_eager_loaded = false