Reset cache on config name change
I stumbled over this when trying to merge multiple configs.
```
viper.SetConfigName("default")
err := viper.MergeInConfig()
```
which caches file path resolvemenet in `v.configFile`
and then
```
viper.SetConfigName("prod")
err := viper.MergeInConfig()
```
which reuses `v.configFile` without updating it accordingly to the new name.
See https://github.com/spf13/viper/blob/c1ccc378a054ea8d4e38d8c67f6938d4760b53dd/viper.go#L1240
diff --git a/viper.go b/viper.go
index e5a360a..d800395 100644
--- a/viper.go
+++ b/viper.go
@@ -1214,6 +1214,7 @@
func (v *Viper) SetConfigName(in string) {
if in != "" {
v.configName = in
+ v.configFile = ""
}
}
diff --git a/viper_test.go b/viper_test.go
index aa9d7fe..0c0c7e5 100644
--- a/viper_test.go
+++ b/viper_test.go
@@ -901,3 +901,9 @@
assert.Equal(t, &C, &config{Id: 1, FirstName: "Steve", Surname: "Owen"})
}
+
+func TestSetConfigNameClearsFileCache(t *testing.T) {
+ SetConfigFile("/tmp/config.yaml")
+ SetConfigName("default")
+ assert.Empty(t, v.getConfigFile())
+}