Skip to content

Rake task gives different results when rails is loaded #508

@rickselby

Description

@rickselby

I noticed a difference when running rubocop and rake rubocop in a rails app. I managed to reproduce this outside of a rails app, when rails is included in Rakefile (which rails apps do, to load the rails rake tasks).

rubocop/rubocop#14430 suggests this is an issue with rubocop-performance.


Expected behavior

rubocop and rake rubocop should report the same cops

Actual behavior

rake rubocop reports an additional cop

Steps to reproduce the problem

.rubocop.yml

---
plugins:
  - rubocop-performance

AllCops:
  NewCops: enable

Gemfile

# frozen_string_literal: true

source 'https://rubygems.org'

gem 'rails', '~> 8.0'
gem 'rake', '~> 13.3'
gem 'rubocop', '~> 1.79'
gem 'rubocop-performance', '~> 1.25'

test.rb

# frozen_string_literal: true

def test
  self.class.reflect_on_all_associations.all? do |assoc|
    [
      %i[restrict_with_error restrict_with_exception].exclude?(assoc.options[:dependent]),
      assoc.macro == :has_one && public_send(assoc.name).nil?,
      assoc.macro == :has_many && public_send(assoc.name).empty?
    ].any?
  end
end

When Rakefile contains:

# frozen_string_literal: true

require 'rubocop/rake_task'

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

the output is

$ bundle exec rake rubocop_debug
Running RuboCop...
For /home/rick/projects/rubocop: configuration from /home/rick/projects/rubocop/.rubocop.yml
Default configuration from /home/rick/.rvm/gems/ruby-3.4.5/gems/rubocop-1.79.2/config/default.yml
Plugin configuration from /home/rick/.rvm/gems/ruby-3.4.5/gems/rubocop-performance-1.25.0/config/default.yml
Use parallel by default.
Running parallel inspection
Inspecting 3 files
Scanning /home/rick/projects/rubocop/Gemfile
Loading cache from /home/rick/.cache/rubocop_cache/4da9c5645bbb636ced4a682072b9b7a752257be4/6d7a3b621ca1730e04accd938619e4bdab66cfb1/31e685053996528d87497ff45743e642f8a93f7f
.Scanning /home/rick/projects/rubocop/Rakefile
Loading cache from /home/rick/.cache/rubocop_cache/4da9c5645bbb636ced4a682072b9b7a752257be4/6d7a3b621ca1730e04accd938619e4bdab66cfb1/cefe1825040f0c39904997d46613f9c8d9dad5e3
.Scanning /home/rick/projects/rubocop/test.rb
Loading cache from /home/rick/.cache/rubocop_cache/4da9c5645bbb636ced4a682072b9b7a752257be4/6d7a3b621ca1730e04accd938619e4bdab66cfb1/cd813e1ce99c3be7ea271a3cf396ebc1ac80bdbf
.

3 files inspected, no offenses detected

However, when Rakefile contains:

# frozen_string_literal: true

require 'rails'
require 'rubocop/rake_task'

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

then the output is:

$ bundle exec rake rubocop_debug
Running RuboCop...
For /home/rick/projects/rubocop: configuration from /home/rick/projects/rubocop/.rubocop.yml
Default configuration from /home/rick/.rvm/gems/ruby-3.4.5/gems/rubocop-1.79.2/config/default.yml
Plugin configuration from /home/rick/.rvm/gems/ruby-3.4.5/gems/rubocop-performance-1.25.0/config/default.yml
Use parallel by default.
Running parallel inspection
Loading cache from /home/rick/.cache/rubocop_cache/173b39b6746e352ba5de31362843bf39d25405c2/6d7a3b621ca1730e04accd938619e4bdab66cfb1/cd813e1ce99c3be7ea271a3cf396ebc1ac80bdbf
Inspecting 3 files
Scanning /home/rick/projects/rubocop/Gemfile
Loading cache from /home/rick/.cache/rubocop_cache/173b39b6746e352ba5de31362843bf39d25405c2/6d7a3b621ca1730e04accd938619e4bdab66cfb1/31e685053996528d87497ff45743e642f8a93f7f
.Scanning /home/rick/projects/rubocop/Rakefile
Loading cache from /home/rick/.cache/rubocop_cache/173b39b6746e352ba5de31362843bf39d25405c2/6d7a3b621ca1730e04accd938619e4bdab66cfb1/48c38e2ad02ae34b7141c661239aca3c0225ecb0
.Scanning /home/rick/projects/rubocop/test.rb
Loading cache from /home/rick/.cache/rubocop_cache/173b39b6746e352ba5de31362843bf39d25405c2/6d7a3b621ca1730e04accd938619e4bdab66cfb1/cd813e1ce99c3be7ea271a3cf396ebc1ac80bdbf
C

Offenses:

test.rb:6:7: C: Performance/CollectionLiteralInLoop: Avoid immutable Array literals in loops. It is better to extract it into a local variable or a constant.
      %i[restrict_with_error restrict_with_exception].exclude?(assoc.options[:dependent]),
      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

3 files inspected, 1 offense detected
RuboCop failed!

RuboCop version

$ bundle exec rubocop -V
1.79.2 (using Parser 3.3.9.0, Prism 1.4.0, rubocop-ast 1.46.0, analyzing as Ruby 3.4, running on ruby 3.4.5) +server [x86_64-linux]
  - rubocop-performance 1.25.0

RuboCop::RakeTask.new(:rubocop_version) { |t| t.options = ['-V'] }

$ bundle exec rake rubocop_version
Running RuboCop...
1.79.2 (using Parser 3.3.9.0, Prism 1.4.0, rubocop-ast 1.46.0, analyzing as Ruby 3.4, running on ruby 3.4.5) [x86_64-linux]
  - rubocop-performance 1.25.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions