-
Notifications
You must be signed in to change notification settings - Fork 889
fix: Disable viper auto-env to avoid assigning to parent structs #4893
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
The viper automatic env mapping and BindEnv were both creating mappings like `vip.BindEnv("telemetry", "CODER_TELEMETRY")` which we don't want since `DeploymentConfig.Telemetry` is a struct housing fields. For some reason, this was causing `DeploymentConfig.Telemetry.URL` to **not** be assigned its default value when `CODER_TELEMETRY=false` was set as an environment variable. Potentially we would want `"telemetry.enable"` to be mapped to `"CODER_TELEMETRY"` for simplicity. But that behavior is not changed by this commit. Arguably, we could remove `vip.SetEnvPrefix` and `vip.SetEnvKeyReplacer` as well since we're manually controlling all environment variable names via `formatEnv`.
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 left some nitpicks, so feel free to ignore these.
@@ -370,7 +370,6 @@ func Config(flagset *pflag.FlagSet, vip *viper.Viper) (*codersdk.DeploymentConfi | |||
return nil, xerrors.Errorf("get global config from flag: %w", err) | |||
} | |||
vip.SetEnvPrefix("coder") | |||
vip.AutomaticEnv() |
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.
Have you reviewed open issues for the spf13/viper? There are a few open regarding vip.AutomaticEnv
. Maybe it's a bug you could raise with the team.
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.
Not really, I thought it'd be good for us to manually control these (we already had all the code in place to do it). But it's a good suggestion, I did a quick browse but nothing stood out. I also just pushed a follow-up PR for this: #4894 in support of not enabling AutomaticEnv
.
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.
This might be related, but it's not the exact same issue. Still, looks like a rabbit hole I don't want to dive into 😄 spf13/viper#1243
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.
Good for me, thanks for reviewing those issues! I suspected that this might be a rabbit hole hence the idea of covering it with a basic unit test. It's up to you to decide if you prefer to open a new issue for viper or leave it as it :)
// Ensure that we only bind env variables to proper fields, | ||
// otherwise Viper will get confused if the parent struct is | ||
// assigned a value. |
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 writing the comment here. I'm afraid that we may run into this issue again in the future (with library bump). Do you think that it's possible to prepare a basic unit test to validate the behavior?
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'm afraid 2f4ac30
(#4893) is the best I can do. I didn't find too many ways to reproduce the issue.
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.
Code LGTM, but please ask somebody else to double-check it.
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.
Nice catch, this makes sense to me, I figured we would probably have to drop vip.AutomaticEnv()
eventually but I'm glad we are still keeping the same effective behavior.
The viper automatic env mapping and BindEnv were both creating mappings
like
vip.BindEnv("telemetry", "CODER_TELEMETRY")
which we don't wantsince
DeploymentConfig.Telemetry
is a struct housing fields.For some reason, this was causing
DeploymentConfig.Telemetry.URL
tonot be assigned its default value when
CODER_TELEMETRY=false
wasset as an environment variable.
Potentially we would want
"telemetry.enable"
to be mapped to"CODER_TELEMETRY"
for simplicity. But that behavior is not changed bythis commit.
Arguably, we could remove
vip.SetEnvPrefix
andvip.SetEnvKeyReplacer
as well since we're manually controlling all environment variable names
via
formatEnv
.