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

Go Version

1.22

Config Source

Files

Format

Dotenv

Repl.it link

No response

Code reproducing the issue

// load the environment configuration
    viper.SetConfigFile(".env")
    if err := viper.ReadInConfig(); err != nil {
        if _, ok := err.(viper.ConfigFileNotFoundError); ok {
            // Config file not found; ignore error if desired
        } else {
            // Config file was found but another error was produced
            log.WithError(err).Error("failed to read .env config file")
        }
    }

Expected Behavior

Expecting to skip over the error if the .env file is not present and instead fallback to the ENVVARS provided to the docker container

Actual Behavior

fails with error message

Steps To Reproduce

No response

Additional Information

No response

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: spacez320

Also experiencing this as described. Happy to help contribute a fix, if there's need.

Comment From: Inmovilizame

Experiencing similar problem. In my case, the problem is even returning the appropriate error the env vars are not loaded.

type Config struct {
    DbFile            string `mapstructure:"DB_FILE"`
    LogLevel          string `mapstructure:"LOG_LEVEL"`
    Port              int    `mapstructure:"PORT"`
    JwtPrivateKey     *rsa.PrivateKey
    JwtPrivateKeyFile string `mapstructure:"JWT_PRIVATE_KEY_FILE"`
    JwtPublicKey      *rsa.PublicKey
    JwtPublicKeyFile  string `mapstructure:"JWT_PUBLIC_KEY_FILE"`
    WebKey            string `mapstructure:"WEB_KEY_FILE"`
    WebPem            string `mapstructure:"WEB_PEM_FILE"`
}

viper.SetConfigName("app")
viper.SetConfigType("env")
viper.AddConfigPath(path)


if err := viper.ReadInConfig(); err != nil {
  if _, ok := err.(viper.ConfigFileNotFoundError); !ok {
    return nil, err
  }
}

c := &Config{}
viper.Unmarshal(c)

jwtKey, err := os.ReadFile(c.JwtPrivateKeyFile)
if err != nil {
  fmt.Println("====>", c.JwtPrivateKeyFile) // At this point the var is empty even having the env var set up
  return nil, err
}

If the file exists the env vars are perfectly loaded and overwrite the values in app.env file.

The problem, IMO, lays in ReadInConfig() where not finding the config fail returns an error without taking into account viper.AutomaticEnv() setting.

Workaround is fairly simple, just providing and empty app.env file, but that is suboptimal and counterintuitive with the function description:

// AutomaticEnv makes Viper check if environment variables match any of the existing keys // (config, default or flags). If matching env vars are found, they are loaded into Viper.

Comment From: randomowo

In version v1.19.0, the feature flag viper_bind_struct is required to use struct tags; without it, keys will only be parsed from the file.

Alternatively, you can use the following approach:

for _, sf := range reflect.VisibleFields(reflect.TypeFor[Config]()) {
    tag := sf.Tag.Get("mapstructure")
    tagParts := strings.Split(tag, ",")
    if tagParts[0] != "" {
        _ = viper.BindEnv(tagParts[0])
    }
}

Comment From: spacez320

I've proposed a fix in https://github.com/spf13/viper/pull/2027