Skip to content

Commit e9ffda4

Browse files
cmd/go: don't clobber go env GOGCCFLAGS
When CC is set in the environment, the mkEnv function sets its version of CC to the first word $CC and sets GOGCCFLAGS to the remainder. That worked since Go 1 but was broken accidentally by https://golang.org/cl/6409, which changed the code such that `go env` calls mkEnv twice. The second call to mkEnv would clobber GOGCCFLAGS based on the value of CC set by the first call. Go back to the old handling by only calling mkEnv once. Fixes #15457. Change-Id: I000a1ebcc48684667e48f2b9b24605867b9e06cd Reviewed-on: https://go-review.googlesource.com/33293 Reviewed-by: Russ Cox <rsc@golang.org>
1 parent 6f31abd commit e9ffda4

File tree

4 files changed

+11
-3
lines changed

4 files changed

+11
-3
lines changed

src/cmd/go/bug.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ func runBug(cmd *Command, args []string) {
3939
fmt.Fprint(&buf, "#### System details\n\n")
4040
fmt.Fprintln(&buf, "```")
4141
fmt.Fprintf(&buf, "go version %s %s/%s\n", runtime.Version(), runtime.GOOS, runtime.GOARCH)
42-
env := mkEnv()
42+
env := newEnv
4343
env = append(env, extraEnvVars()...)
4444
for _, e := range env {
4545
fmt.Fprintf(&buf, "%s=\"%s\"\n", e.name, e.value)

src/cmd/go/env.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ func extraEnvVars() []envVar {
104104
}
105105

106106
func runEnv(cmd *Command, args []string) {
107-
env := mkEnv()
107+
env := newEnv
108108
env = append(env, extraEnvVars()...)
109109
if len(args) > 0 {
110110
for _, name := range args {

src/cmd/go/go_test.go

+6
Original file line numberDiff line numberDiff line change
@@ -3586,6 +3586,12 @@ func TestGoEnv(t *testing.T) {
35863586
tg.setenv("CGO_CFLAGS", "-foobar")
35873587
tg.run("env", "CGO_CFLAGS")
35883588
tg.grepStdout("^-foobar$", "CGO_CFLAGS not honored")
3589+
3590+
tg.setenv("CC", "gcc -fmust -fgo -ffaster")
3591+
tg.run("env", "CC")
3592+
tg.grepStdout("gcc", "CC not found")
3593+
tg.run("env", "GOGCCFLAGS")
3594+
tg.grepStdout("-ffaster", "CC arguments not found")
35893595
}
35903596

35913597
const (

src/cmd/go/main.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ func setExitStatus(n int) {
115115
}
116116

117117
var origEnv []string
118+
var newEnv []envVar
118119

119120
func main() {
120121
_ = go11tag
@@ -164,7 +165,8 @@ func main() {
164165
// but in practice there might be skew
165166
// This makes sure we all agree.
166167
origEnv = os.Environ()
167-
for _, env := range mkEnv() {
168+
newEnv = mkEnv()
169+
for _, env := range newEnv {
168170
if os.Getenv(env.name) != env.value {
169171
os.Setenv(env.name, env.value)
170172
}

0 commit comments

Comments
 (0)