internal/lsp/cache: fix resolution of the go directive in multi-module

workspaces

Go versions are not valid semver, so we were always resolving Go 1.12 in
the workspace module. This matters with lazy module loading.

Fixes golang/go#49668
Updates golang/go#49105
Fixes golang/go#48364

Change-Id: Iae4f5f6e17df4be1f4d6ee9d3f439efa3cf658d3
Reviewed-on: https://go-review.googlesource.com/c/tools/+/365737
Trust: Robert Findley <rfindley@google.com>
Run-TryBot: Robert Findley <rfindley@google.com>
gopls-CI: kokoro <noreply+kokoro@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Heschi Kreinick <heschi@google.com>
This commit is contained in:
Robert Findley 2021-11-19 12:47:49 -05:00
parent 43b469a3a9
commit d0c72119bb
2 changed files with 6 additions and 2 deletions

View File

@ -855,6 +855,7 @@ func TestWorkspaceDirAccess(t *testing.T) {
-- moda/a/go.mod --
module a.com
go 1.15
-- moda/a/a.go --
package main
@ -863,6 +864,8 @@ func main() {
}
-- modb/go.mod --
module b.com
go 1.16
-- modb/b/b.go --
package main
@ -892,7 +895,7 @@ func main() {
t.Fatalf("reading expected workspace modfile: %v", err)
}
got := string(gotb)
for _, want := range []string{"a.com v1.9999999.0-goplsworkspace", "b.com v1.9999999.0-goplsworkspace"} {
for _, want := range []string{"go 1.16", "a.com v1.9999999.0-goplsworkspace", "b.com v1.9999999.0-goplsworkspace"} {
if !strings.Contains(got, want) {
// want before got here, since the go.mod is multi-line
t.Fatalf("workspace go.mod missing %q. got:\n%s", want, got)

View File

@ -2290,7 +2290,8 @@ func buildWorkspaceModFile(ctx context.Context, modFiles map[span.URI]struct{},
if file == nil || parsed.Module == nil {
return nil, fmt.Errorf("no module declaration for %s", modURI)
}
if parsed.Go != nil && semver.Compare(goVersion, parsed.Go.Version) < 0 {
// Prepend "v" to go versions to make them valid semver.
if parsed.Go != nil && semver.Compare("v"+goVersion, "v"+parsed.Go.Version) < 0 {
goVersion = parsed.Go.Version
}
path := parsed.Module.Mod.Path