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

Metrics/ModuleLength disabling not respected when rubocop runs inside subdirectory #3664

Closed
ylansegal opened this issue Oct 21, 2016 · 8 comments

Comments

@ylansegal
Copy link

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.

@mikegee
Copy link
Contributor

mikegee commented Oct 21, 2016

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

@mikegee
Copy link
Contributor

mikegee commented Oct 21, 2016

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.

@ylansegal
Copy link
Author

@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.

@mikegee
Copy link
Contributor

mikegee commented Oct 21, 2016

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

@mikegee
Copy link
Contributor

mikegee commented Oct 21, 2016

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.

@ylansegal
Copy link
Author

ylansegal commented Oct 21, 2016

@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.

@dkniffin
Copy link
Contributor

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.

@jonas054
Copy link
Collaborator

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.

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