Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure only directories exist in Rails default load paths #50723

Merged
merged 1 commit into from Jan 16, 2024

Conversation

aeroastro
Copy link
Contributor

Motivation / Background

As commented at #37011 (comment),
files such as app/README.md should not be in the autoload paths.

Detail

This PR removes files from the default load paths set up by the Rails framework.

Additional information

Although File.stub(:exist?, true) in existing test has been changed to File.stub(:directory?, true),
it is obvious that this does not break the objectives of the test.

Checklist

Before submitting the PR make sure the following are checked:

  • This Pull Request is related to one change. Changes that are unrelated should be opened in separate PRs.
  • Commit message has a detailed description of what changed and why. If this PR fixes a related issue include it in the commit message. Ex: [Fix #issue-number]
  • Tests are added or updated if you fix a bug or add a feature.
  • CHANGELOG files are updated for the changed libraries if there is a behavior change or additional feature. Minor bug fixes and documentation changes should not be included.

@rails-bot rails-bot bot added the railties label Jan 12, 2024
@aeroastro aeroastro force-pushed the feature/no-file-in-load-path branch 2 times, most recently from 7c1ad7d to 64fe2ff Compare January 15, 2024 03:39
Previously, some files put under `app` directory would contaminate the load paths.
This commit removes files from the default load paths set up by the Rails framework.

Now, only directories are included as default in the following paths:

* autoload_paths
* autoload_once_paths
* eager_load_paths
* load_paths
@aeroastro
Copy link
Contributor Author

All the tests have successfully passed 🐱

Currently, since these paths (autoload_paths, autoload_once_paths, eager_load_paths, and load_paths) are used with File.directory? or Dir.exist?, the changes introduced in this PR does not break the behavior of public interfaces of Rails framework.

ActiveSupport::Dependencies.autoload_once_paths.freeze
ActiveSupport::Dependencies.autoload_once_paths.uniq.each do |path|
# Zeitwerk only accepts existing directories in `push_dir`.
next unless File.directory?(path)

ActiveSupport::Dependencies.autoload_paths.freeze
ActiveSupport::Dependencies.autoload_paths.uniq.each do |path|
# Zeitwerk only accepts existing directories in `push_dir`.
next unless File.directory?(path)

def self.check
Zeitwerk::Loader.eager_load_all
autoloaded = ActiveSupport::Dependencies.autoload_paths + ActiveSupport::Dependencies.autoload_once_paths
eager_loaded = ActiveSupport::Dependencies._eager_load_paths.to_a
unchecked = autoloaded - eager_loaded
unchecked.select! { |dir| Dir.exist?(dir) && !Dir.empty?(dir) }

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!
end

@rafaelfranca rafaelfranca requested a review from fxn January 15, 2024 21:23
@fxn
Copy link
Member

fxn commented Jan 16, 2024

Looks good. I'll edit a bit the CHANGELOG later (first sentence lacks a final period, in a Rails CHANGELOG you do not need to say you are talking about the Rails framework, ...).

Thanks!

@fxn fxn merged commit cb035bd into rails:main Jan 16, 2024
4 checks passed
@aeroastro aeroastro deleted the feature/no-file-in-load-path branch January 17, 2024 05:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants