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

Disable Rails/EnvironmentVariableAccess in config/initializers? #46

Closed
danielmorrison opened this issue Jun 26, 2024 · 4 comments · Fixed by #47
Closed

Disable Rails/EnvironmentVariableAccess in config/initializers? #46

danielmorrison opened this issue Jun 26, 2024 · 4 comments · Fixed by #47

Comments

@danielmorrison
Copy link

danielmorrison commented Jun 26, 2024

Upgrading standard-rails to 1.1.0 has started flagging things like:

config/initializers/stripe.rb:1:18: Rails/EnvironmentVariableAccess: Do not read from `ENV` directly post initialization.

IMHO, an initializer is not "post initialization" and is the correct spot for this.

This comes from rubocop/rubocop-rails#1229 which specifically looks at config/initializers/**/*.rb to apply this rule.

What do others think about removing that directory from this check? Happy to whip up a PR if there's enough interest.

@searls
Copy link
Contributor

searls commented Jun 26, 2024

I hate this rule across the board (due to the fact Rails has changed the "correct" way to access ENV vars conventionally 4 times in 10 years) and am absolutely supportive of nixing it. Send a PR to disable

@dogweather
Copy link
Contributor

dogweather commented Jun 29, 2024

Awesome. I came here with this same issue.

I think this rule has never worked as intended. Rubocop-rails has disabled it by default because of a lack of a full example to migrate code.

I suspect that the Rails config and #config_for helper do not provide boot-time env var checking as someone claimed in your conference voting presentation. I think it's the opposite: Rails config is a silent-failure API. Unknown config names just return nil.

Personally, I ENV.fetch in my initializers and store them in namespaced constants. That way, I get some help from the interpreter if I misspell one. This also enables static analysis tools like Sorbet to provide more help.

@dogweather
Copy link
Contributor

dogweather commented Jun 29, 2024

@searls wrote:

Send a PR to disable

#47

@danielmorrison
Copy link
Author

You're a hero, @dogweather!

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

Successfully merging a pull request may close this issue.

3 participants