Skip to content

os/exec: environment variable de-duplication breaks old behaviour #19877

@rogpeppe

Description

@rogpeppe

go version devel +2bd6360 Wed Mar 8 03:24:44 2017 +0000 linux/amd64

The change reviewed in https://go-review.googlesource.com/c/37586/
has broken some of our tests because Go's behaviour when
importing environment variables is to use the first instance
of the variable, not the last (unlike bash, for example), so
when some code that appends a duplicate variable to the
environment and then executes a Go program, the
variable seen by the program is now the last value
where before it was the first.

To demonstrate the issue, build the following two programs:

# tst1.go
package main

import (
	"fmt"
	"os"
)

func main() {
	fmt.Printf("xxx=%s\n", os.Getenv("xxx"))
}

second program:

# tst2.go
package main

import (
	"os"
	"os/exec"
)

func main() {
	env := []string{"xxx=a", "xxx=b"}
	c := exec.Command("tst1")
	c.Env = env
	c.Stdout = os.Stdout
	c.Run()
}

Try running tst2 with different versions of Go. I'd expect to see "x=b" in both cases
but old versions of Go print "x=a" instead.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions