Skip to content

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

Merged
merged 2 commits into from
Nov 4, 2022

Conversation

mafredri
Copy link
Member

@mafredri mafredri commented Nov 4, 2022

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.

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`.
@mafredri mafredri self-assigned this Nov 4, 2022
@mafredri mafredri requested a review from a team November 4, 2022 10:05
Copy link
Member

@mtojek mtojek left a 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()
Copy link
Member

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.

Copy link
Member Author

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.

Copy link
Member Author

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

Copy link
Member

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 :)

Comment on lines +395 to +397
// Ensure that we only bind env variables to proper fields,
// otherwise Viper will get confused if the parent struct is
// assigned a value.
Copy link
Member

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?

Copy link
Member Author

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.

Copy link
Member

@mtojek mtojek left a 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.

@mafredri mafredri requested a review from f0ssel November 4, 2022 15:45
Copy link
Contributor

@f0ssel f0ssel left a 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.

@mafredri mafredri merged commit 70048ac into main Nov 4, 2022
@mafredri mafredri deleted the mafredri/fix-viper-envs branch November 4, 2022 17:47
@github-actions github-actions bot locked and limited conversation to collaborators Nov 4, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants