1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267
|
// Copyright 2022 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 workspace
import (
"strings"
"testing"
"golang.org/x/tools/gopls/internal/lsp"
. "golang.org/x/tools/gopls/internal/lsp/regtest"
"golang.org/x/tools/internal/testenv"
)
// This file holds various tests for UX with respect to broken workspaces.
//
// TODO: consolidate other tests here.
//
// TODO: write more tests:
// - an explicit GOWORK value that doesn't exist
// - using modules and/or GOWORK inside of GOPATH?
// Test for golang/go#53933
func TestBrokenWorkspace_DuplicateModules(t *testing.T) {
testenv.NeedsGo1Point(t, 18)
// This proxy module content is replaced by the workspace, but is still
// required for module resolution to function in the Go command.
const proxy = `
-- example.com/foo@v0.0.1/go.mod --
module example.com/foo
go 1.12
`
const src = `
-- go.work --
go 1.18
use (
./package1
./package1/vendor/example.com/foo
./package2
./package2/vendor/example.com/foo
)
-- package1/go.mod --
module mod.test
go 1.18
require example.com/foo v0.0.1
-- package1/main.go --
package main
import "example.com/foo"
func main() {
_ = foo.CompleteMe
}
-- package1/vendor/example.com/foo/go.mod --
module example.com/foo
go 1.18
-- package1/vendor/example.com/foo/foo.go --
package foo
const CompleteMe = 111
-- package2/go.mod --
module mod2.test
go 1.18
require example.com/foo v0.0.1
-- package2/main.go --
package main
import "example.com/foo"
func main() {
_ = foo.CompleteMe
}
-- package2/vendor/example.com/foo/go.mod --
module example.com/foo
go 1.18
-- package2/vendor/example.com/foo/foo.go --
package foo
const CompleteMe = 222
`
WithOptions(
ProxyFiles(proxy),
).Run(t, src, func(t *testing.T, env *Env) {
env.OpenFile("package1/main.go")
env.Await(
OutstandingWork(lsp.WorkspaceLoadFailure, `found module "example.com/foo" multiple times in the workspace`),
)
// Remove the redundant vendored copy of example.com.
env.WriteWorkspaceFile("go.work", `go 1.18
use (
./package1
./package2
./package2/vendor/example.com/foo
)
`)
env.Await(NoOutstandingWork())
// Check that definitions in package1 go to the copy vendored in package2.
location, _ := env.GoToDefinition("package1/main.go", env.RegexpSearch("package1/main.go", "CompleteMe"))
const wantLocation = "package2/vendor/example.com/foo/foo.go"
if !strings.HasSuffix(location, wantLocation) {
t.Errorf("got definition of CompleteMe at %q, want %q", location, wantLocation)
}
})
}
// Test for golang/go#43186: correcting the module path should fix errors
// without restarting gopls.
func TestBrokenWorkspace_WrongModulePath(t *testing.T) {
const files = `
-- go.mod --
module mod.testx
go 1.18
-- p/internal/foo/foo.go --
package foo
const C = 1
-- p/internal/bar/bar.go --
package bar
import "mod.test/p/internal/foo"
const D = foo.C + 1
-- p/internal/bar/bar_test.go --
package bar_test
import (
"mod.test/p/internal/foo"
. "mod.test/p/internal/bar"
)
const E = D + foo.C
-- p/internal/baz/baz_test.go --
package baz_test
import (
named "mod.test/p/internal/bar"
)
const F = named.D - 3
`
Run(t, files, func(t *testing.T, env *Env) {
env.OpenFile("p/internal/bar/bar.go")
env.Await(
OnceMet(
env.DoneWithOpen(),
env.DiagnosticAtRegexp("p/internal/bar/bar.go", "\"mod.test/p/internal/foo\""),
),
)
env.OpenFile("go.mod")
env.RegexpReplace("go.mod", "mod.testx", "mod.test")
env.SaveBuffer("go.mod") // saving triggers a reload
env.Await(NoOutstandingDiagnostics())
})
}
func TestMultipleModules_Warning(t *testing.T) {
msgForVersion := func(ver int) string {
if ver >= 18 {
return `gopls was not able to find modules in your workspace.`
} else {
return `gopls requires a module at the root of your workspace.`
}
}
const modules = `
-- a/go.mod --
module a.com
go 1.12
-- a/a.go --
package a
-- a/empty.go --
// an empty file
-- b/go.mod --
module b.com
go 1.12
-- b/b.go --
package b
`
for _, go111module := range []string{"on", "auto"} {
t.Run("GO111MODULE="+go111module, func(t *testing.T) {
WithOptions(
Modes(Default),
EnvVars{"GO111MODULE": go111module},
).Run(t, modules, func(t *testing.T, env *Env) {
ver := env.GoVersion()
msg := msgForVersion(ver)
env.OpenFile("a/a.go")
env.OpenFile("a/empty.go")
env.OpenFile("b/go.mod")
env.AfterChange(
env.DiagnosticAtRegexp("a/a.go", "package a"),
env.DiagnosticAtRegexp("b/go.mod", "module b.com"),
OutstandingWork(lsp.WorkspaceLoadFailure, msg),
)
// Changing the workspace folders to the valid modules should resolve
// the workspace errors and diagnostics.
//
// TODO(rfindley): verbose work tracking doesn't follow changing the
// workspace folder, therefore we can't invoke AfterChange here.
env.ChangeWorkspaceFolders("a", "b")
env.Await(
EmptyDiagnostics("a/a.go"),
EmptyDiagnostics("b/go.mod"),
NoOutstandingWork(),
)
env.ChangeWorkspaceFolders(".")
// TODO(rfindley): when GO111MODULE=auto, we need to open or change a
// file here in order to detect a critical error. This is because gopls
// has forgotten about a/a.go, and therefore doesn't hit the heuristic
// "all packages are command-line-arguments".
//
// This is broken, and could be fixed by adjusting the heuristic to
// account for the scenario where there are *no* workspace packages, or
// (better) trying to get workspace packages for each open file. See
// also golang/go#54261.
env.OpenFile("b/b.go")
env.Await(
// TODO(rfindley): fix these missing diagnostics.
// env.DiagnosticAtRegexp("a/a.go", "package a"),
// env.DiagnosticAtRegexp("b/go.mod", "module b.com"),
env.DiagnosticAtRegexp("b/b.go", "package b"),
OutstandingWork(lsp.WorkspaceLoadFailure, msg),
)
})
})
}
// Expect no warning if GO111MODULE=auto in a directory in GOPATH.
t.Run("GOPATH_GO111MODULE_auto", func(t *testing.T) {
WithOptions(
Modes(Default),
EnvVars{"GO111MODULE": "auto"},
InGOPATH(),
).Run(t, modules, func(t *testing.T, env *Env) {
env.OpenFile("a/a.go")
env.Await(
OnceMet(
env.DoneWithOpen(),
EmptyDiagnostics("a/a.go"),
),
NoOutstandingWork(),
)
})
})
}
|