Rubocop: Metrics/ModuleLength disabling not respected when rubocop runs inside subdirectory

Created on 21 Oct 2016  路  8Comments  路  Source: rubocop-hq/rubocop

Expected behavior

I want to disable the Metrics/ModuleLength cop. If rubocop runs from inside a subdirectory of the project, the configuration is not respected. To reproduce, create the following two files:

# .rubocop.yml
Metrics/ModuleLength:
  Enabled: false
# spec/example_spec.rb
require 'rspec'

describe 'Example' do
  it 'adds' do
    expect(1 + 1).to be eq(2)
  end

  it 'adds' do
    expect(1 + 1).to be eq(2)
  end

  it 'adds' do
    expect(1 + 1).to be eq(2)
  end

  it 'adds' do
    expect(1 + 1).to be eq(2)
  end

  it 'adds' do
    expect(1 + 1).to be eq(2)
  end

  it 'adds' do
    expect(1 + 1).to be eq(2)
  end

  it 'adds' do
    expect(1 + 1).to be eq(2)
  end

  it 'adds' do
    expect(1 + 1).to be eq(2)
  end

  it 'adds' do
    expect(1 + 1).to be eq(2)
  end
end

When running rubocop from the root of the project. It works as expected, and no offenses are found:

$ rubocop
Inspecting 1 file
.

1 file inspected, no offenses detected

When running inside the spec directory, the cop doesn't seem to be disabled

$ cd spec
$ rubocop -d
For /Users/ylansegal/Downloads/test/spec: configuration from /Users/ylansegal/Downloads/test/.rubocop.yml
Default configuration from /Users/ylansegal/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/rubocop-0.44.1/config/default.yml
Inheriting configuration from /Users/ylansegal/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/rubocop-0.44.1/config/enabled.yml
Inheriting configuration from /Users/ylansegal/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/rubocop-0.44.1/config/disabled.yml
Inspecting 1 file
Scanning /Users/ylansegal/Downloads/test/spec/example_spec.rb
C

Offenses:

example_spec.rb:3:1: C: Block has too many lines. [27/25]
describe 'Example' do ...
^^^^^^^^^^^^^^^^^^^^^

1 file inspected, 1 offense detected

Actual behavior

This behavior is only present in 0.44.1, but not in 0.43.0

$ cd spec
$ rubocop _0.43.0_
Inspecting 1 file
.

1 file inspected, no offenses detected
$ rubocop _0.44.1_
Inspecting 1 file
C

Offenses:

example_spec.rb:3:1: C: Block has too many lines. [27/25]
describe 'Example' do ...
^^^^^^^^^^^^^^^^^^^^^

1 file inspected, 1 offense detected

RuboCop version

$ rubocop _0.44.1_ -V
0.44.1 (using Parser 2.3.1.4, running on ruby 2.3.1 x86_64-darwin15)
$ rubocop _0.43.0_ -V
0.43.0 (using Parser 2.3.1.4, running on ruby 2.3.1 x86_64-darwin15)

_Note_: I noticed this issue running rubocop from inside Atom, using the linter-rubocop package, which modifies the present working directory to be the one that contains the file passed to rubpcop.

Thank you for maintaining this project: It provides great value to the ruby community.

Most helpful comment

@mikegee,

Thanks for pointing me in the right direction. In my original mistake, as you pointed out I disabled Metrics/ModuleLength, when I thought I was disabling also Metrics/BlockLength.

In any case, I can remove the offenses that were bothering me by using a different exclusion in my rubocop.yml:

Metrics/ModuleLength:
  Exclude:
    - "**/*_spec.rb"

Metrics/BlockLength:
  Exclude:
    - "**/*_spec.rb"

I am closing this ticket, because I have a good workaround, but I'll leave it up to you to consider maybe changing the default regex so that Atom users don't get bit by this.

Thanks a lot for the help: Your time is appreciated.

All 8 comments

Looks like the new Metrics/BlockLength cop is complaining here. Its default is 25 lines.

Use the -D/--display-cop-names command line flag or stick this in your .rubocop.yml to get the cop names in the output:

AllCops:
  DisplayCopNames: true

It helps a bunch with debugging.

@mikegee,

I've added cop names to the .rubocop.yml as suggested:

$ cd spec
$ rubocop
Inspecting 1 file
C

Offenses:

example_spec.rb:3:1: C: Metrics/BlockLength: Block has too many lines. [27/25]
describe 'Example' do ...
^^^^^^^^^^^^^^^^^^^^^

1 file inspected, 1 offense detected

Somehow, that cop is not disabled as expected when I run robocop from inside the subdirectory.

Did you disable Metrics/BlockLength, too? Your original post only shows Metrics/ModuleLength disabled.

I just realized Metrics/BlockLength excludes spec/**/*.rb. That probably explains different behavior based on where you run Rubocop.

In particular, example_spec.rb won't be ignored, but spec/example_spec.rb will be.

@mikegee,

Thanks for pointing me in the right direction. In my original mistake, as you pointed out I disabled Metrics/ModuleLength, when I thought I was disabling also Metrics/BlockLength.

In any case, I can remove the offenses that were bothering me by using a different exclusion in my rubocop.yml:

Metrics/ModuleLength:
  Exclude:
    - "**/*_spec.rb"

Metrics/BlockLength:
  Exclude:
    - "**/*_spec.rb"

I am closing this ticket, because I have a good workaround, but I'll leave it up to you to consider maybe changing the default regex so that Atom users don't get bit by this.

Thanks a lot for the help: Your time is appreciated.

I am running into this issue now, and it's related to AtomLinter/linter-rubocop#174. I'd like to request changing the default to what @ylansegal suggested.

Starting in version 0.47.0, there is no longer a default setting for Exclude in Metrics/BlockLength. It's been replaced by a default (empty) setting of ExcludedMethods. So we're all in the same boat now - linter-rubocop users and others - in that we have to set up exclusions ourselves for this cop, and can't rely on defaults.

Was this page helpful?
0 / 5 - 0 ratings