Skip to content

Commit 6652d1f

Browse files
committed
internal/gocommand: add a GoVersion function to avoid duplicating logic
It seems easier to have this as a shared internal function. Updates golang/go#41598 Change-Id: If35bdbdf5499624dd55ae9daede1ff1ae9495026 Reviewed-on: https://go-review.googlesource.com/c/tools/+/263985 Trust: Rebecca Stambler <[email protected]> Run-TryBot: Rebecca Stambler <[email protected]> gopls-CI: kokoro <[email protected]> TryBot-Result: Go Bot <[email protected]> Reviewed-by: Peter Weinberger <[email protected]> Reviewed-by: Heschi Kreinick <[email protected]>
1 parent d105bfa commit 6652d1f

File tree

4 files changed

+49
-58
lines changed

4 files changed

+49
-58
lines changed

go/packages/golist.go

Lines changed: 4 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ type golistState struct {
9191

9292
goVersionOnce sync.Once
9393
goVersionError error
94-
goVersion string // third field of 'go version'
94+
goVersion int // The X in Go 1.X.
9595

9696
// vendorDirs caches the (non)existence of vendor directories.
9797
vendorDirs map[string]bool
@@ -731,7 +731,7 @@ func (state *golistState) shouldAddFilenameFromError(p *jsonPackage) bool {
731731

732732
// On Go 1.14 and earlier, only add filenames from errors if the import stack is empty.
733733
// The import stack behaves differently for these versions than newer Go versions.
734-
if strings.HasPrefix(goV, "go1.13") || strings.HasPrefix(goV, "go1.14") {
734+
if goV < 15 {
735735
return len(p.Error.ImportStack) == 0
736736
}
737737

@@ -742,31 +742,9 @@ func (state *golistState) shouldAddFilenameFromError(p *jsonPackage) bool {
742742
return len(p.Error.ImportStack) == 0 || p.Error.ImportStack[len(p.Error.ImportStack)-1] == p.ImportPath
743743
}
744744

745-
func (state *golistState) getGoVersion() (string, error) {
745+
func (state *golistState) getGoVersion() (int, error) {
746746
state.goVersionOnce.Do(func() {
747-
var b *bytes.Buffer
748-
// Invoke go version. Don't use invokeGo because it will supply build flags, and
749-
// go version doesn't expect build flags.
750-
inv := gocommand.Invocation{
751-
Verb: "version",
752-
Env: state.cfg.Env,
753-
Logf: state.cfg.Logf,
754-
}
755-
gocmdRunner := state.cfg.gocmdRunner
756-
if gocmdRunner == nil {
757-
gocmdRunner = &gocommand.Runner{}
758-
}
759-
b, _, _, state.goVersionError = gocmdRunner.RunRaw(state.cfg.Context, inv)
760-
if state.goVersionError != nil {
761-
return
762-
}
763-
764-
sp := strings.Split(b.String(), " ")
765-
if len(sp) < 3 {
766-
state.goVersionError = fmt.Errorf("go version output: expected 'go version <version>', got '%s'", b.String())
767-
return
768-
}
769-
state.goVersion = sp[2]
747+
state.goVersion, state.goVersionError = gocommand.GoVersion(state.ctx, state.cfgInvocation(), state.cfg.gocmdRunner)
770748
})
771749
return state.goVersion, state.goVersionError
772750
}

internal/gocommand/version.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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.
4+
5+
package gocommand
6+
7+
import (
8+
"context"
9+
"fmt"
10+
"strings"
11+
)
12+
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) {
16+
inv.Verb = "list"
17+
inv.Args = []string{"-e", "-f", `{{context.ReleaseTags}}`}
18+
inv.Env = append(append([]string{}, inv.Env...), "GO111MODULE=off")
19+
// Unset any unneeded flags.
20+
inv.ModFile = ""
21+
inv.ModFlag = ""
22+
stdoutBytes, err := r.Run(ctx, inv)
23+
if err != nil {
24+
return 0, err
25+
}
26+
stdout := stdoutBytes.String()
27+
if len(stdout) < 3 {
28+
return 0, fmt.Errorf("bad ReleaseTags output: %q", stdout)
29+
}
30+
// Split up "[go1.1 go1.15]"
31+
tags := strings.Fields(stdout[1 : len(stdout)-2])
32+
for i := len(tags) - 1; i >= 0; i-- {
33+
var version int
34+
if _, err := fmt.Sscanf(tags[i], "go1.%d", &version); err != nil {
35+
continue
36+
}
37+
return version, nil
38+
}
39+
return 0, fmt.Errorf("no parseable ReleaseTags in %v", tags)
40+
}

internal/lsp/cache/session.go

Lines changed: 0 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -650,34 +650,3 @@ func (s *Session) Overlays() []source.Overlay {
650650
}
651651
return overlays
652652
}
653-
654-
// goVersion returns the Go version in use for the given session.
655-
func (s *Session) goVersion(ctx context.Context, folder string, env []string) (int, error) {
656-
// Check the go version by running "go list" with modules off.
657-
// Borrowed from internal/imports/mod.go:620.
658-
const format = `{{context.ReleaseTags}}`
659-
inv := gocommand.Invocation{
660-
Verb: "list",
661-
Args: []string{"-e", "-f", format},
662-
Env: append(env, "GO111MODULE=off"),
663-
WorkingDir: folder,
664-
}
665-
stdoutBytes, err := s.gocmdRunner.Run(ctx, inv)
666-
if err != nil {
667-
return 0, err
668-
}
669-
stdout := stdoutBytes.String()
670-
if len(stdout) < 3 {
671-
return 0, fmt.Errorf("bad ReleaseTags output: %q", stdout)
672-
}
673-
// Split up "[go1.1 go1.15]"
674-
tags := strings.Fields(stdout[1 : len(stdout)-2])
675-
for i := len(tags) - 1; i >= 0; i-- {
676-
var version int
677-
if _, err := fmt.Sscanf(tags[i], "go1.%d", &version); err != nil {
678-
continue
679-
}
680-
return version, nil
681-
}
682-
return 0, fmt.Errorf("no parseable ReleaseTags in %v", tags)
683-
}

internal/lsp/cache/view.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -631,7 +631,11 @@ func (s *Session) getWorkspaceInformation(ctx context.Context, folder span.URI,
631631
return nil, errors.Errorf("invalid workspace configuration: %w", err)
632632
}
633633
var err error
634-
goversion, err := s.goVersion(ctx, folder.Filename(), options.EnvSlice())
634+
inv := gocommand.Invocation{
635+
WorkingDir: folder.Filename(),
636+
Env: options.EnvSlice(),
637+
}
638+
goversion, err := gocommand.GoVersion(ctx, inv, s.gocmdRunner)
635639
if err != nil {
636640
return nil, err
637641
}

0 commit comments

Comments
 (0)