diff --git a/guides/source/configuring.md b/guides/source/configuring.md index 02f7e8ebd2..1a148db2c5 100644 --- a/guides/source/configuring.md +++ b/guides/source/configuring.md @@ -137,6 +137,8 @@ defaults to `:debug` for all environments. The available log levels are: `:debug * `config.middleware` allows you to configure the application's middleware. This is covered in depth in the [Configuring Middleware](#configuring-middleware) section below. +* `config.rake_eager_load` when `true`, eager load the application when running Rake tasks. Defaults to `false`. + * `config.reload_classes_only_on_change` enables or disables reloading of classes only when tracked files change. By default tracks everything on autoload paths and is set to `true`. If `config.cache_classes` is `true`, this option is ignored. * `config.credentials.content_path` configures lookup path for encrypted credentials. diff --git a/railties/CHANGELOG.md b/railties/CHANGELOG.md index 0bd2037ac9..61b7b98c83 100644 --- a/railties/CHANGELOG.md +++ b/railties/CHANGELOG.md @@ -1,3 +1,11 @@ +* Allow configuration of eager_load behaviour for rake environment: + + `config.rake_eager_load` + + Defaults to `false` as per previous behaviour. + + *Thierry Joyal* + * Ensure Rails migration generator respects system-wide primary key config When rails is configured to use a specific primary key type: diff --git a/railties/lib/rails/application.rb b/railties/lib/rails/application.rb index 29b13f3e7a..b831fc64b9 100644 --- a/railties/lib/rails/application.rb +++ b/railties/lib/rails/application.rb @@ -505,7 +505,7 @@ module Rails super require "rails/tasks" task :environment do - ActiveSupport.on_load(:before_initialize) { config.eager_load = false } + ActiveSupport.on_load(:before_initialize) { config.eager_load = config.rake_eager_load } require_environment! end diff --git a/railties/lib/rails/application/configuration.rb b/railties/lib/rails/application/configuration.rb index 635b14d037..f22b47124a 100644 --- a/railties/lib/rails/application/configuration.rb +++ b/railties/lib/rails/application/configuration.rb @@ -19,7 +19,8 @@ module Rails :beginning_of_week, :filter_redirect, :x, :enable_dependency_loading, :read_encrypted_secrets, :log_level, :content_security_policy_report_only, :content_security_policy_nonce_generator, :content_security_policy_nonce_directives, - :require_master_key, :credentials, :disable_sandbox, :add_autoload_paths_to_load_path + :require_master_key, :credentials, :disable_sandbox, :add_autoload_paths_to_load_path, + :rake_eager_load attr_reader :encoding, :api_only, :loaded_config_version, :autoloader @@ -70,6 +71,7 @@ module Rails @disable_sandbox = false @add_autoload_paths_to_load_path = true @feature_policy = nil + @rake_eager_load = false end # Loads default configurations. See {the result of the method for each version}[https://guides.rubyonrails.org/configuring.html#results-of-config-load-defaults]. diff --git a/railties/test/application/configuration_test.rb b/railties/test/application/configuration_test.rb index 827a0fdca1..a8084e4acc 100644 --- a/railties/test/application/configuration_test.rb +++ b/railties/test/application/configuration_test.rb @@ -2487,6 +2487,20 @@ module ApplicationTests assert Rails.configuration.disable_sandbox end + test "rake_eager_load is false by default" do + app "development" + assert_equal false, Rails.application.config.rake_eager_load + end + + test "rake_eager_load is set correctly" do + add_to_config <<-RUBY + config.root = "#{app_path}" + config.rake_eager_load = true + RUBY + + assert_equal true, Rails.application.config.rake_eager_load + end + private def force_lazy_load_hooks yield # Tasty clarifying sugar, homie! We only need to reference a constant to load it. diff --git a/railties/test/application/rake_test.rb b/railties/test/application/rake_test.rb index e8456e8c19..c7255f5d4c 100644 --- a/railties/test/application/rake_test.rb +++ b/railties/test/application/rake_test.rb @@ -96,7 +96,7 @@ module ApplicationTests assert_match "Hello world", output end - def test_should_not_eager_load_model_for_rake + def test_should_not_eager_load_model_for_rake_when_rake_eager_load_is_false add_to_config <<-RUBY rake_tasks do task do_nothing: :environment do @@ -117,6 +117,29 @@ module ApplicationTests assert_match "There is nothing", output end + def test_should_eager_load_model_for_rake_when_rake_eager_load_is_true + add_to_config <<-RUBY + rake_tasks do + task do_something: :environment do + puts "Answer: " + Hello::TEST.to_s + end + end + RUBY + + add_to_env_config "production", <<-RUBY + config.rake_eager_load = true + RUBY + + app_file "app/models/hello.rb", <<-RUBY + class Hello + TEST = 42 + end + RUBY + + output = Dir.chdir(app_path) { `bin/rails do_something RAILS_ENV=production` } + assert_equal "Answer: 42\n", output + end + def test_code_statistics_sanity assert_match "Code LOC: 32 Test LOC: 0 Code to Test Ratio: 1:0.0", rails("stats")