it looks like we have the same problem,for some project that i have sometimes it worked sometimes not ,for some my project im call the function in the root app
config/config.go
func InitConfig() {
viper.SetConfigName("config")
viper.SetConfigType("yaml")
viper.AddConfigPath(".")
err := viper.ReadInConfig()
if err != nil {
panic(fmt.Errorf("fatal error config file: %w", err))
}
replacer := strings.NewReplacer(".", "_")
viper.SetEnvKeyReplacer(replacer)
viper.AutomaticEnv()
}
cmd/root.go(or just call it in your main.go)
func initConfig(){
config.InitConfig()
}
func Execute(){
initConfig()
if err := rootCmd.Execute(); err != nil {
log.Fatal(err)
}
}