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.15.0

Go Version

1.19

Config Source

Files

Format

Dotenv

Repl.it link

No response

Code reproducing the issue

var Config appConfig

type appConfig struct {
        ...
}

func LoadConfig(configFile string) error {

        v := viper.New()

        v.SetConfigFile(configFile)

        if err := v.ReadInConfig(); err != nil {
                return err
        }

        return v.Unmarshal(&Config)
}

Expected Behavior

When I pass ".env" as configFile, it works, but it doesn't when I pass ".env.test" nor ".env.dev". And the error message says file or directory named .env.test does not exist despite the file actually exists.

SetConfigFile is not clear in usage for this use case. I guess this is because viper assumes the config type as env by default.

Expected: It should be more explicit in documentation or in the actual use case. And the error message should say the correct reason of the error.

Actual Behavior

ReadInConfig doesn't read in env files other than those who has .env extension. And the error message says file or directory named .env.test does not exist despite the file actually exists.

Steps To Reproduce

  1. Use the basic example code in the viper docs
  2. Use the provided snippet to reproduce the use case
  3. Confirm that it works with .env
  4. Confirm if it works with .env.test or .env.dev

Additional Information

Documentation about func (*viper.Viper).SetConfigFile(in string) should be improved IMO.

It turned out that after adding v.SetConfigType("env") it works perfectly with all extensions, like any.

Comment From: github-actions[bot]

👋 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! ❤️

Comment From: stmcginnis

Have you tried first setting v.SetConfigType(".env")? I believe you need to tell it what format to try to read from the file.

Comment From: crazyoptimist

Setting v.SetConfigType("env") makes it work perfectly with all extensions, like any.

I already stated it in the "Additional Information" section.

This issue was about improving the documentation.

Comment From: github-actions[bot]

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