Skip to content

Commit

Permalink
internal/gocommand: simplify GoVersion invocation
Browse files Browse the repository at this point in the history
Detecting the go version does not require build flags,
and should not depend on the value of GO111MODULE.

For golang/go#59841.

Change-Id: I74255953985e6d2ad01095eea2b6d72f7d280e3f
Reviewed-on: https://go-review.googlesource.com/c/tools/+/489215
Run-TryBot: Bryan Mills <bcmills@google.com>
Auto-Submit: Bryan Mills <bcmills@google.com>
Reviewed-by: Heschi Kreinick <heschi@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
gopls-CI: kokoro <noreply+kokoro@google.com>
Reviewed-by: Alan Donovan <adonovan@google.com>
  • Loading branch information
Bryan C. Mills authored and gopherbot committed Apr 27, 2023
1 parent 9e0a7b1 commit ac40903
Showing 1 changed file with 4 additions and 14 deletions.
18 changes: 4 additions & 14 deletions internal/gocommand/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,11 @@ import (
func GoVersion(ctx context.Context, inv Invocation, r *Runner) (int, error) {
inv.Verb = "list"
inv.Args = []string{"-e", "-f", `{{context.ReleaseTags}}`, `--`, `unsafe`}
inv.Env = append(append([]string{}, inv.Env...), "GO111MODULE=off")
// Unset any unneeded flags, and remove them from BuildFlags, if they're
// present.
inv.ModFile = ""
inv.BuildFlags = nil // This is not a build command.
inv.ModFlag = ""
var buildFlags []string
for _, flag := range inv.BuildFlags {
// Flags can be prefixed by one or two dashes.
f := strings.TrimPrefix(strings.TrimPrefix(flag, "-"), "-")
if strings.HasPrefix(f, "mod=") || strings.HasPrefix(f, "modfile=") {
continue
}
buildFlags = append(buildFlags, flag)
}
inv.BuildFlags = buildFlags
inv.ModFile = ""
inv.Env = append(inv.Env[:len(inv.Env):len(inv.Env)], "GO111MODULE=off")

stdoutBytes, err := r.Run(ctx, inv)
if err != nil {
return 0, err
Expand Down

0 comments on commit ac40903

Please sign in to comment.