Skip to content

Commit

Permalink
internal/lsp: handle exclude directives in multi-module mode
Browse files Browse the repository at this point in the history
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 <[email protected]>
Run-TryBot: Rebecca Stambler <[email protected]>
gopls-CI: kokoro <[email protected]>
TryBot-Result: Go Bot <[email protected]>
Reviewed-by: Robert Findley <[email protected]>
  • Loading branch information
stamblerre committed May 6, 2021
1 parent f4a4129 commit 08a4f34
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 1 deletion.
60 changes: 60 additions & 0 deletions gopls/internal/regtest/workspace/workspace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,66 @@ func Hello() int {
})
}

func TestMultiModuleWithExclude(t *testing.T) {
testenv.NeedsGo1Point(t, 16)

const proxy = `
-- [email protected]/go.mod --
module c.com
go 1.12
require b.com v1.2.3
-- [email protected]/blah/blah.go --
package blah
func SaySomething() {
fmt.Println("something")
}
-- [email protected]/go.mod --
module b.com
go 1.12
-- [email protected]/b/b.go --
package b
func Hello() {}
-- [email protected]/go.mod --
module b.com
go 1.12
-- [email protected]/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) {
Expand Down
11 changes: 10 additions & 1 deletion internal/lsp/cache/snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
}
Expand Down

0 comments on commit 08a4f34

Please sign in to comment.