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

configuration in child directory can corrupt configuration in parent directory #10263

Closed
ccutrer opened this issue Nov 23, 2021 · 1 comment
Closed
Assignees
Labels

Comments

@ccutrer
Copy link
Contributor

ccutrer commented Nov 23, 2021

The general problem here is that rubocop is using different configuration for a single file depending on how it's run, and that configuration is wrong. I haven't completely nailed down everything that's affected, but it's at least a fairly simple repro.

Given the following four files:

.rubocop.yml:

require:
  - rubocop-rails

AllCops:
  TargetRubyVersion: 2.7
  NewCops: enable

Rails:
  Severity: error

subdir/.rubocop.yml:

inherit_from:
  - ../.rubocop.yml

Rails:
  Enabled: false

test.rb:

# frozen_string_literal: true

User.where('name = ?', 'Gabe')

subdir/test2.rb (I'm not sure if this file is necessary in order for rubocop to process the configuration file in its directory):

# frozen_string_literal: true

puts 'hi'

Expected behavior

I would expect that rubocop will find an offense of Rails/WhereEquals regardless of where it's run. I.e. rubocop, rubocop -A, rubocop -- test.rb, rubocop -A -- test.rb should find a single offense each time.

Actual behavior

If I run rubocop or rubocop -A, no offenses are found in either file (WRONG!). But if I run rubocop -- test.rb, the offense is found (RIGHT!). My supposition is that there's a bug in the inheritance mechanism that because the entire Rails department is disabled in the sub-directory that state leaks to the parent directory's copy of the configuration. But if you're running just test.rb, the sub-directory's config is never loaded, not having a chance to corrupt the parent. I have reason to believe it's not as simple as that, though. In my full app, the only cops that seems to "regress" with this sort of configuration were all cops that are marked as pending. So it might be that Enabled is getting reset incorrectly somewhere. I'm also not sure if being a separate gem (rubocop-rails) is important at all - that just happened to be the department that I encountered this with.

RuboCop version

1.22.3 (using Parser 3.0.2.0, rubocop-ast 1.13.0, running on ruby 2.7.2 x86_64-darwin20)
  - rubocop-rails 2.12.4
@jonas054 jonas054 self-assigned this Jul 7, 2022
@jonas054 jonas054 added the bug label Jul 7, 2022
@jonas054
Copy link
Collaborator

jonas054 commented Jul 8, 2022

I've been able to reproduce the problem. It's not related to rubocop-rails specifically, but to pending cops in general. A PR is coming.

@koic koic closed this as completed in 0724f1a Jul 8, 2022
koic added a commit that referenced this issue Jul 8, 2022
[Fix #10263] Fix aliasing problem for Enabled in Config
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

2 participants