From a15b5d30925e7be1101d812311545afb82c45a68 Mon Sep 17 00:00:00 2001 From: "Bryan C. Mills" Date: Fri, 6 Dec 2019 13:43:23 -0500 Subject: [PATCH] cmd/go: allow arguments to 'go test' and 'go vet' to duplicate or override flags from GOFLAGS This is a minimal fix for Go 1.14, but this parsing logic is much too complex and seems like it will cause more trouble going forward. I intend to mail a followup change to refactor this logic for 1.15. Updates #32471 Change-Id: I00ed07dcf3a23c9cd4ffa8cf764921fb5c18bcd6 Reviewed-on: https://go-review.googlesource.com/c/go/+/210940 Run-TryBot: Bryan C. Mills TryBot-Result: Gobot Gobot Reviewed-by: Jay Conrod --- src/cmd/go/internal/test/testflag.go | 6 +++++- src/cmd/go/internal/vet/vetflag.go | 6 +++++- src/cmd/go/testdata/script/goflags.txt | 8 ++++++++ 3 files changed, 18 insertions(+), 2 deletions(-) diff --git a/src/cmd/go/internal/test/testflag.go b/src/cmd/go/internal/test/testflag.go index 79dc5eb2a0c..e214b1532bd 100644 --- a/src/cmd/go/internal/test/testflag.go +++ b/src/cmd/go/internal/test/testflag.go @@ -88,7 +88,8 @@ func init() { // go test fmt -custom-flag-for-fmt-test // go test -x math func testFlags(usage func(), args []string) (packageNames, passToTest []string) { - args = str.StringList(cmdflag.FindGOFLAGS(testFlagDefn), args) + goflags := cmdflag.FindGOFLAGS(testFlagDefn) + args = str.StringList(goflags, args) inPkg := false var explicitArgs []string for i := 0; i < len(args); i++ { @@ -127,6 +128,9 @@ func testFlags(usage func(), args []string) (packageNames, passToTest []string) passToTest = append(passToTest, args[i]) continue } + if i < len(goflags) { + f.Present = false // Not actually present on the command line. + } if f.Value != nil { if err := f.Value.Set(value); err != nil { base.Fatalf("invalid flag argument for -%s: %v", f.Name, err) diff --git a/src/cmd/go/internal/vet/vetflag.go b/src/cmd/go/internal/vet/vetflag.go index 7179f73cfcd..e3de48bbffa 100644 --- a/src/cmd/go/internal/vet/vetflag.go +++ b/src/cmd/go/internal/vet/vetflag.go @@ -126,7 +126,8 @@ func vetFlags(usage func(), args []string) (passToVet, packageNames []string) { }) // Process args. - args = str.StringList(cmdflag.FindGOFLAGS(vetFlagDefn), args) + goflags := cmdflag.FindGOFLAGS(vetFlagDefn) + args = str.StringList(goflags, args) for i := 0; i < len(args); i++ { if !strings.HasPrefix(args[i], "-") { return args[:i], args[i:] @@ -139,6 +140,9 @@ func vetFlags(usage func(), args []string) (passToVet, packageNames []string) { base.SetExitStatus(2) base.Exit() } + if i < len(goflags) { + f.Present = false // Not actually present on the command line. + } if f.Value != nil { if err := f.Value.Set(value); err != nil { base.Fatalf("invalid flag argument for -%s: %v", f.Name, err) diff --git a/src/cmd/go/testdata/script/goflags.txt b/src/cmd/go/testdata/script/goflags.txt index fac6d807202..686d1138b83 100644 --- a/src/cmd/go/testdata/script/goflags.txt +++ b/src/cmd/go/testdata/script/goflags.txt @@ -49,3 +49,11 @@ stderr '^go: invalid boolean value \"asdf\" for flag -e \(from (\$GOFLAGS|%GOFLA go env stdout GOFLAGS +# Flags listed in GOFLAGS should be safe to duplicate on the command line. +env GOFLAGS=-tags=magic +go list -tags=magic +go test -tags=magic -c -o $devnull +go vet -tags=magic + +-- foo_test.go -- +package foo