1 // Copyright 2020 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
13 // GoVersion checks the go version by running "go list" with modules off.
14 // It returns the X in Go 1.X.
15 func GoVersion(ctx context.Context, inv Invocation, r *Runner) (int, error) {
17 inv.Args = []string{"-e", "-f", `{{context.ReleaseTags}}`}
18 inv.Env = append(append([]string{}, inv.Env...), "GO111MODULE=off")
19 // Unset any unneeded flags, and remove them from BuildFlags, if they're
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=") {
30 buildFlags = append(buildFlags, flag)
32 inv.BuildFlags = buildFlags
33 stdoutBytes, err := r.Run(ctx, inv)
37 stdout := stdoutBytes.String()
39 return 0, fmt.Errorf("bad ReleaseTags output: %q", stdout)
41 // Split up "[go1.1 go1.15]"
42 tags := strings.Fields(stdout[1 : len(stdout)-2])
43 for i := len(tags) - 1; i >= 0; i-- {
45 if _, err := fmt.Sscanf(tags[i], "go1.%d", &version); err != nil {
50 return 0, fmt.Errorf("no parseable ReleaseTags in %v", tags)