Skip to content

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

Closed

Description

@lucasvo

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
[...]

Activity

  1. lucasvo commented on Jun 2, 2018

    @lucasvo
    Author

    Related to: #307

  2. vail130 commented on Aug 17, 2018

    @vail130

    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.

  3. lucasvo commented on Aug 17, 2018

    @lucasvo
    Author

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

  4. guyisra commented on Aug 21, 2019

    @guyisra

    any updates on this?

  5. added this to the v1.16.0 milestone on Feb 25, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions