Skip to content

Commit ddaa25b

Browse files
rscgopherbot
authored andcommitted
cmd/go: split quotes in GOFLAGS same as in other env vars
GOFLAGS didn't split on quotes because no other env vars (such as CC, CXX, ...) did either. This kept them all consistent. CL 341936 changed everything but GOFLAGS, making them inconsistent. Split GOFLAGS the same way as the other environment variables. Fixes #26849. Change-Id: I99bb450fe30cab949da48af133b6a36ff320532f Reviewed-on: https://go-review.googlesource.com/c/go/+/443956 Run-TryBot: Russ Cox <[email protected]> Reviewed-by: Bryan Mills <[email protected]> TryBot-Result: Gopher Robot <[email protected]> Auto-Submit: Russ Cox <[email protected]>
1 parent 2d63305 commit ddaa25b

File tree

3 files changed

+22
-8
lines changed

3 files changed

+22
-8
lines changed

src/cmd/go/internal/base/goflags.go

+16-7
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"strings"
1212

1313
"cmd/go/internal/cfg"
14+
"cmd/internal/quoted"
1415
)
1516

1617
var goflags []string // cached $GOFLAGS list; can be -x or --x form
@@ -30,19 +31,27 @@ func InitGOFLAGS() {
3031
return
3132
}
3233

33-
goflags = strings.Fields(cfg.Getenv("GOFLAGS"))
34-
if len(goflags) == 0 {
35-
// nothing to do; avoid work on later InitGOFLAGS call
36-
goflags = []string{}
37-
return
38-
}
39-
4034
// Ignore bad flag in go env and go bug, because
4135
// they are what people reach for when debugging
4236
// a problem, and maybe they're debugging GOFLAGS.
4337
// (Both will show the GOFLAGS setting if let succeed.)
4438
hideErrors := cfg.CmdName == "env" || cfg.CmdName == "bug"
4539

40+
var err error
41+
goflags, err = quoted.Split(cfg.Getenv("GOFLAGS"))
42+
if err != nil {
43+
if hideErrors {
44+
return
45+
}
46+
Fatalf("go: parsing $GOFLAGS: %v", err)
47+
}
48+
49+
if len(goflags) == 0 {
50+
// nothing to do; avoid work on later InitGOFLAGS call
51+
goflags = []string{}
52+
return
53+
}
54+
4655
// Each of the words returned by strings.Fields must be its own flag.
4756
// To set flag arguments use -x=value instead of -x value.
4857
// For boolean flags, -x is fine instead of -x=true.

src/cmd/go/internal/work/exec.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -2822,7 +2822,7 @@ func (b *Builder) gccArchArgs() []string {
28222822
// into fields, using the default value when the variable is empty.
28232823
//
28242824
// The environment variable must be quoted correctly for
2825-
// str.SplitQuotedFields. This should be done before building
2825+
// quoted.Split. This should be done before building
28262826
// anything, for example, in BuildInit.
28272827
func envList(key, def string) []string {
28282828
v := cfg.Getenv(key)

src/cmd/go/testdata/script/goflags.txt

+5
Original file line numberDiff line numberDiff line change
@@ -55,5 +55,10 @@ go list -tags=magic
5555
go test -tags=magic -c -o $devnull
5656
go vet -tags=magic
5757

58+
# GOFLAGS uses the same quoting rules (quoted.Split) as the rest of
59+
# the go command env variables
60+
env GOFLAGS='"-tags=magic wizardry"'
61+
go list
62+
5863
-- foo_test.go --
5964
package foo

0 commit comments

Comments
 (0)