tool/gocross: don't absorb --tags flags passed to subcommand

Fixes tailscale/corp#15117

Signed-off-by: David Anderson <danderson@tailscale.com>
This commit is contained in:
David Anderson
2023-10-05 16:43:09 -07:00
committed by Dave Anderson
parent 559f560d2d
commit c761d102ea
2 changed files with 56 additions and 5 deletions
+11 -2
View File
@@ -166,7 +166,7 @@ func autoflagsForTest(argv []string, env *Environment, goroot, nativeGOOS, nativ
// commandline and environment modifications.
newArgv = append(newArgv, argv[:2]...) // Program name and `go` tool subcommand
filteredArgvPostSubcmd, originalTags := extractTags(argv[2:])
filteredArgvPostSubcmd, originalTags := extractTags(argv[1], argv[2:])
newArgv = append(newArgv, buildFlags...)
tags = append(tags, originalTags...)
@@ -201,7 +201,7 @@ func autoflagsForTest(argv []string, env *Environment, goroot, nativeGOOS, nativ
// extractTags parses out "-tags=foo,bar" (or double hyphen or "-tags",
// "foo,bar") in its various forms and returns v filtered to remove the 0, 1 or
// 2 build tag elements, then the tags parsed, split on commas ("foo", "bar").
func extractTags(v []string) (filtered, tags []string) {
func extractTags(gocmd string, v []string) (filtered, tags []string) {
for len(v) > 0 {
e := v[0]
if strings.HasPrefix(e, "--tags=") {
@@ -225,6 +225,15 @@ func extractTags(v []string) (filtered, tags []string) {
}
continue
}
if gocmd == "run" && !strings.HasPrefix(e, "-") {
// go run can include arguments to pass to the program
// being run. They all appear after the name of the
// package or Go file to run, so when we hit the first
// non-flag positional argument, stop extracting tags and
// wrap up.
filtered = append(filtered, v...)
break
}
filtered = append(filtered, e)
v = v[1:]
}