Preflight Checklist

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

Viper Version

1.19.0

Go Version

1.23.5

Config Source

Flags

Format

YAML

Repl.it link

No response

Code reproducing the issue


viper.SetConfigName("testfile")
viper.SetConfigType("yaml")
viper.AddConfigPath("/dir/dir2/testfile")

Expected Behavior

When searching if file exists, file path should only be searched with list of available extension and custom extension if provided in config.

Actual Behavior

In line 86 in file.go , extension is not added while checking exists so incorrect files will be matched.

Steps To Reproduce

No response

Additional Information

No response

Comment From: rishavch2104

Fixed in https://github.com/spf13/viper/pull/1990

Comment From: rishavch2104

@sagikazarmark can you please check this?

Comment From: sagikazarmark

Admittedly the "old" file search API is weird and not trivial to use. SetConfigType was never intended to be used as a file extension, rather an override and a way to set type information in cases where there is no file name at all.

The proposed fix would interfere with other supported scenarios though. For example, some people set the config name to .env and the type to dotenv to support reading from env files.

I would suggest checking out the newly released file search API in 1.20.0. At allows you to customize the entire file search process, while also gives you a much better default implementation.

Comment From: github-actions[bot]

Issues with no activity for 30 days are marked stale and subject to being closed.