Rubocop: When running RuboCop is run as a rake task it fails, but not if not a rake task

Created on 11 Aug 2015  路  12Comments  路  Source: rubocop-hq/rubocop

Response when running just rubocop

Inspecting 400 files
................................................................................................................................................................................................................................................................................................................................................................................................................

400 files inspected, no offenses detected

When running rubocop via rake task

Running RuboCop...
Inspecting 400 files
......................................................................................................................................................
To see the complete backtrace run rubocop -d.
..........................................................................................................................................................................................................................................................

400 files inspected, no offenses detected


Errors are usually caused by RuboCop bugs.
Please, report your problems to RuboCop's issue tracker.
Mention the following information in the issue report:
0.33.0 (using Parser 2.2.2.6, running on ruby 2.2.2 x86_64-darwin14)
RuboCop failed!

Rakefile

require File.expand_path('../config/application', __FILE__)
Rails.application.load_tasks

if %w(development test).include? Rails.env
  require 'rubocop/rake_task'
  RuboCop::RakeTask.new

  task(:default).clear
  task default: [:rubocop, :spec]
end

.rubocop.yml

AllCops:
  Exclude:
    - 'bin/**/*'
    - 'db/**/*'
    - 'log/**/*'
    - 'tmp/**/*'
    - 'vendor/**/*'
    - 'Guardfile'
  DisplayCopNames: true
  RunRailsCops: true

Metrics/AbcSize:
  Enabled: false

Metrics/CyclomaticComplexity:
  Enabled: false

Metrics/PerceivedComplexity:
  Enabled: false

Metrics/MethodLength:
  Enabled: false

Metrics/LineLength:
  Max: 99
  Exclude:
    - 'config/**/*'

Metrics/ModuleLength:
  Max: 150

Rails/HasAndBelongsToMany:
  Enabled: false

Rails/Validation:
  Enabled: false

Style/AsciiComments:
  Enabled: false

Style/BlockComments:
  Enabled: false

Style/BlockDelimiters:
  Enabled: true
  Exclude:
    - 'spec/controllers/*'

Style/RegexpLiteral:
  Enabled: false

Style/ClassAndModuleChildren:
  Enabled: false

Style/Documentation:
  Enabled: false

Style/Lambda:
  Enabled: false

Style/StringLiterals:
  Enabled: false

Style/SpaceInsideStringInterpolation:
  Enabled: false

Lint/NestedMethodDefinition:
  Enabled: false

Style/ExtraSpacing:
  Enabled: false

rubocop -D also works with no problem

Most helpful comment

I get a different result with the rake task too, but it is not related to exclude patterns. Seems like different rules are loaded. I get this error with the rake task, but not with rubocop :

file.rb:84:3: W: Lint/ShadowedException: Do not shadow rescued Exceptions.
  rescue JSON::ParserError, Faraday::ClientError, Faraday::ResourceNotFound => e
  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

All 12 comments

If you add the following to your Rakefile:

RuboCop::RakeTask.new(:rubocop_debug) do |t|
  t.options = ['-d']
end

You should be able to pinpoint the problem by running rake rubocop_debug. Let us know what you find.

@jonas054 Produces the same results as above i.e. rubocop -d

436 files inspected, no offenses detected

Errors are usually caused by RuboCop bugs.
Please, report your problems to RuboCop's issue tracker.
Mention the following information in the issue report:
0.33.0 (using Parser 2.2.2.6, running on ruby 2.2.2 x86_64-darwin14)
RuboCop failed!

Don't you get a stack trace?

@ericbrooke Can you confirm that when t.options = ['-d'] is added to your Rakefile, you still don't get a stack trace?

Closing due to lack of activity.

Had a similar issue: running "rubocop ." was ok, but running as a Rake task using patterns argument with '/.rb' and *formatters with 'files' was failing, outputting only the files having issues, but no other error message.
The issue came after an update of chefdk tool which is using a newer version of ruby; the solution was to use:

Metrics/BlockLength:
  Max: 100

where block length was previously increased to 80, but still, the error wasn't thrown by simple rubocop call in CLI .

It happened to me too.

This is minimal settings.

Rakefile

require 'treetop'
require 'rubocop/rake_task'

RuboCop::RakeTask.new

.rubocop.yml

inherit_gem:
  foo-bar: .rubocop.yml

foo-bar: .rubocop.yml

Metrics/BlockLength:
  Exclude:
    - spec/**/*.rb
$ rake rubocop
:
spec/unit/recipes/base_spec.rb:3:1 C: Block has too many lines. [27/25]
describe 'sample::base' do ...
^^^^^^^^^^^^^^^^^^^^^^^^^^

Exculde has no effect.
But $ rubocop is OK.

This error happens when treetop is required and configuration is written in inherit_gem.
Treetop is required by foodcritic (https://github.com/acrmp/foodcritic/blob/master/lib/foodcritic.rb)

$ gem list | grep -E 'rubocop|treetop'
rubocop (0.46.0)
treetop (1.6.8)

I get a different result with the rake task too, but it is not related to exclude patterns. Seems like different rules are loaded. I get this error with the rake task, but not with rubocop :

file.rb:84:3: W: Lint/ShadowedException: Do not shadow rescued Exceptions.
  rescue JSON::ParserError, Faraday::ClientError, Faraday::ResourceNotFound => e
  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

I tried above measures, still getting error,
rake aborted
NameError: constant Object:config not defined

What's the output if you define the task as

RuboCop::RakeTask.new(:rubocop_debug) do |t|
  t.options = ['-d']
end

Anybody who has the problem can answer.

It looks like the rake task doesn't load all defaults.

# bundle exec rails rubocop_debug
Running RuboCop...
For /Users/j15e/repos/project: configuration from /Users/j15e/repos/project/.rubocop.yml
Inspecting 173 files

VS

# bundle exec rubocop -d
For /Users/j15e/repos/project: configuration from /Users/j15e/repos/project/.rubocop.yml
configuration from /Users/j15e/.rvm/gems/ruby-2.5.0/gems/rubocop-rspec-1.25.1/config/default.yml
configuration from /Users/j15e/.rvm/gems/ruby-2.5.0/gems/rubocop-rspec-1.25.1/config/default.yml
Default configuration from /Users/j15e/.rvm/gems/ruby-2.5.0/gems/rubocop-0.55.0/config/default.yml
Inheriting configuration from /Users/j15e/.rvm/gems/ruby-2.5.0/gems/rubocop-0.55.0/config/enabled.yml
Inheriting configuration from /Users/j15e/.rvm/gems/ruby-2.5.0/gems/rubocop-0.55.0/config/disabled.yml
Inspecting 173 files

Full logs here (removed file names / project name for confidentiality) :

https://gist.github.com/j15e/6389efeb0fd3112e19c26e602ccef0cf

For anyone interested, I just merged a fix for this, see #8636.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

david942j picture david942j  路  3Comments

benoittgt picture benoittgt  路  3Comments

AndreiMotinga picture AndreiMotinga  路  3Comments

bquorning picture bquorning  路  3Comments

mlammers picture mlammers  路  3Comments