diff --git a/viper.go b/viper.go index 7b12b36..ee09760 100644 --- a/viper.go +++ b/viper.go @@ -1978,12 +1978,6 @@ func (v *Viper) searchInPath(in string) (filename string) { } } - if v.configType != "" { - if b, _ := exists(v.fs, filepath.Join(in, v.configName)); b { - return filepath.Join(in, v.configName) - } - } - return "" } @@ -1998,6 +1992,14 @@ func (v *Viper) findConfigFile() (string, error) { return file, nil } } + + for _, in := range v.configPaths { + if v.configType != "" { + if b, _ := exists(v.fs, filepath.Join(in, v.configName)); b { + return filepath.Join(in, v.configName), nil + } + } + } return "", ConfigFileNotFoundError{v.configName, fmt.Sprintf("%s", v.configPaths)} } diff --git a/viper_test.go b/viper_test.go index b8ceccb..3f1fb35 100644 --- a/viper_test.go +++ b/viper_test.go @@ -361,6 +361,30 @@ func TestSearchInPath_FilesOnly(t *testing.T) { assert.NoError(t, err) } +func TestSearchInPath_ExtensionFirst(t *testing.T) { + exceptFile := "/tmp/withExtension.yaml" + _, err := v.fs.Create(exceptFile) + defer func() { + _ = v.fs.Remove("/tmp/withExtension.yaml") + }() + + assert.NoError(t, err) + _, err = v.fs.Create("./.withoutExtension") + assert.NoError(t, err) + defer func() { + _ = v.fs.Remove("./.withoutExtension") + }() + + SetConfigName("withExtension") + SetConfigType("yaml") + AddConfigPath(".") + AddConfigPath("/tmp") + + filename, err := v.getConfigFile() + assert.Equal(t, exceptFile, filename) + assert.NoError(t, err) +} + func TestDefault(t *testing.T) { SetDefault("age", 45) assert.Equal(t, 45, Get("age"))