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
func main() {
v := viper.New()
v.SetConfigFile("config.yml")
v.Set("number", 100)
v.SafeWriteConfig()
}
Expected Behavior
Viper writes the config to the config.yml file
Actual 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:
// 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
v.SetConfigName("config")
v.SetConfigType("yml")
v.AddConfigPath(".")
v.SafeWriteConfig()
Here's some more examples:
// 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 apparently
v.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!
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.ymlfileActual 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!