Skip to content

Commit

Permalink
new config to opt-out from adding app directories to $LOAD_PATH
Browse files Browse the repository at this point in the history
  • Loading branch information
fxn committed Apr 28, 2019
1 parent f9330ab commit b6e17b6
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 6 deletions.
2 changes: 2 additions & 0 deletions guides/source/configuring.md
Expand Up @@ -64,6 +64,8 @@ These configuration methods are to be called on a `Rails::Railtie` object, such

* `config.autoload_paths` accepts an array of paths from which Rails will autoload constants. Default is all directories under `app`. It is no longer recommended to adjust this. See [Autoloading and Reloading Constants](autoloading_and_reloading_constants.html#autoload-paths-and-eager-load-paths)

* `config.add_autoload_paths_to_load_path` says whether autoload paths have to be added to `$LOAD_PATH`. This flag is `true` by default, but it is recommended to be set to `false` in `:zeitwerk` mode early, in `config/application.rb`. Zeitwerk uses absolute paths internally, and applications running in `:zeitwerk` mode do not need `require_relative`, so models, controllers, jobs, etc. do not need to be in `$LOAD_PATH`. Setting this to `false` saves Ruby from checking these directories when resolving `require` calls with relative paths, and saves Bootsnap work and RAM, since it does not need to build an index for them.

* `config.cache_classes` controls whether or not application classes and modules should be reloaded on each request. Defaults to `false` in development mode, and `true` in test and production modes.

* `config.beginning_of_week` sets the default beginning of week for the
Expand Down
3 changes: 2 additions & 1 deletion railties/lib/rails/application/configuration.rb
Expand Up @@ -19,7 +19,7 @@ class Configuration < ::Rails::Engine::Configuration
:beginning_of_week, :filter_redirect, :x, :enable_dependency_loading,
:read_encrypted_secrets, :log_level, :content_security_policy_report_only,
:content_security_policy_nonce_generator, :require_master_key, :credentials,
:disable_sandbox
:disable_sandbox, :add_autoload_paths_to_load_path

attr_reader :encoding, :api_only, :loaded_config_version, :autoloader

Expand Down Expand Up @@ -67,6 +67,7 @@ def initialize(*)
@credentials.key_path = default_credentials_key_path
@autoloader = :classic
@disable_sandbox = false
@add_autoload_paths_to_load_path = true
end

def load_defaults(target_version)
Expand Down
13 changes: 8 additions & 5 deletions railties/lib/rails/engine.rb
Expand Up @@ -559,9 +559,8 @@ def load_seed
end
end

# Add configured load paths to Ruby's load path, and remove duplicate entries.
initializer :set_load_path, before: :bootstrap_hook do
_all_load_paths.reverse_each do |path|
initializer :set_load_path, before: :bootstrap_hook do |app|
_all_load_paths(app.config.add_autoload_paths_to_load_path).reverse_each do |path|
$LOAD_PATH.unshift(path) if File.directory?(path)
end
$LOAD_PATH.uniq!
Expand Down Expand Up @@ -709,8 +708,12 @@ def _all_autoload_paths
@_all_autoload_paths ||= (config.autoload_paths + config.eager_load_paths + config.autoload_once_paths).uniq
end

def _all_load_paths
@_all_load_paths ||= (config.paths.load_paths + _all_autoload_paths).uniq
def _all_load_paths(add_autoload_paths_to_load_path)
@_all_load_paths ||= begin
load_paths = config.paths.load_paths
load_paths += _all_autoload_paths if add_autoload_paths_to_load_path
load_paths.uniq
end
end

def build_request(env)
Expand Down
28 changes: 28 additions & 0 deletions railties/test/application/configuration_test.rb
Expand Up @@ -4,6 +4,7 @@
require "rack/test"
require "env_helpers"
require "set"
require "active_support/core_ext/string/starts_ends_with"

class ::MyMailInterceptor
def self.delivering_email(email); email; end
Expand Down Expand Up @@ -1704,6 +1705,33 @@ def index
end
end

test "autoload paths are added to $LOAD_PATH by default" do
app "development"

# Action Mailer modifies AS::Dependencies.autoload_paths in-place.
autoload_paths = ActiveSupport::Dependencies.autoload_paths
autoload_paths_from_app_and_engines = autoload_paths.reject do |path|
path.ends_with?("mailers/previews")
end
assert_equal true, Rails.configuration.add_autoload_paths_to_load_path
assert_empty autoload_paths_from_app_and_engines - $LOAD_PATH

# Precondition, ensure we are testing something next.
assert_not_empty Rails.configuration.paths.load_paths
assert_empty Rails.configuration.paths.load_paths - $LOAD_PATH
end

test "autoload paths are not added to $LOAD_PATH if opted-out" do
add_to_config "config.add_autoload_paths_to_load_path = false"
app "development"

assert_empty ActiveSupport::Dependencies.autoload_paths & $LOAD_PATH

# Precondition, ensure we are testing something next.
assert_not_empty Rails.configuration.paths.load_paths
assert_empty Rails.configuration.paths.load_paths - $LOAD_PATH
end

test "autoloading during initialization gets deprecation message and clearing if config.cache_classes is false" do
app_file "lib/c.rb", <<~EOS
class C
Expand Down

4 comments on commit b6e17b6

@prathamesh-sonpatki
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@fxn Does this need a CHANGELOG?

@fxn
Copy link
Member Author

@fxn fxn commented on b6e17b6 Apr 28, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for noticing.

It does not because I squeezed that one to ship with Rails 6 (668673f).

@prathamesh-sonpatki
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome 😸

@byroot
Copy link
Member

@byroot byroot commented on b6e17b6 Apr 28, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

❤️

Please sign in to comment.