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

Behaviour of home directory .rubocop.yml vs project specific .rubocop.yml #9879

Closed
odinhb opened this issue Jun 16, 2021 · 16 comments
Closed

Comments

@odinhb
Copy link

odinhb commented Jun 16, 2021

Hey. I'm having a problem using a .rubocop.yml in my home directory.

I'm using rbenv, rbenv-gemsets and bundler to isolate the project, and in a project dir we have a project specific .rubocop.yml, specifying a TargetRubyVersion of 2.6, compatible with the older version of rubocop.

In my home directory I have a .rubocop.yml intended for use w/ a newer version of rubocop and ruby than what we're running in the project. It has a TargetRubyVersion of 3.0.

When invoking rubocop -d in the project dir, here is the output.

output when invoking rubocop -d in the project dir
For /home/odin/dev/project/repo: configuration from /home/odin/dev/project/repo/.rubocop.yml
configuration from /home/odin/.rbenv/versions/2.6.6/gemsets/project/gems/rubocop-rspec-1.32.0/config/default.yml
configuration from /home/odin/.rbenv/versions/2.6.6/gemsets/project/gems/rubocop-rspec-1.32.0/config/default.yml
Default configuration from /home/odin/.rbenv/versions/2.6.6/gemsets/project/gems/rubocop-0.70.0/config/default.yml
configuration from /home/odin/.rbenv/versions/2.6.6/gemsets/project/gems/rubocop-performance-1.3.0/config/default.yml
configuration from /home/odin/.rbenv/versions/2.6.6/gemsets/project/gems/rubocop-performance-1.3.0/config/default.yml
Inheriting configuration from /home/odin/dev/work/project/repo/.rubocop_todo.yml
.rubocop.yml: Layout/MultilineMethodCallIndentation:Enabled overrides the same parameter in .rubocop_todo.yml
AllCops/Exclude configuration from /home/odin/.rubocop.yml
Warning: unrecognized cop Gemspec/DateAssignment found in /home/odin/.rubocop.yml
Warning: unrecognized cop Layout/SpaceBeforeBrackets found in /home/odin/.rubocop.yml
Warning: unrecognized cop Lint/AmbiguousAssignment found in /home/odin/.rubocop.yml
Warning: unrecognized cop Lint/DeprecatedConstants found in /home/odin/.rubocop.yml
Warning: unrecognized cop Lint/DuplicateBranch found in /home/odin/.rubocop.yml
Warning: unrecognized cop Lint/DuplicateRegexpCharacterClassElement found in /home/odin/.rubocop.yml
Warning: unrecognized cop Lint/EmptyBlock found in /home/odin/.rubocop.yml
Warning: unrecognized cop Lint/EmptyClass found in /home/odin/.rubocop.yml
Warning: unrecognized cop Lint/EmptyInPattern found in /home/odin/.rubocop.yml
Warning: unrecognized cop Lint/LambdaWithoutLiteralBlock found in /home/odin/.rubocop.yml
Warning: unrecognized cop Lint/NoReturnInBeginEndBlocks found in /home/odin/.rubocop.yml
Warning: unrecognized cop Lint/NumberedParameterAssignment found in /home/odin/.rubocop.yml
Warning: unrecognized cop Lint/OrAssignmentToConstant found in /home/odin/.rubocop.yml
Warning: unrecognized cop Lint/RedundantDirGlobSort found in /home/odin/.rubocop.yml
Warning: unrecognized cop Lint/SymbolConversion found in /home/odin/.rubocop.yml
Warning: unrecognized cop Lint/ToEnumArguments found in /home/odin/.rubocop.yml
Warning: unrecognized cop Lint/TripleQuotes found in /home/odin/.rubocop.yml
Warning: unrecognized cop Lint/UnexpectedBlockArity found in /home/odin/.rubocop.yml
Warning: unrecognized cop Lint/UnmodifiedReduceAccumulator found in /home/odin/.rubocop.yml
Warning: unrecognized cop Style/ArgumentsForwarding found in /home/odin/.rubocop.yml
Warning: unrecognized cop Style/CollectionCompact found in /home/odin/.rubocop.yml
Warning: unrecognized cop Style/DocumentDynamicEvalDefinition found in /home/odin/.rubocop.yml
Warning: unrecognized cop Style/EndlessMethod found in /home/odin/.rubocop.yml
Warning: unrecognized cop Style/HashConversion found in /home/odin/.rubocop.yml
Warning: unrecognized cop Style/HashExcept found in /home/odin/.rubocop.yml
Warning: unrecognized cop Style/IfWithBooleanLiteralBranches found in /home/odin/.rubocop.yml
Warning: unrecognized cop Style/InPatternThen found in /home/odin/.rubocop.yml
Warning: unrecognized cop Style/MultilineInPatternThen found in /home/odin/.rubocop.yml
Warning: unrecognized cop Style/NegatedIfElseCondition found in /home/odin/.rubocop.yml
Warning: unrecognized cop Style/NilLambda found in /home/odin/.rubocop.yml
Warning: unrecognized cop Style/QuotedSymbols found in /home/odin/.rubocop.yml
Warning: unrecognized cop Style/RedundantArgument found in /home/odin/.rubocop.yml
Warning: unrecognized cop Style/StringChars found in /home/odin/.rubocop.yml
Warning: unrecognized cop Style/SwapValues found in /home/odin/.rubocop.yml
Error: Unknown Ruby version 3.0 found in `TargetRubyVersion` parameter (in /home/odin/.rubocop.yml).
Supported versions: 2.3, 2.4, 2.5, 2.6                                                                                                        
Finished in 0.3350943620025646 seconds

