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 <rstambler@golang.org>
Run-TryBot: Rebecca Stambler <rstambler@golang.org>
gopls-CI: kokoro <noreply+kokoro@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Robert Findley <rfindley@google.com>
This commit is contained in:
Rebecca Stambler 2021-05-05 19:02:45 -04:00
parent f4a41298b9
commit 08a4f343fb
2 changed files with 70 additions and 1 deletions

View File

@ -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) {

View File

@ -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
}