From 5619c0edbec91139df79dd0d0d8d4967f4646a08 Mon Sep 17 00:00:00 2001 From: Roland Schilter Date: Fri, 5 Aug 2016 09:18:19 +0200 Subject: [PATCH] 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 --- viper.go | 1 + viper_test.go | 6 ++++++ 2 files changed, 7 insertions(+) diff --git a/viper.go b/viper.go index e5a360a..d800395 100644 --- a/viper.go +++ b/viper.go @@ -1214,6 +1214,7 @@ func SetConfigName(in string) { v.SetConfigName(in) } 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 @@ func TestUnmarshalingWithAliases(t *testing.T) { 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()) +}