rubocop / rubocop

A Ruby static code analyzer and formatter, based on the community Ruby style guide.

Home Page:https://docs.rubocop.org

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Some offenses only with spring

znz opened this issue · comments

bin/rubocop and bundle exec rubocop detect different offenses with spring-commands-rubocop.


Expected behavior

Same offenses with/without spring.

Actual behavior

With spring:

% bin/rubocop sample.rb
Running via Spring preloader in process 86090
Inspecting 1 file
W

Offenses:

sample.rb:5:5: C: Performance/CollectionLiteralInLoop: Avoid immutable Array literals in loops. It is better to extract it into a local variable or a constant.
    [1, 2].exclude?(i)
    ^^^^^^
sample.rb:7:1: W: Lint/ShadowedException: Do not shadow rescued Exceptions.
rescue ActiveRecord::RecordNotFound, ActiveRecord::ActiveRecordError, StandardError => e ...
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

1 file inspected, 2 offenses detected

Without spring:

% bundle exec rubocop sample.rb
Inspecting 1 file
.

1 file inspected, no offenses detected

Steps to reproduce the problem

I created a minimal reproduce example.

Use https://github.com/znz/spring-rubocop-offenses-sample and run commands in Actual behavior.

RuboCop version

$ [bundle exec] rubocop -V
1.63.2 (using Parser 3.3.0.5, rubocop-ast 1.31.2, running on ruby 3.2.3) [arm64-darwin23]
  - rubocop-performance 1.21.0
  - rubocop-rails 2.24.1

These are bugs in each of the two cops, caused by their dependency on the runtime information.

Just to elaborate a bit, spring preloads the application and this seems to result in RuboCop running in that environment as well so there does appear to happen some accidental runtime analysis:

converted << Kernel.const_get(exception.source)

Something similar happens here https://github.com/rubocop/rubocop-performance/blob/6d72c730962dd0aee9d80daab6e9269a2bbdceee/lib/rubocop/cop/performance/collection_literal_in_loop.rb#L41 where Enumerable now contains methods added by rails.