116

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?

emery
  • 8,603
  • 10
  • 44
  • 51
  • 1
    Try https://github.com/bbatsov/rubocop#includingexcluding-files – orde Apr 27 '16 at 17:02
  • 1
    Why did you copy the gem's source code into your project's folder? – spickermann Apr 27 '16 at 17:25
  • 1
    spickermann, because we need to modify some methods on the class, and our tech lead said we aren't allowed to use monkey patches to do it. – emery Apr 27 '16 at 18:24
  • As an aside, why is Rubocop picking up the Gem's source code? Did you bundle-install the gem into a sub directory within your project? – user229044 Apr 29 '16 at 14:00
  • As another aside, if the gem is on github, you can clone the gem's github repo and then point your Gemfile to your clone. This is very easy to do. – Wayne Conrad Apr 29 '16 at 14:14
  • A new module was written in our code, with classes that extend the CukeForker gem's classes and override their methods, in order to modify the gem's output and colorization. We had previously been monkey patching the gem's methods, but wanted to do this instead. However, within one of the gem classes that our module extended and called, an original method was being called instead of our new method, so that code needed to be copied into the new module and modified to call the new class. It was that code that caused Rubocop to fail. – emery Apr 30 '16 at 17:25
  • 2
    The manual section referenced in @orde's comment is now at http://rubocop.readthedocs.io/en/latest/configuration/#includingexcluding-files rather than on the github main page. – David Moles Aug 25 '16 at 18:22
  • @emery why don't you use refinements? http://yehudakatz.com/2010/11/30/ruby-2-0-refinements-in-practice/ – Yo Ludke Oct 19 '16 at 15:37

6 Answers6

141

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

David Moles
  • 48,006
  • 27
  • 136
  • 235
emery
  • 8,603
  • 10
  • 44
  • 51
  • 5
    This should probably be the accepted answer, since it's short. One important note I just ran into though: this will override the default AllCops.Excludes, which in Rails mode at least, will prevent rubocop from checking `node_modules` and `vendor`. So you'll likely want to add those to your excludes as well. – Ramfjord Nov 08 '18 at 21:29
  • 3
    Note that you can do this for **specific** cops as well. E.g. `Metrics/BlockLength: \n Exclude: \n - 'Rakefile' \n - '**/*.rake' \n - 'factories/**/*.rb' \n - 'spec/**/*.rb'` I wish you could do newlines in code snippets for sub-comments. – stwr667 Oct 15 '19 at 04:06
  • Make sure you keep the default values as well (see https://github.com/rubocop/rubocop/blob/924b939202eddeee2516aec958a2487356f3e53c/config/default.yml#L63-L67) – Spone Feb 23 '22 at 21:40
57

From rubocop/default.yml:

AllCops:
  Exclude:
    - 'node_modules/**/*'
    - 'vendor/**/*'
Jason Swett
  • 43,526
  • 67
  • 220
  • 351
Dorian
  • 22,759
  • 8
  • 120
  • 116
34

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
Dennis
  • 56,821
  • 26
  • 143
  • 139
  • Bear in mind that this won't skip the file, it will only suppress the warning, but Rubocop seems to still check the file. – Sergio Dec 15 '21 at 12:31
22

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.

Common Rubocop Errors: Improve Your Ruby Code Quality

noraj
  • 3,964
  • 1
  • 30
  • 38
DennyZhang
  • 342
  • 2
  • 7
3

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.

Peter Nixey
  • 16,187
  • 14
  • 79
  • 133
3

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
abmap
  • 141
  • 5