Skip to content

Commit

Permalink
cmd/go: split quotes in GOFLAGS same as in other env vars
Browse files Browse the repository at this point in the history
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 golang#26849.

Change-Id: I99bb450fe30cab949da48af133b6a36ff320532f
Reviewed-on: https://go-review.googlesource.com/c/go/+/443956
Run-TryBot: Russ Cox <rsc@golang.org>
Reviewed-by: Bryan Mills <bcmills@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Auto-Submit: Russ Cox <rsc@golang.org>
  • Loading branch information
rsc authored and romaindoumenc committed Nov 3, 2022
1 parent ebb0b87 commit c47e72d
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 8 deletions.
23 changes: 16 additions & 7 deletions src/cmd/go/internal/base/goflags.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"strings"

"cmd/go/internal/cfg"
"cmd/internal/quoted"
)

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

goflags = strings.Fields(cfg.Getenv("GOFLAGS"))
if len(goflags) == 0 {
// nothing to do; avoid work on later InitGOFLAGS call
goflags = []string{}
return
}

// Ignore bad flag in go env and go bug, because
// they are what people reach for when debugging
// a problem, and maybe they're debugging GOFLAGS.
// (Both will show the GOFLAGS setting if let succeed.)
hideErrors := cfg.CmdName == "env" || cfg.CmdName == "bug"

var err error
goflags, err = quoted.Split(cfg.Getenv("GOFLAGS"))
if err != nil {
if hideErrors {
return
}
Fatalf("go: parsing $GOFLAGS: %v", err)
}

if len(goflags) == 0 {
// nothing to do; avoid work on later InitGOFLAGS call
goflags = []string{}
return
}

// Each of the words returned by strings.Fields must be its own flag.
// To set flag arguments use -x=value instead of -x value.
// For boolean flags, -x is fine instead of -x=true.
Expand Down
2 changes: 1 addition & 1 deletion src/cmd/go/internal/work/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -2822,7 +2822,7 @@ func (b *Builder) gccArchArgs() []string {
// into fields, using the default value when the variable is empty.
//
// The environment variable must be quoted correctly for
// str.SplitQuotedFields. This should be done before building
// quoted.Split. This should be done before building
// anything, for example, in BuildInit.
func envList(key, def string) []string {
v := cfg.Getenv(key)
Expand Down
5 changes: 5 additions & 0 deletions src/cmd/go/testdata/script/goflags.txt
Original file line number Diff line number Diff line change
Expand Up @@ -55,5 +55,10 @@ go list -tags=magic
go test -tags=magic -c -o $devnull
go vet -tags=magic

# GOFLAGS uses the same quoting rules (quoted.Split) as the rest of
# the go command env variables
env GOFLAGS='"-tags=magic wizardry"'
go list

-- foo_test.go --
package foo

0 comments on commit c47e72d

Please sign in to comment.