Based on the documentation at https://docs.rubocop.org/rubocop/configuration.html I expected rubocop only to load configuration from my home directory if it could not find a .rubocop.yml inside the project directory.

As an example, if RuboCop is invoked from inside /path/to/project/lib/utils, then RuboCop will use the config as specified inside the first of the following files:

  • /path/to/project/lib/utils/.rubocop.yml
  • /path/to/project/lib/.rubocop.yml
  • /path/to/project/.rubocop.yml
  • /.rubocop.yml
  • ~/.rubocop.yml
  • ~/.config/rubocop/config.yml
  • RuboCop’s default configuration

Am I misunderstanding the docs or is this a bug?

Versions in project

0.70.0 (using Parser 2.6.3.0, running on ruby 2.6.6 x86_64-linux)

Versions in home dir (system)

1.16.1 (using Parser 3.0.1.1, rubocop-ast 1.7.0, running on ruby 3.0.1 x86_64-linux)
@jonas054
Copy link
Collaborator

jonas054 commented Jul 2, 2021

It's taking AllCops/Exclude configuration from /home/odin/.rubocop.yml, not becase it's in your home directory but because the directory is an ancestor of /home/odin/dev/project/repo. The reason is that Exclude must be specified on a higher level so that we don't get "the project wants to exclude these files but the directory where the files are wants to override that decision". The typical use case is vendor/ subdirectories that may have .rubocop.yml files in them, but you still want to configure in the project root that vendor/ should be excluded.

So it's not a bug exactly, but an undesirable side effect of how the configuration system doesn't have the concept of project root, only of ancestor directories. I would recommend that you don't have RuboCop configuration in your home directory.

@deivid-rodriguez
Copy link
Contributor

I think this problem was fixed by #8314?

@jonas054
Copy link
Collaborator

jonas054 commented Jul 8, 2021

Yes, that should have fixed it. And sorry I wasn't aware of your improvement of the logic for looking up configuration, @deivid-rodriguez!

But for some reason /home/odin/.rubocop.yml is still read, and I guess the explanation must be that no Gemfile or gems.rb was found when determining the project root.

@deivid-rodriguez
Copy link
Contributor

@jonas054 I think it's because OP is using rubocop 0.70 and my improvements were introduced in rubocop 0.80.

@jonas054
Copy link
Collaborator

@deivid-rodriguez's explanation seems correct. Solution is to upgrade RuboCop. Closing.

@odinhb
Copy link
Author

odinhb commented Jul 10, 2021

This is a useless solution to me, because there are so many projects still using pre-0.80.

I can't go around upgrading every project's rubocop before contributing to it. Even if I did, the moment I pull down a new project (to me) that has an old version of rubocop I just can't run the linter locally.

I think this could use a backport.

Thank you guys for figuring it out though.

@jonas054
Copy link
Collaborator

What about moving ~/.rubocop.yml to some other place and inheriting from it explicitly where yo need it?

@odinhb
Copy link
Author

odinhb commented Jul 10, 2021

My intention is to maintain a personal cross-computer set of rubocop rules which automagically (when opened w/ my editor) applies to my various ruby scripts littered around my computer. These scripts do not warrant the whole ceremony of setting up a project directory w/ a rubocop.yml.

I could place a .rubocop.yml that explicitly inherits from my config in every folder in which I have a ruby script, but I won't remember/bother to do that when dropping into said script for a quick edit or quickly making a new one (which is the nature of these scripts). I also don't want to litter my filesystem with these .rubocop.yml files. (Also what if this .rubocop.yml ends up being in a parent directory to a project w/ pre-0.80 rubocop installed?)

