From 08a4f343fbad850e52bbbbace4dad0f0070fb514 Mon Sep 17 00:00:00 2001 From: Rebecca Stambler Date: Wed, 5 May 2021 19:02:45 -0400 Subject: [PATCH] internal/lsp: handle exclude directives in multi-module mode Add exclude directives to the workspace module go.mod file. Fixes golang/go#44932 Change-Id: I93f587b321dc6b35e7df30ea39cf8f70f656d04c Reviewed-on: https://go-review.googlesource.com/c/tools/+/317449 Trust: Rebecca Stambler Run-TryBot: Rebecca Stambler gopls-CI: kokoro TryBot-Result: Go Bot Reviewed-by: Robert Findley --- .../regtest/workspace/workspace_test.go | 60 +++++++++++++++++++ internal/lsp/cache/snapshot.go | 11 +++- 2 files changed, 70 insertions(+), 1 deletion(-) diff --git a/gopls/internal/regtest/workspace/workspace_test.go b/gopls/internal/regtest/workspace/workspace_test.go index 09efce3fc36..6f0e692a078 100644 --- a/gopls/internal/regtest/workspace/workspace_test.go +++ b/gopls/internal/regtest/workspace/workspace_test.go @@ -242,6 +242,66 @@ func Hello() int { }) } +func TestMultiModuleWithExclude(t *testing.T) { + testenv.NeedsGo1Point(t, 16) + + const proxy = ` +-- c.com@v1.2.3/go.mod -- +module c.com + +go 1.12 + +require b.com v1.2.3 +-- c.com@v1.2.3/blah/blah.go -- +package blah + +func SaySomething() { + fmt.Println("something") +} +-- b.com@v1.2.3/go.mod -- +module b.com + +go 1.12 +-- b.com@v1.2.4/b/b.go -- +package b + +func Hello() {} +-- b.com@v1.2.4/go.mod -- +module b.com + +go 1.12 +-- b.com@v1.2.4/b/b.go -- +package b + +func Hello() {} +` + const multiModule = ` +-- go.mod -- +module a.com + +require c.com v1.2.3 + +exclude b.com v1.2.3 +-- go.sum -- +c.com v1.2.3 h1:n07Dz9fYmpNqvZMwZi5NEqFcSHbvLa9lacMX+/g25tw= +c.com v1.2.3/go.mod h1:/4TyYgU9Nu5tA4NymP5xyqE8R2VMzGD3TbJCwCOvHAg= +-- main.go -- +package a + +func main() { + var x int +} +` + WithOptions( + ProxyFiles(proxy), + Modes(Experimental), + ).Run(t, multiModule, func(t *testing.T, env *Env) { + env.Await( + env.DiagnosticAtRegexp("main.go", "x"), + ) + }) +} + // This change tests that the version of the module used changes after it has // been deleted from the workspace. func TestDeleteModule_Interdependent(t *testing.T) { diff --git a/internal/lsp/cache/snapshot.go b/internal/lsp/cache/snapshot.go index f182ffaec3b..cdb8fb3e637 100644 --- a/internal/lsp/cache/snapshot.go +++ b/internal/lsp/cache/snapshot.go @@ -1810,7 +1810,8 @@ func buildWorkspaceModFile(ctx context.Context, modFiles map[span.URI]struct{}, // Fall back to 1.12 -- old versions insist on having some version. goVersion := "1.12" - paths := make(map[string]span.URI) + paths := map[string]span.URI{} + excludes := map[string][]string{} var sortedModURIs []span.URI for uri := range modFiles { sortedModURIs = append(sortedModURIs, uri) @@ -1853,6 +1854,9 @@ func buildWorkspaceModFile(ctx context.Context, modFiles map[span.URI]struct{}, if err := file.AddReplace(path, "", dirURI(modURI).Filename(), ""); err != nil { return nil, err } + for _, exclude := range parsed.Exclude { + excludes[exclude.Mod.Path] = append(excludes[exclude.Mod.Path], exclude.Mod.Version) + } } if goVersion != "" { file.AddGoStmt(goVersion) @@ -1896,6 +1900,11 @@ func buildWorkspaceModFile(ctx context.Context, modFiles map[span.URI]struct{}, } } } + for path, versions := range excludes { + for _, version := range versions { + file.AddExclude(path, version) + } + } file.SortBlocks() return file, nil }