| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
package workspace |
| 6 |
|
| 7 |
import ( |
| 8 |
"strings" |
| 9 |
"testing" |
| 10 |
|
| 11 |
"golang.org/x/tools/gopls/internal/server" |
| 12 |
. "golang.org/x/tools/gopls/internal/test/integration" |
| 13 |
) |
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
func TestBrokenWorkspace_DuplicateModules(t *testing.T) { |
| 25 |
|
| 26 |
|
| 27 |
const proxy = ` |
| 28 |
-- example.com/[email protected]/go.mod -- |
| 29 |
module example.com/foo |
| 30 |
|
| 31 |
go 1.12 |
| 32 |
` |
| 33 |
|
| 34 |
const src = ` |
| 35 |
-- go.work -- |
| 36 |
go 1.18 |
| 37 |
|
| 38 |
use ( |
| 39 |
./package1 |
| 40 |
./package1/vendor/example.com/foo |
| 41 |
./package2 |
| 42 |
./package2/vendor/example.com/foo |
| 43 |
) |
| 44 |
|
| 45 |
-- package1/go.mod -- |
| 46 |
module mod.test |
| 47 |
|
| 48 |
go 1.18 |
| 49 |
|
| 50 |
require example.com/foo v0.0.1 |
| 51 |
-- package1/main.go -- |
| 52 |
package main |
| 53 |
|
| 54 |
import "example.com/foo" |
| 55 |
|
| 56 |
func main() { |
| 57 |
_ = foo.CompleteMe |
| 58 |
} |
| 59 |
-- package1/vendor/example.com/foo/go.mod -- |
| 60 |
module example.com/foo |
| 61 |
|
| 62 |
go 1.18 |
| 63 |
-- package1/vendor/example.com/foo/foo.go -- |
| 64 |
package foo |
| 65 |
|
| 66 |
const CompleteMe = 111 |
| 67 |
-- package2/go.mod -- |
| 68 |
module mod2.test |
| 69 |
|
| 70 |
go 1.18 |
| 71 |
|
| 72 |
require example.com/foo v0.0.1 |
| 73 |
-- package2/main.go -- |
| 74 |
package main |
| 75 |
|
| 76 |
import "example.com/foo" |
| 77 |
|
| 78 |
func main() { |
| 79 |
_ = foo.CompleteMe |
| 80 |
} |
| 81 |
-- package2/vendor/example.com/foo/go.mod -- |
| 82 |
module example.com/foo |
| 83 |
|
| 84 |
go 1.18 |
| 85 |
-- package2/vendor/example.com/foo/foo.go -- |
| 86 |
package foo |
| 87 |
|
| 88 |
const CompleteMe = 222 |
| 89 |
` |
| 90 |
|
| 91 |
WithOptions( |
| 92 |
ProxyFiles(proxy), |
| 93 |
).Run(t, src, func(t *testing.T, env *Env) { |
| 94 |
env.OpenFile("package1/main.go") |
| 95 |
env.AfterChange( |
| 96 |
OutstandingWork(server.WorkspaceLoadFailure, `module example.com/foo appears multiple times in workspace`), |
| 97 |
) |
| 98 |
|
| 99 |
|
| 100 |
env.WriteWorkspaceFile("go.work", `go 1.18 |
| 101 |
use ( |
| 102 |
./package1 |
| 103 |
./package2 |
| 104 |
./package2/vendor/example.com/foo |
| 105 |
) |
| 106 |
`) |
| 107 |
env.AfterChange(NoOutstandingWork(IgnoreTelemetryPromptWork)) |
| 108 |
|
| 109 |
|
| 110 |
location := string(env.FirstDefinition(env.RegexpSearch("package1/main.go", "CompleteMe")).URI) |
| 111 |
const wantLocation = "package2/vendor/example.com/foo/foo.go" |
| 112 |
if !strings.HasSuffix(location, wantLocation) { |
| 113 |
t.Errorf("got definition of CompleteMe at %q, want %q", location, wantLocation) |
| 114 |
} |
| 115 |
}) |
| 116 |
} |
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
func TestBrokenWorkspace_WrongModulePath(t *testing.T) { |
| 121 |
const files = ` |
| 122 |
-- go.mod -- |
| 123 |
module mod.testx |
| 124 |
|
| 125 |
go 1.18 |
| 126 |
-- p/internal/foo/foo.go -- |
| 127 |
package foo |
| 128 |
|
| 129 |
const C = 1 |
| 130 |
-- p/internal/bar/bar.go -- |
| 131 |
package bar |
| 132 |
|
| 133 |
import "mod.test/p/internal/foo" |
| 134 |
|
| 135 |
const D = foo.C + 1 |
| 136 |
-- p/internal/bar/bar_test.go -- |
| 137 |
package bar_test |
| 138 |
|
| 139 |
import ( |
| 140 |
"mod.test/p/internal/foo" |
| 141 |
. "mod.test/p/internal/bar" |
| 142 |
) |
| 143 |
|
| 144 |
const E = D + foo.C |
| 145 |
-- p/internal/baz/baz_test.go -- |
| 146 |
package baz_test |
| 147 |
|
| 148 |
import ( |
| 149 |
named "mod.test/p/internal/bar" |
| 150 |
) |
| 151 |
|
| 152 |
const F = named.D - 3 |
| 153 |
` |
| 154 |
|
| 155 |
Run(t, files, func(t *testing.T, env *Env) { |
| 156 |
env.OpenFile("p/internal/bar/bar.go") |
| 157 |
env.AfterChange( |
| 158 |
Diagnostics(env.AtRegexp("p/internal/bar/bar.go", "\"mod.test/p/internal/foo\"")), |
| 159 |
) |
| 160 |
env.OpenFile("go.mod") |
| 161 |
env.RegexpReplace("go.mod", "mod.testx", "mod.test") |
| 162 |
env.SaveBuffer("go.mod") |
| 163 |
env.AfterChange(NoDiagnostics()) |
| 164 |
}) |
| 165 |
} |
| 166 |
|
| 167 |
func TestMultipleModules_Warning(t *testing.T) { |
| 168 |
t.Skip("temporary skip for golang/go#57979: revisit after zero-config logic is in place") |
| 169 |
|
| 170 |
msgForVersion := func(ver int) string { |
| 171 |
if ver >= 18 { |
| 172 |
return `gopls was not able to find modules in your workspace.` |
| 173 |
} else { |
| 174 |
return `gopls requires a module at the root of your workspace.` |
| 175 |
} |
| 176 |
} |
| 177 |
|
| 178 |
const modules = ` |
| 179 |
-- a/go.mod -- |
| 180 |
module a.com |
| 181 |
|
| 182 |
go 1.12 |
| 183 |
-- a/a.go -- |
| 184 |
package a |
| 185 |
-- a/empty.go -- |
| 186 |
// an empty file |
| 187 |
-- b/go.mod -- |
| 188 |
module b.com |
| 189 |
|
| 190 |
go 1.12 |
| 191 |
-- b/b.go -- |
| 192 |
package b |
| 193 |
` |
| 194 |
for _, go111module := range []string{"on", "auto"} { |
| 195 |
t.Run("GO111MODULE="+go111module, func(t *testing.T) { |
| 196 |
WithOptions( |
| 197 |
Modes(Default), |
| 198 |
EnvVars{"GO111MODULE": go111module}, |
| 199 |
).Run(t, modules, func(t *testing.T, env *Env) { |
| 200 |
ver := env.GoVersion() |
| 201 |
msg := msgForVersion(ver) |
| 202 |
env.OpenFile("a/a.go") |
| 203 |
env.OpenFile("a/empty.go") |
| 204 |
env.OpenFile("b/go.mod") |
| 205 |
env.AfterChange( |
| 206 |
Diagnostics(env.AtRegexp("a/a.go", "package a")), |
| 207 |
Diagnostics(env.AtRegexp("b/go.mod", "module b.com")), |
| 208 |
OutstandingWork(server.WorkspaceLoadFailure, msg), |
| 209 |
) |
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
env.ChangeWorkspaceFolders("a", "b") |
| 217 |
env.Await( |
| 218 |
NoDiagnostics(ForFile("a/a.go")), |
| 219 |
NoDiagnostics(ForFile("b/go.mod")), |
| 220 |
NoOutstandingWork(IgnoreTelemetryPromptWork), |
| 221 |
) |
| 222 |
|
| 223 |
env.ChangeWorkspaceFolders(".") |
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
env.OpenFile("b/b.go") |
| 235 |
env.AfterChange( |
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
Diagnostics(env.AtRegexp("b/b.go", "package b")), |
| 240 |
OutstandingWork(server.WorkspaceLoadFailure, msg), |
| 241 |
) |
| 242 |
}) |
| 243 |
}) |
| 244 |
} |
| 245 |
|
| 246 |
|
| 247 |
t.Run("GOPATH_GO111MODULE_auto", func(t *testing.T) { |
| 248 |
WithOptions( |
| 249 |
Modes(Default), |
| 250 |
EnvVars{"GO111MODULE": "auto"}, |
| 251 |
InGOPATH(), |
| 252 |
).Run(t, modules, func(t *testing.T, env *Env) { |
| 253 |
env.OpenFile("a/a.go") |
| 254 |
env.AfterChange( |
| 255 |
NoDiagnostics(ForFile("a/a.go")), |
| 256 |
NoOutstandingWork(IgnoreTelemetryPromptWork), |
| 257 |
) |
| 258 |
}) |
| 259 |
}) |
| 260 |
} |