]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/go: split quotes in GOFLAGS same as in other env vars
authorRuss Cox <rsc@golang.org>
Wed, 19 Oct 2022 13:20:21 +0000 (09:20 -0400)
committerGopher Robot <gobot@golang.org>
Tue, 25 Oct 2022 16:47:27 +0000 (16:47 +0000)
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 <rsc@golang.org>
Reviewed-by: Bryan Mills <bcmills@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Auto-Submit: Russ Cox <rsc@golang.org>

src/cmd/go/internal/base/goflags.go
src/cmd/go/internal/work/exec.go
src/cmd/go/testdata/script/goflags.txt

index 267006be7a2a8646dd53add4dc9f3e59cb76aa61..eced2c5d5822682792727bb6335589ea14f88707 100644 (file)
@@ -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
@@ -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.
index fb1a9bbc14e21db8231aeeea4cb19a7f19487559..79d5615f894fc7f304298e187a693ae6ba7acf1f 100644 (file)
@@ -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)
index f4872ffd356b5b3874e1205c0959c20c79a38ca0..112086059ce0e57bdea7c08b2c0e132d4c4d7d8d 100644 (file)
@@ -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