Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sub() ignores variables set with BindEnv/AutomaticEnv #507

Closed
lucasvo opened this issue Jun 2, 2018 · 4 comments · Fixed by #1056
Closed

Sub() ignores variables set with BindEnv/AutomaticEnv #507

lucasvo opened this issue Jun 2, 2018 · 4 comments · Fixed by #1056
Milestone

Comments

@lucasvo
Copy link

lucasvo commented Jun 2, 2018

The following example fails because Sub() because variables overwritten in env are ignored.

grandParent:
    parent:
        child: bar
package main

import (
	"fmt"
	"github.com/spf13/viper"
	"os"
	"strings"
)

var v *viper.Viper

func main() {
	os.Setenv("GRANDPARENT_PARENT_CHILD", "foo")
	v = viper.New()
	v.SetConfigType("yaml")
	v.AutomaticEnv()
	v.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
	f, _ := os.Open("test.yaml")
	v.ReadConfig(f)
	sub := v.Sub("grandParent.parent")
	fmt.Println("Direct", v.GetString("grandParent.parent.child"))
	fmt.Println("Sub", sub.GetString("child"))
	if sub.GetString("child") != v.GetString("grandParent.parent.child") {
		panic("sub ignores os.environ")
	}
}

The output of the program is:

Direct foo
Sub bar
panic: sub ignores os.environ
[...]
@lucasvo
Copy link
Author

lucasvo commented Jun 2, 2018

Related to: #307

@vail130
Copy link

vail130 commented Aug 17, 2018

Ran into this issue, which took a couple hours of debugging to discover. I'd love to see some explicit documentation about this limitation, if not a fix.

@lucasvo
Copy link
Author

lucasvo commented Aug 17, 2018

@spf13 would be awesome to get #508 merged! I am sure @vail130 and I are not the only ones here...

@guyisra
Copy link

guyisra commented Aug 21, 2019

any updates on this?

c2nes pushed a commit to c2nes/opencensus-service that referenced this issue Aug 28, 2019
`viper.AutomaticEnv` is enabled and environment variable overrides works
for configuration sections outside of `exporters`. However, a Viper
limitation (spf13/viper#507) prevents this
from working as expected in "sub" viper instances.

This work around allows environment variables to be used with exporter
configurations as well.
c2nes pushed a commit to c2nes/opencensus-service that referenced this issue Aug 28, 2019
`viper.AutomaticEnv` is enabled and environment variable overrides works
for configuration sections outside of `exporters`. However, a Viper
limitation (spf13/viper#507) prevents this
from working as expected in "sub" viper instances.

This work around allows environment variables to be used with exporter
configurations as well.
@sagikazarmark sagikazarmark added this to the v1.16.0 milestone Feb 25, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants