You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
There are multiple instances where the behavior is unexpected to me. Looking at the documentation of SafeWriteConfig(), I would expect the config to be written to a file if it doesn't exist. In none of the following cases that happens:
// In this example I suppose not writing the config doesn't surprise// me as much because the docs of `SetConfigFile` say that all// paths are ignored. So maybe viper doesn't know the exact path// of the file.v.SetConfigFile("config.yml")
v.AddConfigPath(".")
v.SafeWriteConfig()
// But in this example with the full path, I would expect viper to// write the config there.v.SetConfigFile(`full/path/to/config.yml`)
v.SafeWriteConfig()
However, the config is created as expected if using
// Writes config to "config.yml" in the current directory (probably// config is some default value for the name).v.SetConfigFile("test.yml")
v.SetConfigType("yml")
v.AddConfigPath(".")
v.SafeWriteConfig()
// Writes config to "config.yml" in the current directory.// Note that in this case viper will not read the config// from "config.yml", if some config is there. Instead, it// will read from "test.yml", which makes it even more// confusing why the config is written to "config.yml".v.SetConfigFile("config")
v.SetConfigType("yml")
v.AddConfigPath(".")
v.SetConfigFile("test.yml")
v.SafeWriteConfig()
It looks to me as if viper.SafeWriteConfig() doesn't play nicely with viper.SetConfigFile().
Additionally, viper.WriteConfig() seems to do the opposite: it writes the file only if set via SetConfigFile() Examples below:
// Writes config in current directory to "test.yml"v.SetConfigFile("test.yml")
v.WriteConfig()
// Doesn't do anything apparentlyv.SetConfigName("config")
v.SetConfigType("yaml")
v.AddConfigPath(".")
v.WriteConfig()
If those examples are working as expected, I'd like to know what would be the reasoning behind. If these are bugs, I'd love to investigate!
The text was updated successfully, but these errors were encountered:
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've already given us your feedback, you can still help by spreading the news,
either by sharing the above link or telling people about this on Twitter:
It appears WriteConfig is first running getConfigFile. When the file does not exist, this function returns ConfigFileNotFoundError or here in viper_go1_15.go. Instead of specifically checking for that error, WriteConfig is blindly returning the error.
It seems that SafeWriteConfig doesn't have this check and defers to SafeWriteConfigAs to check for an existing file. I'm not sure Regardless, it definitely appears WriteConfig's behavior does not match it's documentation.
Furthermore, if the directory indicated by the first path call to AddConfigPath does not exist, SafeWriteConfig and WriteConfig will return an error also instead of attempting to create directories. Documentation for both write functions should probably point this out.
Preflight Checklist
Viper Version
1.15.0
Go Version
1.20
Config Source
Manual set
Format
JSON, YAML
Repl.it link
No response
Code reproducing the issue
Expected Behavior
Viper writes the config to the
config.yml
fileActual Behavior
Nothing seems to happen
Steps To Reproduce
No response
Additional Information
There are multiple instances where the behavior is unexpected to me. Looking at the documentation of
SafeWriteConfig()
, I would expect the config to be written to a file if it doesn't exist. In none of the following cases that happens:However, the config is created as expected if using
Here's some more examples:
It looks to me as if
viper.SafeWriteConfig()
doesn't play nicely withviper.SetConfigFile()
.Additionally,
viper.WriteConfig()
seems to do the opposite: it writes the file only if set viaSetConfigFile()
Examples below:If those examples are working as expected, I'd like to know what would be the reasoning behind. If these are bugs, I'd love to investigate!
The text was updated successfully, but these errors were encountered: