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 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327
|
// Copyright 2023 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"
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
"golang.org/x/tools/gopls/internal/cache"
"golang.org/x/tools/gopls/internal/protocol/command"
. "golang.org/x/tools/gopls/internal/test/integration"
)
func TestAddAndRemoveGoWork(t *testing.T) {
// Use a workspace with a module in the root directory to exercise the case
// where a go.work is added to the existing root directory. This verifies
// that we're detecting changes to the module source, not just the root
// directory.
const nomod = `
-- go.mod --
module a.com
go 1.16
-- main.go --
package main
func main() {}
-- b/go.mod --
module b.com
go 1.16
-- b/main.go --
package main
func main() {}
`
WithOptions(
Modes(Default),
).Run(t, nomod, func(t *testing.T, env *Env) {
env.OpenFile("main.go")
env.OpenFile("b/main.go")
summary := func(typ cache.ViewType, root, folder string) command.View {
return command.View{
Type: typ.String(),
Root: env.Sandbox.Workdir.URI(root),
Folder: env.Sandbox.Workdir.URI(folder),
}
}
checkViews := func(want ...command.View) {
got := env.Views()
if diff := cmp.Diff(want, got, cmpopts.IgnoreFields(command.View{}, "ID")); diff != "" {
t.Errorf("SummarizeViews() mismatch (-want +got):\n%s", diff)
}
}
// Zero-config gopls makes this work.
env.AfterChange(
NoDiagnostics(ForFile("main.go")),
NoDiagnostics(env.AtRegexp("b/main.go", "package (main)")),
)
checkViews(summary(cache.GoModView, ".", "."), summary(cache.GoModView, "b", "."))
env.WriteWorkspaceFile("go.work", `go 1.16
use (
.
b
)
`)
env.AfterChange(NoDiagnostics())
checkViews(summary(cache.GoWorkView, ".", "."))
// Removing the go.work file should put us back where we started.
env.RemoveWorkspaceFile("go.work")
// Again, zero-config gopls makes this work.
env.AfterChange(
NoDiagnostics(ForFile("main.go")),
NoDiagnostics(env.AtRegexp("b/main.go", "package (main)")),
)
checkViews(summary(cache.GoModView, ".", "."), summary(cache.GoModView, "b", "."))
// Close and reopen b, to ensure the views are adjusted accordingly.
env.CloseBuffer("b/main.go")
env.AfterChange()
checkViews(summary(cache.GoModView, ".", "."))
env.OpenFile("b/main.go")
env.AfterChange()
checkViews(summary(cache.GoModView, ".", "."), summary(cache.GoModView, "b", "."))
})
}
func TestOpenAndClosePorts(t *testing.T) {
// This test checks that as we open and close files requiring a different
// port, the set of Views is adjusted accordingly.
const files = `
-- go.mod --
module a.com/a
go 1.20
-- a_linux.go --
package a
-- a_darwin.go --
package a
-- a_windows.go --
package a
`
WithOptions(
EnvVars{
"GOOS": "linux", // assume that linux is the default GOOS
},
).Run(t, files, func(t *testing.T, env *Env) {
summary := func(envOverlay ...string) command.View {
return command.View{
Type: cache.GoModView.String(),
Root: env.Sandbox.Workdir.URI("."),
Folder: env.Sandbox.Workdir.URI("."),
EnvOverlay: envOverlay,
}
}
checkViews := func(want ...command.View) {
got := env.Views()
if diff := cmp.Diff(want, got, cmpopts.IgnoreFields(command.View{}, "ID")); diff != "" {
t.Errorf("SummarizeViews() mismatch (-want +got):\n%s", diff)
}
}
checkViews(summary())
env.OpenFile("a_linux.go")
checkViews(summary())
env.OpenFile("a_darwin.go")
checkViews(
summary(),
summary("GOARCH=amd64", "GOOS=darwin"),
)
env.OpenFile("a_windows.go")
checkViews(
summary(),
summary("GOARCH=amd64", "GOOS=darwin"),
summary("GOARCH=amd64", "GOOS=windows"),
)
env.CloseBuffer("a_darwin.go")
checkViews(
summary(),
summary("GOARCH=amd64", "GOOS=windows"),
)
env.CloseBuffer("a_linux.go")
checkViews(
summary(),
summary("GOARCH=amd64", "GOOS=windows"),
)
env.CloseBuffer("a_windows.go")
checkViews(summary())
})
}
func TestCriticalErrorsInOrphanedFiles(t *testing.T) {
// This test checks that as we open and close files requiring a different
// port, the set of Views is adjusted accordingly.
const files = `
-- go.mod --
modul golang.org/lsptests/broken
go 1.20
-- a.go --
package broken
const C = 0
`
Run(t, files, func(t *testing.T, env *Env) {
env.OpenFile("a.go")
env.AfterChange(
Diagnostics(env.AtRegexp("go.mod", "modul")),
Diagnostics(env.AtRegexp("a.go", "broken"), WithMessage("initialization failed")),
)
})
}
func TestGoModReplace(t *testing.T) {
// This test checks that we treat locally replaced modules as workspace
// modules, according to the "includeReplaceInWorkspace" setting.
const files = `
-- moda/go.mod --
module golang.org/a
require golang.org/b v1.2.3
replace golang.org/b => ../modb
go 1.20
-- moda/a.go --
package a
import "golang.org/b"
const A = b.B
-- modb/go.mod --
module golang.org/b
go 1.20
-- modb/b.go --
package b
const B = 1
`
for useReplace, expectation := range map[bool]Expectation{
true: FileWatchMatching("modb"),
false: NoFileWatchMatching("modb"),
} {
WithOptions(
WorkspaceFolders("moda"),
Settings{
"includeReplaceInWorkspace": useReplace,
},
).Run(t, files, func(t *testing.T, env *Env) {
env.OnceMet(
InitialWorkspaceLoad,
expectation,
)
})
}
}
func TestDisableZeroConfig(t *testing.T) {
// This test checks that we treat locally replaced modules as workspace
// modules, according to the "includeReplaceInWorkspace" setting.
const files = `
-- moda/go.mod --
module golang.org/a
go 1.20
-- moda/a.go --
package a
-- modb/go.mod --
module golang.org/b
go 1.20
-- modb/b.go --
package b
`
WithOptions(
Settings{"zeroConfig": false},
).Run(t, files, func(t *testing.T, env *Env) {
env.OpenFile("moda/a.go")
env.OpenFile("modb/b.go")
env.AfterChange()
if got := env.Views(); len(got) != 1 || got[0].Type != cache.AdHocView.String() {
t.Errorf("Views: got %v, want one adhoc view", got)
}
})
}
func TestVendorExcluded(t *testing.T) {
// Test that we don't create Views for vendored modules.
//
// We construct the vendor directory manually here, as `go mod vendor` will
// omit the go.mod file. This synthesizes the setup of Kubernetes, where the
// entire module is vendored through a symlinked directory.
const src = `
-- go.mod --
module example.com/a
go 1.18
require other.com/b v1.0.0
-- a.go --
package a
import "other.com/b"
var _ b.B
-- vendor/modules.txt --
# other.com/b v1.0.0
## explicit; go 1.14
other.com/b
-- vendor/other.com/b/go.mod --
module other.com/b
go 1.14
-- vendor/other.com/b/b.go --
package b
type B int
func _() {
var V int // unused
}
`
WithOptions(
Modes(Default),
).Run(t, src, func(t *testing.T, env *Env) {
env.OpenFile("a.go")
env.AfterChange(NoDiagnostics())
loc := env.GoToDefinition(env.RegexpSearch("a.go", `b\.(B)`))
if !strings.Contains(string(loc.URI), "/vendor/") {
t.Fatalf("Definition(b.B) = %v, want vendored location", loc.URI)
}
env.AfterChange(
Diagnostics(env.AtRegexp("vendor/other.com/b/b.go", "V"), WithMessage("not used")),
)
if views := env.Views(); len(views) != 1 {
t.Errorf("After opening /vendor/, got %d views, want 1. Views:\n%v", len(views), views)
}
})
}
|