-
Notifications
You must be signed in to change notification settings - Fork 524
CLOUDP-302068: Add a linter to forbid env var usage #1690
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
Conversation
We want to avoid spreading the practice of accessing environment variables deep in the call hierarchy. We only want to access env vars in the `main` package or very close to it. This commit adds a linter to help us with that. It also adds `nolint:forbidigo` exceptions into places: * Where we allow access (`main` package) * Where we still unfortunately have access. These places ideally need to change with time. To find remaining undesired places, from repo root you can `grep` all files for `nolint:forbidigo` excluding files with `main` package. For example: `grep "nolint:forbidigo" --exclude main.go -r .`
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.
Awesome!
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.
LGTM! Thanks! One Q
cmd/manager/main.go
Outdated
@@ -44,7 +44,7 @@ func configureLogger() (*zap.Logger, error) { | |||
func hasRequiredVariables(logger *zap.Logger, envVariables ...string) bool { | |||
allPresent := true | |||
for _, envVariable := range envVariables { | |||
if _, envSpecified := os.LookupEnv(envVariable); !envSpecified { | |||
if _, envSpecified := os.LookupEnv(envVariable); !envSpecified { // nolint:forbidigo |
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.
q: I thought we are fine with all files named main.go
to keep the env var usage? If yes, could we exclude the whole file instead of single lines? Or is that on purpose here?
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.
@nammn I considered doing this, but as far as I understand it is only possible to exclude whole linter. It is not an issue right now because we only use forbidigo
only for env vars right now. But in the future we decide to use forbidigo
for something else main.go
will become a blind spot for this linter.
What do you think - still worth adding an exception for main.go?
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 its better to skip the whole file for now and in case we add more linters we can revisit this decision.
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.
Summary:
We want to avoid spreading the practice of accessing environment variables deep in the call hierarchy. We only want to access env vars in the
main
package or very close to it.This commit adds a linter to help us with that. It also adds
nolint:forbidigo
exceptions into places where we still unfortunately have access. These places ideally need to change with time.To find remaining undesired places, from repo root you can
grep
all files fornolint:forbidigo
. For example:grep "nolint:forbidigo" -r .
All Submissions:
closes #XXXX
in your comment to auto-close the issue that your PR fixes (if such).