-
-
Notifications
You must be signed in to change notification settings - Fork 263
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
Add new Rails/EnvironmentVariableAccess cop #442
Add new Rails/EnvironmentVariableAccess cop #442
Conversation
I saw case of assigning to environment variable required by library (e.g.: |
I came up with a mundane idea of splitting the cop by reference and assignment to |
I can add a configuration option. In the interest of time, I think I will limit to |
I think it's good enough :-) |
307fe9b
to
d558844
Compare
@koic Added! |
Not sure why JRuby is failing. Seems unrelated to the change. |
@Drenmi Can you rebase with the latest master branch and update this PR? |
Will have a look this week. 🙏 |
@Drenmi I plan to release new RuboCop Rails in a few days. Is it possible to make progress this PR? |
70d8383
to
6931aba
Compare
6931aba
to
cef2b7b
Compare
Sorry for the delay, @koic. I've been caught up with work. I just rebased this and incorporated the suggested documentation. |
@Drenmi Thank you for updating! 😃 |
@Drenmi how do you feel about |
Hey, @swrobel. I initially had this response, but I've come around to the idea and am changing my code to comply with the cop. You're right that
I find this really handy; it informs which variables my environment needs, without needing to rummage through the whole codebase to check if there are any ENV.fetch, etc. (that I wouldn't otherwise notice until runtime) and prevents me accidentally forgetting to add a new (optional or required) env var to a You could of course use this to have the same effect if helpful; # in initializer or config setup
Rails.application.config.my_undefined_env_var = nil
# deep in code somewhere
Rails.application.config.my_undefined_env_var || 'will return this' What do you think? |
While I'm not against this cop itself, it was an unpleasant surprise to stumble upon after a rubocop-rails update. I need to invent my own thing to make it pass, and while that's surely possible - based on the PR-description and comments - I don't think it's ideal. There need to be a corresponding best practice example for this cop, in the Rails Style Guide, or somewhere else. As it is now I fear this cop will mostly lead to fragmentation and wasted time. |
@Drenmi can you provide example code for how you're loading your env vars via |
@Drenmi Could you write a migration guide to a good case? Probably due to user confusion, I'm considering disabling the cop by default if migration explanation doc is not yet in place by the next release. |
This PR sets disabled by default for `Rails/EnvironmentVariableAccess`. The default status will be set to `pending` again when migration doc is written. rubocop#442 (comment)
Background
In my team, we made a decision to stop accessing
ENV
directly from within application code. We did this because of primarily two reasons:ENV
variables at one glance.Instead, we added an
environment.yml
file, and we load it in ourapplication.rb
using Rails'#config_for
helper, and copy all the key-value pairs into the Rails configuration object. This solves both our problems, asenvironment.yml
gives an at-a-glance reference for the variables, and any missing configuration causes a failure at boot-time, rather than at runtime.What is this change
This is the next step. Enforcement. We currently have no means of preventing
ENV
access from slipping into our application code, which means it will happen. 🙂This PR adds a cop that flags all access to
ENV
inside/app
and/lib
(excluding rake tasks.)Before submitting the PR make sure the following are checked:
[Fix #issue-number]
(if the related issue exists).master
(if not - rebase it).and description in grammatically correct, complete sentences.
bundle exec rake default
. It executes all tests and RuboCop for itself, and generates the documentation.