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 Style/FetchEnvVar
cop
#10502
Add new Style/FetchEnvVar
cop
#10502
Conversation
Seems this cop does not attract anyone; I’m closing this PR :) |
We've got a pretty big backlog, so we're moving relatively slowly through all PRs. Lack of feedback, doesn't mean we're not interested in some proposal. I hope to get to review this over the weekend. |
I just reviewed the cop and I like the idea overall. It seems, however, there's no cop for checking the same thing for hashes (likely because it's hard to infer what's a hash or not). I've been wondering if it makes sense to try to make a more generic cop that checks hashes and ENV (maybe with some configuration options to decide what exactly you want to check) or just limit this to what's currently being proposed. The current cop definitely has the advantage of being free from false positives which is nice. @rubocop/rubocop-core Any opinions? |
@bbatsov Thank you for reviewing the code. (and sorry for my impatiently closing the PR) |
In my opinion, guarding against |
Okay, let's limit the scope just to env variables. One thing I'd like us to do is to come up with a shorter name for the cop, though - the currently suggest name is descriptive, but it's super long. Perhaps something like |
.rubocop.yml
Outdated
@@ -42,6 +42,15 @@ Style/IpAddresses: | |||
Exclude: | |||
- spec/rubocop/cop/style/ip_addresses_spec.rb | |||
|
|||
Style/ReadingEnvVarWithoutDefaultValue: | |||
ExcludedEnvVars: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd name this AllowedEnvVars
or just AllowedVars
.
You can also add this to the cop metadata as a reference. |
How about For the same reason, I'm wondering if the error message below was better, while the auto-correction probably should give - Use `ENV.fetch('X', nil)` instead of `ENV['X']`.
+ Use `ENV.fetch('X')` or `ENV.fetch('X', nil)` instead of `ENV['X']`. What do you think? |
I like the proposed changes to the cop name and its message.
Yeah, exactly. |
Style/ReadingEnvVarWithoutDefaultValue
copStyle/FetchEnvVar
cop
Thanks, I updated this PR. |
.rubocop.yml
Outdated
- XDG_CACHE_HOME | ||
- XDG_CONFIG_HOME | ||
- CIRCLE_STAGE | ||
- CI |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How were these 6 variable names chosen as exceptions to the new rule?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for pointing it out. They are the environment variables used in this project. I listed them here to get opinions on how these should be. I think we can replace almost all ENV[...]
in this project with ENV.fetch(..., nil)
, except that here probably should be replaced with ENV.fetch(...)
without the default values. Do you have any opinions?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added a commit to show how the code will be.
1901117
|
||
# rubocop:disable Metrics/CyclomaticComplexity | ||
def offensive?(node) | ||
only_node_of_expression?(node) || |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it'd be a good idea to add some explanatory comment here, as it's not very clear why all those checks are needed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great point. I will write some comment for that.
BTW, this cop ignore some ENV[]
in the below cases;
- when an
ENV[]
is no more than a flag for a condition - when an
ENV[]
receives some message (This may be controversial though)
This feature is documented in the also good
section of the documentation.
I’m going to mention about above as well, but would like to hear if this idea sounds good for others.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added some comments and made the logic simpler.
17a52ef
2b6e80a
to
17a52ef
Compare
Looks good. Just address the conflict in |
I resolved the conflict and fixed a method so that it can cover another case. |
Thanks! |
The thing about ENV is that its values (and keys) will always be a (frozen) string so the Ruby Style Guide |
As you pointed out, the Style Guide The purpose of this cop is to alert the developers that the code to read an environment variable is possibly |
Hey there, wanted to point out something I noticed when running this cop's autocorrection. Code like value = ENV['EXAMPLE_KEY'] || 'default_value' gets autocorrected to value = ENV.fetch('EXAMPLE_KEY', nil) || 'default_value' I ended up manually changing that to value = ENV.fetch('EXAMPLE_KEY', 'default_value') in my codebase. It'd be neat if the autocorrection could handle that pattern. |
@rmacklin I agree with you, I've replaced a lot of |
That's true. I opened an issue for that enhancement. |
Because `.rubocop.yml` is set to `NewCops: enable`, these newly added cops detect offenses when Rake Task is running. * rubocop/rubocop#10243 * rubocop/rubocop#10502
Reading environment variables using brackets like
ENV['SOME_KEY']
may unexpectedly be evaluated asnil
if the variable is unset.Because ENV behaves like a Hash, I think the style guide for Hash can be applied to ENV as well.We can use
ENV.fetch
likeHash#fetch
.https://rubystyle.guide/#hash-fetch-defaults
This cop suggests
ENV.fetch
for the replacement ofENV[]
.(Description updated)
Edited a confusing description where referencing to a style guide
Hash#fetch
.Before submitting the PR make sure the following are checked:
[Fix #issue-number]
(if the related issue exists).master
(if not - rebase it).bundle exec rake default
. It executes all tests and runs RuboCop on its own code.{change_type}_{change_description}.md
if the new code introduces user-observable changes. See changelog entry format for details.