standardrb / standard-rails

A Standard Ruby plugin that configures rubocop-rails
MIT License
163 stars 15 forks source link

Can't ignore Rails/EnvironmentVariableAccess cop #7

Closed knutsenm closed 1 year ago

knutsenm commented 1 year ago

standardrb --generate-todo does create Rails exclusions; example:

% ack -A2 lib/log/logger.rb .standard_todo.yml
- lib/log/logger.rb:
  - Style/GlobalStdStream
  - Rails/EnvironmentVariableAccess

However, despite this:

% standardrb --format simple
[ ... ]
== lib/log/logger.rb ==
C: 15: 10: Rails/EnvironmentVariableAccess: Do not read from ENV directly post initialization.
C: 16: 36: Rails/EnvironmentVariableAccess: Do not read from ENV directly post initialization.
searls commented 1 year ago

This is probably a legit bug as I've never used this feature or really understand how it's implemented

knutsenm commented 1 year ago

OK, could @mrbiggred possibly help?

knutsenm commented 1 year ago

Hmm, it's not a todo thing. I have the same problem even after I move the lines to .standard.yml:

+++ b/.standard.yml
@@ -2,3 +2,6 @@ plugins:
   - standard-rails
 ignore:
   - 'db/migrate/*'
+  - 'lib/log/logger.rb':
+    - Style/GlobalStdStream
+    - Rails/EnvironmentVariableAccess
knutsenm commented 1 year ago

The Style/GlobalStdStream ignore in my .standard.yml example above does work (tested by removing it), it's only the Rails cop that's not ignored.

knutsenm commented 1 year ago

The problem is even more specific: I can't ignore the Rails/EnvironmentVariableAccess cop. Other Rails cops can be ignored from either file. I changed the subject accordingly.

searls commented 1 year ago

Thanks for narrowing your report. Unfortunately I was unable to replicate your issue in this commit, which adds a test of an ignore

https://github.com/standardrb/standard/commit/c741249517bbda747cb06d393e512c4212a8502b

Note that the nested ignore here is two levels of indentation deep (one more than you might expect) so that the YAML parses it as a nested map and not a subsequent element in an array:

ignore:
  - '**/*.rb':
      - Bananas/BananaBomb

Your example was this:

- lib/log/logger.rb:
  - Style/GlobalStdStream
  - Rails/EnvironmentVariableAccess

Does this work?

- lib/log/logger.rb:
    - Style/GlobalStdStream
    - Rails/EnvironmentVariableAccess
knutsenm commented 1 year ago

Note that the nested ignore here is two levels of indentation deep

I'm sorry to report that doesn't help in my case.

I'm baffled why this one particular cop can't be ignored. I'm trying to convert a private repo from rubocop to standard, and have removed old .rubocop.yml and .rubocop_todo.yml files. Rails/EnvironmentVariableAccess is disabled by default in rubocop-rails, and so the old config and code didn't have to ignore it.

I do note that I can successfully rubocop:disable or standard:disable it in code comments, so that's something.

mrbiggred commented 1 year ago

@knutsenm I'm not sure why the Rails cops aren't being ignored. I haven't used this new Standard Rails plugin. I'll play around with it and if I find anything I'll post an update.

@knutsenm do you have an example project that show cases the issue?

knutsenm commented 1 year ago

@knutsenm do you have an example project that show cases the issue?

Yes, see https://github.com/CruGlobal/rails-infrastructure-canary/pull/99

The Ruby/lint Action fails the same way it does for me locally,

== lib/log/logger.rb ==
C: 15: 10: Rails/EnvironmentVariableAccess: Do not read from ENV directly post initialization.
C: 16: 36: Rails/EnvironmentVariableAccess: Do not read from ENV directly post initialization.
searls commented 1 year ago

I figured this out and it was not fun. See https://github.com/standardrb/standard/commit/ed4253a0cdc3e8790cacf2e397f7c28e2b53157c for a description

Released a fix in standard@1.28.4

mrbiggred commented 1 year ago

Glad you figured it out @searls.

WMahoney09 commented 4 months ago

I'm unable ignore this rule, I'm on Standard version: 1.36.0


plugins:
  - standard-rails
ignore:
  - 'Rails/EnvironmentVariableAccess'
bb commented 1 month ago

@WMahoney09 try this:

plugins:
  - standard-rails

ignore:
  - config/initializers/*.rb:
      - Rails/EnvironmentVariableAccess