mirror of
https://github.com/golang/go
synced 2024-09-30 20:18:33 -06:00
383b97c0b5
This change adds the notion of a "workspace directory", which is basically the set of directories that contains workspace packages. These are mainly used for replace targets right now. It's a little trickier than expected because the set of workspace directories can technically change on any go.mod change. At first, I wanted DidModifyFiles to report whether there was a change, but I don't think it's actually that expensive to check on each call and it complicates the code a bit. I can change it back if you think it's worth doing. The parse mod handle changes are because I needed an unlocked way of parsing the mod file, but I imagine they'll conflict with CL 244769 anyway. The next CL will be to "promote" replace targets to the level of workspace packages, meaning we will be able to find references in them. Change-Id: I5dd58fe29415473496ca6634a94a3134923228dc Reviewed-on: https://go-review.googlesource.com/c/tools/+/245327 Run-TryBot: Rebecca Stambler <rstambler@golang.org> Reviewed-by: Heschi Kreinick <heschi@google.com>
158 lines
3.5 KiB
Go
158 lines
3.5 KiB
Go
// Copyright 2020 The Go Authors. All rights reserved.
|
|
// Use of this source code is governed by a BSD-style
|
|
// license that can be found in the LICENSE file.
|
|
|
|
package regtest
|
|
|
|
import (
|
|
"fmt"
|
|
"testing"
|
|
|
|
"golang.org/x/tools/internal/lsp"
|
|
)
|
|
|
|
const workspaceProxy = `
|
|
-- example.com@v1.2.3/go.mod --
|
|
module example.com
|
|
|
|
go 1.12
|
|
-- example.com@v1.2.3/blah/blah.go --
|
|
package blah
|
|
|
|
func SaySomething() {
|
|
fmt.Println("something")
|
|
}
|
|
-- random.org@v1.2.3/go.mod --
|
|
module random.org
|
|
|
|
go 1.12
|
|
-- random.org@v1.2.3/bye/bye.go --
|
|
package bye
|
|
|
|
func Goodbye() {
|
|
println("Bye")
|
|
}
|
|
`
|
|
|
|
// TODO: Add a replace directive.
|
|
const workspaceModule = `
|
|
-- pkg/go.mod --
|
|
module mod.com
|
|
|
|
go 1.14
|
|
|
|
require (
|
|
example.com v1.2.3
|
|
random.org v1.2.3
|
|
)
|
|
-- pkg/main.go --
|
|
package main
|
|
|
|
import (
|
|
"example.com/blah"
|
|
"mod.com/inner"
|
|
"random.org/bye"
|
|
)
|
|
|
|
func main() {
|
|
blah.SaySomething()
|
|
inner.Hi()
|
|
bye.Goodbye()
|
|
}
|
|
-- pkg/main2.go --
|
|
package main
|
|
|
|
import "fmt"
|
|
|
|
func _() {
|
|
fmt.Print("%s")
|
|
}
|
|
-- pkg/inner/inner.go --
|
|
package inner
|
|
|
|
import "example.com/blah"
|
|
|
|
func Hi() {
|
|
blah.SaySomething()
|
|
}
|
|
-- goodbye/bye/bye.go --
|
|
package bye
|
|
|
|
func Bye() {}
|
|
-- goodbye/go.mod --
|
|
module random.org
|
|
|
|
go 1.12
|
|
`
|
|
|
|
// Confirm that find references returns all of the references in the module,
|
|
// regardless of what the workspace root is.
|
|
func TestReferences(t *testing.T) {
|
|
for _, tt := range []struct {
|
|
name, rootPath string
|
|
}{
|
|
{
|
|
name: "module root",
|
|
rootPath: "pkg",
|
|
},
|
|
{
|
|
name: "subdirectory",
|
|
rootPath: "pkg/inner",
|
|
},
|
|
} {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
opts := []RunOption{WithProxyFiles(workspaceProxy)}
|
|
if tt.rootPath != "" {
|
|
opts = append(opts, WithRootPath(tt.rootPath))
|
|
}
|
|
withOptions(opts...).run(t, workspaceModule, func(t *testing.T, env *Env) {
|
|
env.OpenFile("pkg/inner/inner.go")
|
|
locations := env.ReferencesAtRegexp("pkg/inner/inner.go", "SaySomething")
|
|
want := 3
|
|
if got := len(locations); got != want {
|
|
t.Fatalf("expected %v locations, got %v", want, got)
|
|
}
|
|
})
|
|
})
|
|
}
|
|
}
|
|
|
|
// Make sure that analysis diagnostics are cleared for the whole package when
|
|
// the only opened file is closed. This test was inspired by the experience in
|
|
// VS Code, where clicking on a reference result triggers a
|
|
// textDocument/didOpen without a corresponding textDocument/didClose.
|
|
func TestClearAnalysisDiagnostics(t *testing.T) {
|
|
withOptions(WithProxyFiles(workspaceProxy), WithRootPath("pkg/inner")).run(t, workspaceModule, func(t *testing.T, env *Env) {
|
|
env.OpenFile("pkg/main.go")
|
|
env.Await(
|
|
env.DiagnosticAtRegexp("pkg/main2.go", "fmt.Print"),
|
|
)
|
|
env.CloseBuffer("pkg/main.go")
|
|
env.Await(
|
|
EmptyDiagnostics("pkg/main2.go"),
|
|
)
|
|
})
|
|
}
|
|
|
|
// This test checks that gopls updates the set of files it watches when a
|
|
// replace target is added to the go.mod.
|
|
func TestWatchReplaceTargets(t *testing.T) {
|
|
withOptions(WithProxyFiles(workspaceProxy), WithRootPath("pkg")).run(t, workspaceModule, func(t *testing.T, env *Env) {
|
|
env.Await(
|
|
CompletedWork(lsp.DiagnosticWorkTitle(lsp.FromInitialWorkspaceLoad), 1),
|
|
)
|
|
// Add a replace directive and expect the files that gopls is watching
|
|
// to change.
|
|
dir := env.Sandbox.Workdir.URI("goodbye").SpanURI().Filename()
|
|
goModWithReplace := fmt.Sprintf(`%s
|
|
replace random.org => %s
|
|
`, env.ReadWorkspaceFile("pkg/go.mod"), dir)
|
|
env.WriteWorkspaceFile("pkg/go.mod", goModWithReplace)
|
|
env.Await(
|
|
CompletedWork(lsp.DiagnosticWorkTitle(lsp.FromDidChangeWatchedFiles), 1),
|
|
UnregistrationMatching("didChangeWatchedFiles"),
|
|
RegistrationMatching("didChangeWatchedFiles"),
|
|
)
|
|
})
|
|
}
|