Skip to content

Commit a15b5d3

Browse files
author
Bryan C. Mills
committed
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 <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Jay Conrod <[email protected]>
1 parent 1b1fbb3 commit a15b5d3

File tree

3 files changed

+18
-2
lines changed

3 files changed

+18
-2
lines changed

src/cmd/go/internal/test/testflag.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,8 @@ func init() {
8888
// go test fmt -custom-flag-for-fmt-test
8989
// go test -x math
9090
func testFlags(usage func(), args []string) (packageNames, passToTest []string) {
91-
args = str.StringList(cmdflag.FindGOFLAGS(testFlagDefn), args)
91+
goflags := cmdflag.FindGOFLAGS(testFlagDefn)
92+
args = str.StringList(goflags, args)
9293
inPkg := false
9394
var explicitArgs []string
9495
for i := 0; i < len(args); i++ {
@@ -127,6 +128,9 @@ func testFlags(usage func(), args []string) (packageNames, passToTest []string)
127128
passToTest = append(passToTest, args[i])
128129
continue
129130
}
131+
if i < len(goflags) {
132+
f.Present = false // Not actually present on the command line.
133+
}
130134
if f.Value != nil {
131135
if err := f.Value.Set(value); err != nil {
132136
base.Fatalf("invalid flag argument for -%s: %v", f.Name, err)

src/cmd/go/internal/vet/vetflag.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,8 @@ func vetFlags(usage func(), args []string) (passToVet, packageNames []string) {
126126
})
127127

128128
// Process args.
129-
args = str.StringList(cmdflag.FindGOFLAGS(vetFlagDefn), args)
129+
goflags := cmdflag.FindGOFLAGS(vetFlagDefn)
130+
args = str.StringList(goflags, args)
130131
for i := 0; i < len(args); i++ {
131132
if !strings.HasPrefix(args[i], "-") {
132133
return args[:i], args[i:]
@@ -139,6 +140,9 @@ func vetFlags(usage func(), args []string) (passToVet, packageNames []string) {
139140
base.SetExitStatus(2)
140141
base.Exit()
141142
}
143+
if i < len(goflags) {
144+
f.Present = false // Not actually present on the command line.
145+
}
142146
if f.Value != nil {
143147
if err := f.Value.Set(value); err != nil {
144148
base.Fatalf("invalid flag argument for -%s: %v", f.Name, err)

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

+8
Original file line numberDiff line numberDiff line change
@@ -49,3 +49,11 @@ stderr '^go: invalid boolean value \"asdf\" for flag -e \(from (\$GOFLAGS|%GOFLA
4949
go env
5050
stdout GOFLAGS
5151

52+
# Flags listed in GOFLAGS should be safe to duplicate on the command line.
53+
env GOFLAGS=-tags=magic
54+
go list -tags=magic
55+
go test -tags=magic -c -o $devnull
56+
go vet -tags=magic
57+
58+
-- foo_test.go --
59+
package foo

0 commit comments

Comments
 (0)