The fact that I'm using editor integration (SublimeLinter for Sublime text), means it's too much of a hassle to use the --config option (because it also has to work in all the various projects that I use the same editor for).

It's not that its impossible, but it is too much effort for too little value.

@jonas054
Copy link
Collaborator

I see. Then you should be able to move ~/.rubocop.yml to ~/.config/rubocop/config.yml. It will be read when there is no RuboCop configuration for the file tree being inspected. It will not, however, be used when RuboCop searches for Exclude configuration on a higher level (the problem you described initially).

@odinhb
Copy link
Author

odinhb commented Jul 10, 2021

The error I'm getting is this:

Error: Unknown Ruby version 3.0 found in `TargetRubyVersion` parameter (in /home/odin/.rubocop.yml).
Supported versions: 2.3, 2.4, 2.5, 2.6 

Rubocop 0.70 doesn't know that Ruby 3.0 exists.

I already tried moving my config into .config/rubocop/config.yml. As far as I remember I get the same error.

@jonas054
Copy link
Collaborator

I've now tried to reproduce your problem on my machine, and it seems that you're right. Moving the common configuration to .config/rubocop/config.yml doesn't help. I thought it would, but the file is still read in search of the higher level AllCops: Exclude. Too bad!

As far as I can see, your problem is quite unique and doesn't really warrant backporting the solution from 0.80 to a new 0.70.2 version. So I'm not re-opening the issue at this point. Let me know if you disagree.

@KSH-code
Copy link

KSH-code commented Nov 8, 2021

I'm using 1.3.1 but the error occurs from Vscode.
스크린샷 2021-11-08 오후 7 36 27

스크린샷 2021-11-08 오후 7 37 48

  • there is no .rubocop.yml in root.
  • there is a .rubocop.yml in current directory
  • rubocop version is 1.3.1

How can I fix this without upgrade?
Or must I do upgrade?

@KSH-code
Copy link

KSH-code commented Nov 8, 2021

And I found weird function

ksh@gimseonghun-ui-MacBook-Pro crema-bg-rails % rubocop --config=/Users/ksh/crema-bg-rails/.rubocop.yml 
Configuration file not found: /Users/ksh/.rubocop.yml
ksh@gimseonghun-ui-MacBook-Pro crema-bg-rails % rubocop --config=.rubocop.yml                          
Inspecting 1289 files
...............................

absolute path is not applied at all.

@jonas054
Copy link
Collaborator

@KSH-code What's the output in those two cases if you add the --debug flag?

@KSH-code
Copy link

@jonas054

ksh@gimseonghun-ui-MacBook-Pro crema-bg-rails % rubocop --config=/Users/ksh/crema-bg-rails/.rubocop.yml --debug
configuration from /Users/ksh/crema-bg-rails/.rubocop.yml
Inheriting configuration from /Users/ksh/crema-bg-rails/.rubocop_todo.yml
Default configuration from /Users/ksh/.rvm/gems/ruby-2.7.1/gems/rubocop-1.3.1/config/default.yml
Inheriting Configuration file not found: /Users/ksh/.rubocop.yml
Finished in 0.10078600002452731 seconds
ksh@gimseonghun-ui-MacBook-Pro crema-bg-rails % rubocop --config=.rubocop.yml --debug 
configuration from /Users/ksh/Projects/crema/crema-bg-rails/.rubocop.yml
Inheriting configuration from /Users/ksh/Projects/crema/crema-bg-rails/.rubocop_todo.yml
Default configuration from /Users/ksh/.rvm/gems/ruby-2.7.1/gems/rubocop-1.3.1/config/default.yml
Inheriting configuration from /Users/ksh/Projects/crema/.rubocop.yml
configuration from /Users/ksh/.rvm/gems/ruby-2.7.1/gems/rubocop-rails-2.5.2/config/default.yml
configuration from /Users/ksh/.rvm/gems/ruby-2.7.1/gems/rubocop-rails-2.5.2/config/default.yml
.rubocop.yml: AllCops:Exclude overrides the same parameter in .rubocop_todo.yml
Inspecting 1004 files
Scanning /Users/ksh/Projects/crema/crema-bg-rails/Capfile
.Scanning /Users/ksh/Projects/crema/crema-bg-rails/Gemfile

Here you are.

@KSH-code
Copy link

KSH-code commented Nov 15, 2021

ksh@gimseonghun-ui-MacBook-Pro crema-bg-rails % pwd
/Users/ksh/Projects/crema/crema-bg-rails

Oooops it seems a my human error.. maybe from a rubocop extension inside vscode.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants