Skip to content
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

Unexpected viper.WriteConfig() and viper.SafeWriteConfig() behaviour #1514

Open
3 tasks done
Ozoniuss opened this issue Feb 23, 2023 · 2 comments
Open
3 tasks done

Unexpected viper.WriteConfig() and viper.SafeWriteConfig() behaviour #1514

Ozoniuss opened this issue Feb 23, 2023 · 2 comments
Labels
kind/bug Something isn't working

Comments

@Ozoniuss
Copy link

Ozoniuss commented Feb 23, 2023

Preflight Checklist

  • I have searched the issue tracker for an issue that matches the one I want to file, without success.
  • I am not looking for support or already pursued the available support channels without success.
  • I have checked the troubleshooting guide for my problem, without success.

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!

@Ozoniuss Ozoniuss added the kind/bug Something isn't working label Feb 23, 2023
@github-actions
Copy link

👋 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,
either by sharing the above link or telling people about this on Twitter:

https://twitter.com/sagikazarmark/status/1306904078967074816

Thank you! ❤️

@dkolb
Copy link

dkolb commented Mar 3, 2023

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind/bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants