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

Custom cop instantiated multiple times when support_multiple_source? returns true #11119

Open
karouf opened this issue Oct 24, 2022 · 0 comments

Comments

@karouf
Copy link

karouf commented Oct 24, 2022

I have a use case where it would be handy to have a custom cop only instantiated once for the whole Rubocop run. After reading the v1 upgrade notes and the matching API doc, I got the idea that implementing on my custom cop a #support_mulitple_source? class method that would return true should do the trick.
Unfortunately, it seems I either missed (or misunderstood) the docs or there is a bug with this feature.
It seems to have been introduced with #7868 which main point wasn't that specific feature but autocorrection API. Which could explain if there was an issue with it. Also I'm not sure it's really used that much.
I extracted a small git repo to make it easier to reproduce the issue: https://github.com/karouf/rubocop_support_multiple_source

Expected behavior

The cop is instantiated only once, therefore the @test instance variable is set when the first file is scanned and its value only displayed once.

$ bundle exec rubocop --cache false
Inspecting 2 files
@test not assigned yet
.
.

2 files inspected, no offenses detected

Actual behavior

The cop is instanciated for each file that Rubocop scans, and the value of @test is displayed twice.

$ bundle exec rubocop --cache false --debug
For /home/renaud/code/rubocop_multiple_sources: configuration from /home/renaud/code/rubocop_multiple_sources/.rubocop.yml
Default configuration from /home/renaud/.rbenv/versions/3.1.2/lib/ruby/gems/3.1.0/gems/rubocop-1.37.1/config/default.yml
Inspecting 2 files
Scanning /home/renaud/code/rubocop_multiple_sources/file1.rb
@test not assigned yet
.Scanning /home/renaud/code/rubocop_multiple_sources/file2.rb
@test not assigned yet
.

2 files inspected, no offenses detected
Finished in 0.11073834299986629 seconds

Steps to reproduce the problem

git clone https://github.com/karouf/rubocop_support_multiple_source.git
cd rubocop_support_multiple_source
bundle install
bundle exec rubocop

RuboCop version

 $ bundle exec rubocop -V
1.37.1 (using Parser 3.1.2.1, rubocop-ast 1.23.0, running on ruby 3.1.2) [x86_64-linux]
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

1 participant