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
If ~/.rubocop.yml contains a Rails (or Performance) cop, loading excludes causes "Rails
cops have been extracted" error
#12147
base: master
Are you sure you want to change the base?
Conversation
b44e7b7
to
7a5d21f
Compare
7a5d21f
to
215d2c9
Compare
I can squash when we agree this solution is acceptable. |
dd71242
to
4761cdc
Compare
lib/rubocop/config_loader.rb
Outdated
return if exclusion_relative_path == PathUtil.relative_path(config_file) | ||
return if exclusion_relative_path == PathUtil.relative_path(File.join(Dir.home, DOTFILE)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you refactor to the below?
return if exclusion_relative_path == PathUtil.relative_path(config_file) | |
return if exclusion_relative_path == PathUtil.relative_path(File.join(Dir.home, DOTFILE)) | |
return if exclusion_relative_path == PathUtil.relative_path(config_file) || | |
exclusion_relative_path == PathUtil.relative_path(File.join(Dir.home, DOTFILE)) |
I leave one small comment, but overall it's LGTM. Can you squash your commits into one? |
4761cdc
to
2fc59ba
Compare
Squashed. Thank you! |
Hey, I just realize this is not the right fix. Please hold off on merging. We need to instead return the file before the $HOME/.rubocop.yml in the directory hierarchy. As is this code will skip the actual project root yml if there is a nested config deeper in the project. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll get these fixes tomorrow.
lib/rubocop/config_loader.rb
Outdated
@@ -139,7 +139,10 @@ def add_excludes_from_files(config, config_file) | |||
exclusion_file = find_last_file_upwards(DOTFILE, config_file, ConfigFinder.project_root) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actual fix needs to modify find_last_file_upwards to not return the home .yml file, but whatever config it finds before that home dotfile.
Worth noting, @deivid-rodriguez linked me to a similar fix #8176 from a few years ago trying to address a similar problem. Hopefully we can get this specified clearly enough this time. |
2fc59ba
to
62cd3df
Compare
I chose to attempt forcing all specs in the isolated environment to stay within the directory tree that is allowed. Many of the specs were placing files outside of the tmpdir, then testing that those files weren't loaded (relying on the root_level behavior which is only enabled in tests). This meant that some of the tests only passed because the test environment kept the file from being found. In the real world it would pick up the file (which is the point of this PR). However, I'm having trouble fixing a few of the specs. They should all be caused by this situation of previously loading or relying on files that are now off limits (or assuming they wouldn't be loaded). They might also be outside of the search path, or some other magic is happening that I don't understand. Maybe someone with more experience in RuboCop code could help? Another thing that might be causing it is just some implied directory locations based on the old dir structure.. I'll try adjusting that and see if it fixes it. (edit: nope, removing the root/ from the tmpdir causes the same failures and more). |
It is common for projects to be in a subdirectory of $HOME, causing a problem in the ConfigLoader where add_excludes_from_files searches for the highest file matching .rubocop.yml and finds ~/.rubocop.yml. This violates the documentation stating that ~/.rubocop.yml will not be loaded when a project specific file is found first. Adds tests for config loading behavior and extends behavior for finding ancestor configs to make it possible to grab the highest config before the home dir.
62cd3df
to
7498a15
Compare
When running rubocop in a project that does not load Rails (in this case I'm running rubocop on rubygems), if you have Rails/ cops in your
~/.rubycop.yml
file, then they will be loaded, validated, and the obsolete checker will raise.However, the docs seem to explicitly say that this file should not be loaded since the project root contains a .rubocop.yml.
This is happening because the tests do not test the common case where projects are in a subdirectory of the user's home dir.
When the
.rubocop.yml
file in $HOME are in an ancestor folder of the current project, theConfigLoader
looks for the "last file upwards" matching the file pattern ".rubocop.yml" and finds~/.rubocop.yml
.This results in fully loading the
~/.rubocop.yml
and loading the excludes contained therein. However, since the validation and obsolete checks are run, errors are generated for not requiringrubocop-rails
.My offered fix is to specifically exclude this file, but I can reduce this PR back to just the failing spec if another solution is desired.
Before submitting the PR make sure the following are checked:
[Fix #issue-number]
(if the related issue exists).master
(if not - rebase it).bundle exec rake default
. It executes all tests and runs RuboCop on its own code.{change_type}_{change_description}.md
if the new code introduces user-observable changes. See changelog entry format for details.