Skip to content

Commit 419a6c0

Browse files
cmd/go: pass an unmodified environment to a go run program
Fixes #11709. Fixed #11449. Change-Id: If8fdb27d3dc25fb7017226d143a29cbebc1374c5 Reviewed-on: https://go-review.googlesource.com/12483 Reviewed-by: David Crawshaw <[email protected]> Run-TryBot: Ian Lance Taylor <[email protected]> TryBot-Result: Gobot Gobot <[email protected]>
1 parent 0505dfc commit 419a6c0

File tree

4 files changed

+20
-1
lines changed

4 files changed

+20
-1
lines changed

src/cmd/go/generate.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -402,7 +402,7 @@ func (g *Generator) exec(words []string) {
402402
"GOFILE=" + g.file,
403403
"GOPACKAGE=" + g.pkg,
404404
}
405-
cmd.Env = mergeEnvLists(env, os.Environ())
405+
cmd.Env = mergeEnvLists(env, origEnv)
406406
err := cmd.Run()
407407
if err != nil {
408408
g.errorf("running %q: %s", words[0], err)

src/cmd/go/go_test.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2229,3 +2229,18 @@ func TestGoInstallShadowedGOPATH(t *testing.T) {
22292229
tg.runFail("install")
22302230
tg.grepStderr("no install location for.*gopath2.src.test: hidden by .*gopath1.src.test", "missing error")
22312231
}
2232+
2233+
func TestIssue11709(t *testing.T) {
2234+
tg := testgo(t)
2235+
defer tg.cleanup()
2236+
tg.tempFile("run.go", `
2237+
package main
2238+
import "os"
2239+
func main() {
2240+
if os.Getenv("TERM") != "" {
2241+
os.Exit(1)
2242+
}
2243+
}`)
2244+
tg.unsetenv("TERM")
2245+
tg.run("run", tg.path("run.go"))
2246+
}

src/cmd/go/main.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,8 @@ func setExitStatus(n int) {
112112
exitMu.Unlock()
113113
}
114114

115+
var origEnv []string
116+
115117
func main() {
116118
_ = go11tag
117119
flag.Usage = usage
@@ -159,6 +161,7 @@ func main() {
159161
// the same default computation of these as we do,
160162
// but in practice there might be skew
161163
// This makes sure we all agree.
164+
origEnv = os.Environ()
162165
for _, env := range mkEnv() {
163166
if os.Getenv(env.name) != env.value {
164167
os.Setenv(env.name, env.value)

src/cmd/go/run.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ func runStdin(cmdline []string) {
137137
cmd.Stdin = os.Stdin
138138
cmd.Stdout = os.Stdout
139139
cmd.Stderr = os.Stderr
140+
cmd.Env = origEnv
140141
startSigHandlers()
141142
if err := cmd.Run(); err != nil {
142143
errorf("%v", err)

0 commit comments

Comments
 (0)