How to tell Rubocop to ignore a specific directory or file
RubyRubocopRuby Problem Overview
My project is extending open-source classes from a third-party gem that we don't want to hold to the same coding standards as our own code. Refactoring the gem code isn't a viable option. We just want Rubocop to ignore the copied code.
How can I instruct Rubocop to completely ignore a file or directory?
Ruby Solutions
Solution 1 - Ruby
As per orde's comment with the link to the manual I found .rubocop.yml and added the following:
AllCops:
Exclude:
- 'path/to/excluded/file.rb'
where the path is relative to .rubocop.yml
Solution 2 - Ruby
From rubocop/default.yml
:
AllCops:
Exclude:
- 'node_modules/**/*'
- 'vendor/**/*'
Solution 3 - Ruby
Can also consider comments for a single file. Great for ignoring the linter for a quick and dirty temporary task.
# rubocop:disable all
module TempTask
...
end
# rubocop:enable all
Solution 4 - Ruby
For your convenience, here is the .rubocop.yml I frequently used.
See formal explanation of .rubocop.yml here.
AllCops:
Exclude:
- Berksfile
- recipes/basic.rb
- attributes/*.rb
# Customize rules
Metrics/LineLength:
Max: 95
MethodLength:
Max: 35
Metrics/AbcSize:
Enabled: false
BlockLength:
Max: 70
I constantly bump by rubocop errors and warning. Thus I've published this post.
Solution 5 - Ruby
When using Rubocop through Visual Studio I could not get the former answers to work.
I eventually noticed that Rubocop was not in fact executing from the root of my project file but from my own root directory and so using:
# Doesn't work :(
AllCops:
Exclude:
- db/schema.rb
was failing because db
was not in the same directory that Rubocop was looking for it.
I then changed it to
# Works :)
AllCops:
Exclude:
- */schema.rb
and it works fine. Note there are no quotation marks around the file name.
Solution 6 - Ruby
I'm using a newer version and every time I add Exclude
rubocop will hang. Turned out I need to tell how to merge Exclude
. This might work for you
require:
- rubocop-rails
inherit_mode:
merge:
- Exclude
AllCops:
Exclude:
- 'node_modules/**/*'
- 'vendor/**/*'
NewCops: disable