-
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
env prefix and env key replacer not applied to dotenv file #1902
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! ❤️ |
Thank you for raising this issue! Upon investigation, it seems that environment variable prefixes and key replacers are not applied to configurations read from a dotenv file. ExplanationThe merging with prefixes and key replacers is only applied when AutomaticEnv is used. AutomaticEnv monitors the OS environment variables, and on any subsequent call to In contrast, when using ConclusionTo summarize, environment variables are required if you need to apply environment prefixes and key replacers, as these are not automatically applied to configurations loaded from a dotenv file using |
I understand, I've read the sources. What I would expect, as said, dotenv file would be 1 to 1 compatible system envs replacement. Which is not the case now. What I would suggest is to mirror automatic envs methods with ones that will be applied to dotenv files. |
That's certainly an interesting proposition. While I see what that would make sense, architecturally, dotenv files are processed very differently from actual environment variables. One thing you could do as a workaround (starting 1.20) is wrapping the dotenv codec and implementing key replacement yourself: https://github.com/spf13/viper/blob/master/encoding.go I would certainly consider adding it as an option here as well. In any case, the above workaround should work in the short term. |
Preflight Checklist
Viper Version
1.18.2
Go Version
1.22
Config Source
Files
Format
Dotenv
Repl.it link
No response
Code reproducing the issue
Expected Behavior
Dotenv files expected to be threated exactly as env variables
Actual Behavior
Neither env prefix, nor env key replacer not applied to configs being read from dotenv file
Steps To Reproduce
No response
Additional Information
example dotenv file content
The text was updated successfully, but these errors were encountered: