Skip to content

Commit 9ed4547

Browse files
committed
go/packages: remove -mod, -modfile flags from build flags for go version
Use provided build flags may include -mod or -modfile, and these should be removed before any go command calls that don't accept them. Fixes golang/go#43418 Change-Id: Ie96626bced5093c67fc1890533f1f8cc03d42c80 Reviewed-on: https://go-review.googlesource.com/c/tools/+/280694 Trust: Rebecca Stambler <[email protected]> Run-TryBot: Rebecca Stambler <[email protected]> gopls-CI: kokoro <[email protected]> TryBot-Result: Go Bot <[email protected]> Reviewed-by: Michael Matloob <[email protected]>
1 parent c658f99 commit 9ed4547

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed

internal/gocommand/version.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,20 @@ func GoVersion(ctx context.Context, inv Invocation, r *Runner) (int, error) {
1616
inv.Verb = "list"
1717
inv.Args = []string{"-e", "-f", `{{context.ReleaseTags}}`}
1818
inv.Env = append(append([]string{}, inv.Env...), "GO111MODULE=off")
19-
// Unset any unneeded flags.
19+
// Unset any unneeded flags, and remove them from BuildFlags, if they're
20+
// present.
2021
inv.ModFile = ""
2122
inv.ModFlag = ""
23+
var buildFlags []string
24+
for _, flag := range inv.BuildFlags {
25+
// Flags can be prefixed by one or two dashes.
26+
f := strings.TrimPrefix(strings.TrimPrefix(flag, "-"), "-")
27+
if strings.HasPrefix(f, "mod=") || strings.HasPrefix(f, "modfile=") {
28+
continue
29+
}
30+
buildFlags = append(buildFlags, flag)
31+
}
32+
inv.BuildFlags = buildFlags
2233
stdoutBytes, err := r.Run(ctx, inv)
2334
if err != nil {
2435
return 0, err

0 commit comments

Comments
 (0)