-
Notifications
You must be signed in to change notification settings - Fork 2k
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
Regression bug: sub tree configs failing to use environment variables #1566
Comments
👋 Thanks for reporting! A maintainer will take a look at your issue shortly. 👀 In the meantime: We are working on Viper v2 and we would love to hear your thoughts about what you like or don't like about Viper, so we can improve or fix those issues. ⏰ If you have a couple minutes, please take some time and share your thoughts: https://forms.gle/R6faU74qPRPAzchZ9 📣 If you've already given us your feedback, you can still help by spreading the news, https://twitter.com/sagikazarmark/status/1306904078967074816 Thank you! ❤️ |
Any update on this? |
` import (
) type Config struct { type DatabaseConfig struct { func main() {
} You could try this code, and see if this resolves your problem? You're manually setting environment variables using os.Setenv() before calling viper.ReadInConfig(). This means that the values from the environment variables set manually will be overwritten by the values from the configuration file during the viper.ReadInConfig() step. You're also setting the DATABASE_PASSWORD to "MyPassword", but the configuration file likely contains a different value for the password field. Since viper.ReadInConfig() is executed after you set the environment variables, the configuration file's value is taking priority over the environment variable. |
Thank you for your reply! Changing the env after using readInConfig doesn't make much sense, most of the time (if not in all situations) you will have your environment set-up before launching your app! Again, the behavior in v1.15 is perfectly fine, but not anymore in v1.16.0. |
Getting hit by this issue after trying the v1.16.0 upgrade too. The fix for sub tree inheritance has broken the automatic env var behaviour. Specifically, the change to env var name used for lookup here: https://github.com/spf13/viper/blob/v1.16.0/viper.go#L1307 If I patch viper to use I don't know this issue or library well enough to understand if our usage pattern is bad or this is a genuine regression that needs fixing. The current behaviour doesn't match this part of the readme though:
|
@redkite127 @robdesbois if I understand correctly the problem appears when you set an env prefix after a Honestly, I find that a bit strange. Although, the API allows that, I would expect that a Viper instance after calling In pseudo code:
Notice, I don't set an env prefix on I believe that is the intended behavior and I believe this test proves that. From what I can tell, you can just drop the following from your code and it should work: subViper.SetEnvPrefix("database")
subViper.AutomaticEnv() viper.SetEnvPrefix("LOGGING") (Note: you may need to set an env key replacer for it to work)
It may need clarification, but I don't think it's incorrect. The env var is prefixed with the env prefixed, but then the additional segments (passed to Sadly the Sub Vipers are only supposed to be used for reading and nothing else. In light of these, I believe what you consider a regression is actually bug well fixed in Viper. But there is a chance I missed something in your examples or didn't understand the issue. Can you provide a failing test that highlights the problem? Maybe that would help getting closer the truth. Thanks! |
Thank you for the reply @sagikazarmark, those 2 lines were more an attempt to force it to work than anything else. Without those, it still fails and takes the value from the config instead of the ENV. I will try to provide you a failing test. In the meantime, someone else provided a fix proposal: #1617 might you be interested in his suggested fix? |
Preflight Checklist
Viper Version
1.16.0
Go Version
1.19.4
Config Source
Environment variables, Files
Format
YAML
Repl.it link
No response
Code reproducing the issue
Expected Behavior
The value of the password of the database is supposed to be taken from the environment variable, set at the beginning of the
main()
. I should haveMyPassword
.Actual Behavior
I have the value from the configuration file and not the one from the environment variable:
admin
Steps To Reproduce
config.yaml
:Additional Information
It works with
viper:v1.15.0
, but it doesn't withviper:v1.16.0
.The text was updated successfully, but these errors were encountered: