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

Some offenses only with spring #12854

Open
znz opened this issue Apr 18, 2024 · 2 comments
Open

Some offenses only with spring #12854

znz opened this issue Apr 18, 2024 · 2 comments
Labels

Comments

@znz
Copy link
Contributor

znz commented Apr 18, 2024

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
@koic
Copy link
Member

koic commented Apr 18, 2024

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

@koic koic added the bug label Apr 18, 2024
@Earlopain
Copy link
Contributor

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants