Debian Codegraph @

gopls/internal/test/integration/modfile/modfile_test.go

raw symbols SCIP
1 // Copyright 2020 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 package modfile
6
7 import (
8 "os"
9 "runtime"
10 "strings"
11 "testing"
12
13 "golang.org/x/tools/gopls/internal/test/compare"
14 . "golang.org/x/tools/gopls/internal/test/integration"
15 "golang.org/x/tools/gopls/internal/util/bug"
16
17 "golang.org/x/tools/gopls/internal/protocol"
18 )
19
20 func TestMain(m *testing.M) {
21 bug.PanicOnBugs = true
22 os.Exit(Main(m))
23 }
24
25 const workspaceProxy = `
26 -- [email protected]/go.mod --
27 module example.com
28
29 go 1.12
30 -- [email protected]/blah/blah.go --
31 package blah
32
33 func SaySomething() {
34 fmt.Println("something")
35 }
36 -- [email protected]/go.mod --
37 module random.org
38
39 go 1.12
40 -- [email protected]/bye/bye.go --
41 package bye
42
43 func Goodbye() {
44 println("Bye")
45 }
46 `
47
48 const proxy = `
49 -- [email protected]/go.mod --
50 module example.com
51
52 go 1.12
53 -- [email protected]/blah/blah.go --
54 package blah
55
56 const Name = "Blah"
57 -- [email protected]/go.mod --
58 module random.org
59
60 go 1.12
61 -- [email protected]/blah/blah.go --
62 package hello
63
64 const Name = "Hello"
65 `
66
67 func TestModFileModification(t *testing.T) {
68 const untidyModule = `
69 -- a/go.mod --
70 module mod.com
71
72 -- a/main.go --
73 package main
74
75 import "example.com/blah"
76
77 func main() {
78 println(blah.Name)
79 }
80 `
81
82 runner := RunMultiple{
83 {"default", WithOptions(ProxyFiles(proxy), WorkspaceFolders("a"))},
84 {"nested", WithOptions(ProxyFiles(proxy))},
85 }
86
87 t.Run("basic", func(t *testing.T) {
88 runner.Run(t, untidyModule, func(t *testing.T, env *Env) {
89 // Open the file and make sure that the initial workspace load does not
90 // modify the go.mod file.
91 goModContent := env.ReadWorkspaceFile("a/go.mod")
92 env.OpenFile("a/main.go")
93 env.AfterChange(
94 Diagnostics(env.AtRegexp("a/main.go", "\"example.com/blah\"")),
95 )
96 if got := env.ReadWorkspaceFile("a/go.mod"); got != goModContent {
97 t.Fatalf("go.mod changed on disk:\n%s", compare.Text(goModContent, got))
98 }
99 // Save the buffer, which will format and organize imports.
100 // Confirm that the go.mod file still does not change.
101 env.SaveBuffer("a/main.go")
102 env.AfterChange(
103 Diagnostics(env.AtRegexp("a/main.go", "\"example.com/blah\"")),
104 )
105 if got := env.ReadWorkspaceFile("a/go.mod"); got != goModContent {
106 t.Fatalf("go.mod changed on disk:\n%s", compare.Text(goModContent, got))
107 }
108 })
109 })
110
111 // Reproduce golang/go#40269 by deleting and recreating main.go.
112 t.Run("delete main.go", func(t *testing.T) {
113 runner.Run(t, untidyModule, func(t *testing.T, env *Env) {
114 goModContent := env.ReadWorkspaceFile("a/go.mod")
115 mainContent := env.ReadWorkspaceFile("a/main.go")
116 env.OpenFile("a/main.go")
117 env.SaveBuffer("a/main.go")
118
119 // Ensure that we're done processing all the changes caused by opening
120 // and saving above. If not, we may run into a file locking issue on
121 // windows.
122 //
123 // If this proves insufficient, env.RemoveWorkspaceFile can be updated to
124 // retry file lock errors on windows.
125 env.AfterChange()
126 env.RemoveWorkspaceFile("a/main.go")
127
128 // TODO(rfindley): awaiting here shouldn't really be necessary. We should
129 // be consistent eventually.
130 //
131 // Probably this was meant to exercise a race with the change below.
132 env.AfterChange()
133
134 env.WriteWorkspaceFile("a/main.go", mainContent)
135 env.AfterChange(
136 Diagnostics(env.AtRegexp("a/main.go", "\"example.com/blah\"")),
137 )
138 if got := env.ReadWorkspaceFile("a/go.mod"); got != goModContent {
139 t.Fatalf("go.mod changed on disk:\n%s", compare.Text(goModContent, got))
140 }
141 })
142 })
143 }
144
145 func TestGoGetFix(t *testing.T) {
146 const mod = `
147 -- a/go.mod --
148 module mod.com
149
150 go 1.12
151
152 -- a/main.go --
153 package main
154
155 import "example.com/blah"
156
157 var _ = blah.Name
158 `
159
160 const want = `module mod.com
161
162 go 1.12
163
164 require example.com v1.2.3
165 `
166
167 RunMultiple{
168 {"default", WithOptions(ProxyFiles(proxy), WorkspaceFolders("a"))},
169 {"nested", WithOptions(ProxyFiles(proxy))},
170 }.Run(t, mod, func(t *testing.T, env *Env) {
171 if strings.Contains(t.Name(), "workspace_module") {
172 t.Skip("workspace module mode doesn't set -mod=readonly")
173 }
174 env.OpenFile("a/main.go")
175 var d protocol.PublishDiagnosticsParams
176 env.AfterChange(
177 Diagnostics(env.AtRegexp("a/main.go", `"example.com/blah"`)),
178 ReadDiagnostics("a/main.go", &d),
179 )
180 var goGetDiag protocol.Diagnostic
181 for _, diag := range d.Diagnostics {
182 if strings.Contains(diag.Message, "could not import") {
183 goGetDiag = diag
184 }
185 }
186 env.ApplyQuickFixes("a/main.go", []protocol.Diagnostic{goGetDiag})
187 if got := env.ReadWorkspaceFile("a/go.mod"); got != want {
188 t.Fatalf("unexpected go.mod content:\n%s", compare.Text(want, got))
189 }
190 })
191 }
192
193 // Tests that multiple missing dependencies gives good single fixes.
194 func TestMissingDependencyFixes(t *testing.T) {
195 const mod = `
196 -- a/go.mod --
197 module mod.com
198
199 go 1.12
200
201 -- a/main.go --
202 package main
203
204 import "example.com/blah"
205 import "random.org/blah"
206
207 var _, _ = blah.Name, hello.Name
208 `
209
210 const want = `module mod.com
211
212 go 1.12
213
214 require random.org v1.2.3
215 `
216
217 RunMultiple{
218 {"default", WithOptions(ProxyFiles(proxy), WorkspaceFolders("a"))},
219 {"nested", WithOptions(ProxyFiles(proxy))},
220 }.Run(t, mod, func(t *testing.T, env *Env) {
221 env.OpenFile("a/main.go")
222 var d protocol.PublishDiagnosticsParams
223 env.AfterChange(
224 Diagnostics(env.AtRegexp("a/main.go", `"random.org/blah"`)),
225 ReadDiagnostics("a/main.go", &d),
226 )
227 var randomDiag protocol.Diagnostic
228 for _, diag := range d.Diagnostics {
229 if strings.Contains(diag.Message, "random.org") {
230 randomDiag = diag
231 }
232 }
233 env.ApplyQuickFixes("a/main.go", []protocol.Diagnostic{randomDiag})
234 if got := env.ReadWorkspaceFile("a/go.mod"); got != want {
235 t.Fatalf("unexpected go.mod content:\n%s", compare.Text(want, got))
236 }
237 })
238 }
239
240 // Tests that multiple missing dependencies gives good single fixes.
241 func TestMissingDependencyFixesWithGoWork(t *testing.T) {
242 const mod = `
243 -- go.work --
244 go 1.18
245
246 use (
247 ./a
248 )
249 -- a/go.mod --
250 module mod.com
251
252 go 1.12
253
254 -- a/main.go --
255 package main
256
257 import "example.com/blah"
258 import "random.org/blah"
259
260 var _, _ = blah.Name, hello.Name
261 `
262
263 const want = `module mod.com
264
265 go 1.12
266
267 require random.org v1.2.3
268 `
269
270 RunMultiple{
271 {"default", WithOptions(ProxyFiles(proxy), WorkspaceFolders("a"))},
272 {"nested", WithOptions(ProxyFiles(proxy))},
273 }.Run(t, mod, func(t *testing.T, env *Env) {
274 env.OpenFile("a/main.go")
275 var d protocol.PublishDiagnosticsParams
276 env.AfterChange(
277 Diagnostics(env.AtRegexp("a/main.go", `"random.org/blah"`)),
278 ReadDiagnostics("a/main.go", &d),
279 )
280 var randomDiag protocol.Diagnostic
281 for _, diag := range d.Diagnostics {
282 if strings.Contains(diag.Message, "random.org") {
283 randomDiag = diag
284 }
285 }
286 env.ApplyQuickFixes("a/main.go", []protocol.Diagnostic{randomDiag})
287 if got := env.ReadWorkspaceFile("a/go.mod"); got != want {
288 t.Fatalf("unexpected go.mod content:\n%s", compare.Text(want, got))
289 }
290 })
291 }
292
293 func TestIndirectDependencyFix(t *testing.T) {
294 const mod = `
295 -- a/go.mod --
296 module mod.com
297
298 go 1.12
299
300 require example.com v1.2.3 // indirect
301 -- a/go.sum --
302 example.com v1.2.3 h1:ihBTGWGjTU3V4ZJ9OmHITkU9WQ4lGdQkMjgyLFk0FaY=
303 example.com v1.2.3/go.mod h1:Y2Rc5rVWjWur0h3pd9aEvK5Pof8YKDANh9gHA2Maujo=
304 -- a/main.go --
305 package main
306
307 import "example.com/blah"
308
309 func main() {
310 fmt.Println(blah.Name)
311 `
312 const want = `module mod.com
313
314 go 1.12
315
316 require example.com v1.2.3
317 `
318
319 RunMultiple{
320 {"default", WithOptions(ProxyFiles(proxy), WorkspaceFolders("a"))},
321 {"nested", WithOptions(ProxyFiles(proxy))},
322 }.Run(t, mod, func(t *testing.T, env *Env) {
323 env.OpenFile("a/go.mod")
324 var d protocol.PublishDiagnosticsParams
325 env.AfterChange(
326 Diagnostics(env.AtRegexp("a/go.mod", "// indirect")),
327 ReadDiagnostics("a/go.mod", &d),
328 )
329 env.ApplyQuickFixes("a/go.mod", d.Diagnostics)
330 if got := env.BufferText("a/go.mod"); got != want {
331 t.Fatalf("unexpected go.mod content:\n%s", compare.Text(want, got))
332 }
333 })
334 }
335
336 // Test to reproduce golang/go#39041. It adds a new require to a go.mod file
337 // that already has an unused require.
338 func TestNewDepWithUnusedDep(t *testing.T) {
339
340 const proxy = `
341 -- github.com/esimov/[email protected]/go.mod --
342 module github.com/esimov/caire
343
344 go 1.12
345 -- github.com/esimov/[email protected]/caire.go --
346 package caire
347
348 func RemoveTempImage() {}
349 -- google.golang.org/[email protected]/go.mod --
350 module google.golang.org/protobuf
351
352 go 1.12
353 -- google.golang.org/[email protected]/hello/hello.go --
354 package hello
355 `
356 const repro = `
357 -- a/go.mod --
358 module mod.com
359
360 go 1.14
361
362 require google.golang.org/protobuf v1.20.0
363 -- a/go.sum --
364 github.com/esimov/caire v1.2.5 h1:OcqDII/BYxcBYj3DuwDKjd+ANhRxRqLa2n69EGje7qw=
365 github.com/esimov/caire v1.2.5/go.mod h1:mXnjRjg3+WUtuhfSC1rKRmdZU9vJZyS1ZWU0qSvJhK8=
366 google.golang.org/protobuf v1.20.0 h1:y9T1vAtFKQg0faFNMOxJU7WuEqPWolVkjIkU6aI8qCY=
367 google.golang.org/protobuf v1.20.0/go.mod h1:FcqsytGClbtLv1ot8NvsJHjBi0h22StKVP+K/j2liKA=
368 -- a/main.go --
369 package main
370
371 import (
372 "github.com/esimov/caire"
373 )
374
375 func _() {
376 caire.RemoveTempImage()
377 }`
378
379 RunMultiple{
380 {"default", WithOptions(ProxyFiles(proxy), WorkspaceFolders("a"))},
381 {"nested", WithOptions(ProxyFiles(proxy))},
382 }.Run(t, repro, func(t *testing.T, env *Env) {
383 env.OpenFile("a/main.go")
384 var d protocol.PublishDiagnosticsParams
385 env.AfterChange(
386 Diagnostics(env.AtRegexp("a/main.go", `"github.com/esimov/caire"`)),
387 ReadDiagnostics("a/main.go", &d),
388 )
389 env.ApplyQuickFixes("a/main.go", d.Diagnostics)
390 want := `module mod.com
391
392 go 1.14
393
394 require (
395 github.com/esimov/caire v1.2.5
396 google.golang.org/protobuf v1.20.0
397 )
398 `
399 if got := env.ReadWorkspaceFile("a/go.mod"); got != want {
400 t.Fatalf("TestNewDepWithUnusedDep failed:\n%s", compare.Text(want, got))
401 }
402 })
403 }
404
405 // TODO: For this test to be effective, the sandbox's file watcher must respect
406 // the file watching GlobPattern in the capability registration. See
407 // golang/go#39384.
408 func TestModuleChangesOnDisk(t *testing.T) {
409 const mod = `
410 -- a/go.mod --
411 module mod.com
412
413 go 1.12
414
415 require example.com v1.2.3
416 -- a/go.sum --
417 example.com v1.2.3 h1:ihBTGWGjTU3V4ZJ9OmHITkU9WQ4lGdQkMjgyLFk0FaY=
418 example.com v1.2.3/go.mod h1:Y2Rc5rVWjWur0h3pd9aEvK5Pof8YKDANh9gHA2Maujo=
419 -- a/main.go --
420 package main
421
422 func main() {
423 fmt.Println(blah.Name)
424 `
425 RunMultiple{
426 {"default", WithOptions(ProxyFiles(proxy), WorkspaceFolders("a"))},
427 {"nested", WithOptions(ProxyFiles(proxy))},
428 }.Run(t, mod, func(t *testing.T, env *Env) {
429 // With zero-config gopls, we must open a/main.go to have a View including a/go.mod.
430 env.OpenFile("a/main.go")
431 env.AfterChange(
432 Diagnostics(env.AtRegexp("a/go.mod", "require")),
433 )
434 env.RunGoCommandInDir("a", "mod", "tidy")
435 env.AfterChange(
436 NoDiagnostics(ForFile("a/go.mod")),
437 )
438 })
439 }
440
441 // Tests golang/go#39784: a missing indirect dependency, necessary
442 // due to [email protected]'s incomplete go.mod file.
443 func TestBadlyVersionedModule(t *testing.T) {
444 const proxy = `
445 -- example.com/blah/@v/v1.0.0.mod --
446 module example.com
447
448 go 1.12
449 -- example.com/[email protected]/blah.go --
450 package blah
451
452 const Name = "Blah"
453 -- example.com/blah/v2/@v/v2.0.0.mod --
454 module example.com
455
456 go 1.12
457 -- example.com/blah/[email protected]/blah.go --
458 package blah
459
460 import "example.com/blah"
461
462 var V1Name = blah.Name
463 const Name = "Blah"
464 `
465 const files = `
466 -- a/go.mod --
467 module mod.com
468
469 go 1.12
470
471 require example.com/blah/v2 v2.0.0
472 -- a/go.sum --
473 example.com/blah v1.0.0 h1:kGPlWJbMsn1P31H9xp/q2mYI32cxLnCvauHN0AVaHnc=
474 example.com/blah v1.0.0/go.mod h1:PZUQaGFeVjyDmAE8ywmLbmDn3fj4Ws8epg4oLuDzW3M=
475 example.com/blah/v2 v2.0.0 h1:DNPsFPkKtTdxclRheaMCiYAoYizp6PuBzO0OmLOO0pY=
476 example.com/blah/v2 v2.0.0/go.mod h1:UZiKbTwobERo/hrqFLvIQlJwQZQGxWMVY4xere8mj7w=
477 -- a/main.go --
478 package main
479
480 import "example.com/blah/v2"
481
482 var _ = blah.Name
483 `
484 RunMultiple{
485 {"default", WithOptions(ProxyFiles(proxy), WorkspaceFolders("a"))},
486 {"nested", WithOptions(ProxyFiles(proxy))},
487 }.Run(t, files, func(t *testing.T, env *Env) {
488 env.OpenFile("a/main.go")
489 env.OpenFile("a/go.mod")
490 var modDiags protocol.PublishDiagnosticsParams
491 env.AfterChange(
492 // We would like for the error to appear in the v2 module, but
493 // as of writing non-workspace packages are not diagnosed.
494 Diagnostics(env.AtRegexp("a/main.go", `"example.com/blah/v2"`), WithMessage("no required module provides")),
495 Diagnostics(env.AtRegexp("a/go.mod", `require example.com/blah/v2`), WithMessage("no required module provides")),
496 ReadDiagnostics("a/go.mod", &modDiags),
497 )
498
499 env.ApplyQuickFixes("a/go.mod", modDiags.Diagnostics)
500 const want = `module mod.com
501
502 go 1.12
503
504 require (
505 example.com/blah v1.0.0 // indirect
506 example.com/blah/v2 v2.0.0
507 )
508 `
509 env.SaveBuffer("a/go.mod")
510 env.AfterChange(NoDiagnostics(ForFile("a/main.go")))
511 if got := env.BufferText("a/go.mod"); got != want {
512 t.Fatalf("suggested fixes failed:\n%s", compare.Text(want, got))
513 }
514 })
515 }
516
517 // Reproduces golang/go#38232.
518 func TestUnknownRevision(t *testing.T) {
519 if runtime.GOOS == "plan9" {
520 t.Skipf("skipping test that fails for unknown reasons on plan9; see https://go.dev/issue/50477")
521 }
522 const unknown = `
523 -- a/go.mod --
524 module mod.com
525
526 require (
527 example.com v1.2.2
528 )
529 -- a/main.go --
530 package main
531
532 import "example.com/blah"
533
534 func main() {
535 var x = blah.Name
536 }
537 `
538
539 runner := RunMultiple{
540 {"default", WithOptions(ProxyFiles(proxy), WorkspaceFolders("a"))},
541 {"nested", WithOptions(ProxyFiles(proxy))},
542 }
543 // Start from a bad state/bad IWL, and confirm that we recover.
544 t.Run("bad", func(t *testing.T) {
545 runner.Run(t, unknown, func(t *testing.T, env *Env) {
546 env.OpenFile("a/go.mod")
547 env.AfterChange(
548 Diagnostics(env.AtRegexp("a/go.mod", "example.com v1.2.2")),
549 )
550 env.RegexpReplace("a/go.mod", "v1.2.2", "v1.2.3")
551 env.SaveBuffer("a/go.mod") // Save to trigger diagnostics.
552
553 d := protocol.PublishDiagnosticsParams{}
554 env.AfterChange(
555 // Make sure the diagnostic mentions the new version -- the old diagnostic is in the same place.
556 Diagnostics(env.AtRegexp("a/go.mod", "example.com v1.2.3"), WithMessage("[email protected]")),
557 ReadDiagnostics("a/go.mod", &d),
558 )
559 qfs := env.GetQuickFixes("a/go.mod", d.Diagnostics)
560 if len(qfs) == 0 {
561 t.Fatalf("got 0 code actions to fix %v, wanted at least 1", d.Diagnostics)
562 }
563 env.ApplyCodeAction(qfs[0]) // Arbitrarily pick a single fix to apply. Applying all of them seems to cause trouble in this particular test.
564 env.SaveBuffer("a/go.mod") // Save to trigger diagnostics.
565 env.AfterChange(
566 NoDiagnostics(ForFile("a/go.mod")),
567 Diagnostics(env.AtRegexp("a/main.go", "x = ")),
568 )
569 })
570 })
571
572 const known = `
573 -- a/go.mod --
574 module mod.com
575
576 require (
577 example.com v1.2.3
578 )
579 -- a/go.sum --
580 example.com v1.2.3 h1:ihBTGWGjTU3V4ZJ9OmHITkU9WQ4lGdQkMjgyLFk0FaY=
581 example.com v1.2.3/go.mod h1:Y2Rc5rVWjWur0h3pd9aEvK5Pof8YKDANh9gHA2Maujo=
582 -- a/main.go --
583 package main
584
585 import "example.com/blah"
586
587 func main() {
588 var x = blah.Name
589 }
590 `
591 // Start from a good state, transform to a bad state, and confirm that we
592 // still recover.
593 t.Run("good", func(t *testing.T) {
594 runner.Run(t, known, func(t *testing.T, env *Env) {
595 env.OpenFile("a/go.mod")
596 env.AfterChange(
597 Diagnostics(env.AtRegexp("a/main.go", "x = ")),
598 )
599 env.RegexpReplace("a/go.mod", "v1.2.3", "v1.2.2")
600 env.SaveBuffer("a/go.mod") // go.mod changes must be on disk
601 env.AfterChange(
602 Diagnostics(env.AtRegexp("a/go.mod", "example.com v1.2.2")),
603 )
604 env.RegexpReplace("a/go.mod", "v1.2.2", "v1.2.3")
605 env.SaveBuffer("a/go.mod") // go.mod changes must be on disk
606 env.AfterChange(
607 Diagnostics(env.AtRegexp("a/main.go", "x = ")),
608 )
609 })
610 })
611 }
612
613 // Confirm that an error in an indirect dependency of a requirement is surfaced
614 // as a diagnostic in the go.mod file.
615 func TestErrorInIndirectDependency(t *testing.T) {
616 const badProxy = `
617 -- [email protected]/go.mod --
618 module example.com
619
620 go 1.12
621
622 require random.org v1.2.3 // indirect
623 -- [email protected]/blah/blah.go --
624 package blah
625
626 const Name = "Blah"
627 -- [email protected]/go.mod --
628 module bob.org
629
630 go 1.12
631 -- [email protected]/blah/blah.go --
632 package hello
633
634 const Name = "Hello"
635 `
636 const module = `
637 -- a/go.mod --
638 module mod.com
639
640 go 1.14
641
642 require example.com v1.2.3
643 -- a/main.go --
644 package main
645
646 import "example.com/blah"
647
648 func main() {
649 println(blah.Name)
650 }
651 `
652 RunMultiple{
653 {"default", WithOptions(ProxyFiles(badProxy), WorkspaceFolders("a"))},
654 {"nested", WithOptions(ProxyFiles(badProxy))},
655 }.Run(t, module, func(t *testing.T, env *Env) {
656 env.OpenFile("a/go.mod")
657 env.AfterChange(
658 Diagnostics(env.AtRegexp("a/go.mod", "require example.com v1.2.3")),
659 )
660 })
661 }
662
663 // A copy of govim's config_set_env_goflags_mod_readonly test.
664 func TestGovimModReadonly(t *testing.T) {
665 const mod = `
666 -- go.mod --
667 module mod.com
668
669 go 1.13
670 -- main.go --
671 package main
672
673 import "example.com/blah"
674
675 func main() {
676 println(blah.Name)
677 }
678 `
679 WithOptions(
680 EnvVars{"GOFLAGS": "-mod=readonly"},
681 ProxyFiles(proxy),
682 Modes(Default),
683 ).Run(t, mod, func(t *testing.T, env *Env) {
684 env.OpenFile("main.go")
685 original := env.ReadWorkspaceFile("go.mod")
686 env.AfterChange(
687 Diagnostics(env.AtRegexp("main.go", `"example.com/blah"`)),
688 )
689 got := env.ReadWorkspaceFile("go.mod")
690 if got != original {
691 t.Fatalf("go.mod file modified:\n%s", compare.Text(original, got))
692 }
693 env.RunGoCommand("get", "example.com/[email protected]")
694 env.RunGoCommand("mod", "tidy")
695 env.AfterChange(
696 NoDiagnostics(ForFile("main.go")),
697 )
698 })
699 }
700
701 func TestMultiModuleModDiagnostics(t *testing.T) {
702 const mod = `
703 -- go.work --
704 go 1.18
705
706 use (
707 a
708 b
709 )
710 -- a/go.mod --
711 module moda.com
712
713 go 1.14
714
715 require (
716 example.com v1.2.3
717 )
718 -- a/go.sum --
719 example.com v1.2.3 h1:Yryq11hF02fEf2JlOS2eph+ICE2/ceevGV3C9dl5V/c=
720 example.com v1.2.3/go.mod h1:Y2Rc5rVWjWur0h3pd9aEvK5Pof8YKDANh9gHA2Maujo=
721 -- a/main.go --
722 package main
723
724 func main() {}
725 -- b/go.mod --
726 module modb.com
727
728 require example.com v1.2.3
729
730 go 1.14
731 -- b/main.go --
732 package main
733
734 import "example.com/blah"
735
736 func main() {
737 blah.SaySomething()
738 }
739 `
740 WithOptions(
741 ProxyFiles(workspaceProxy),
742 ).Run(t, mod, func(t *testing.T, env *Env) {
743 env.AfterChange(
744 Diagnostics(
745 env.AtRegexp("a/go.mod", "example.com v1.2.3"),
746 WithMessage("is not used"),
747 ),
748 )
749 })
750 }
751
752 func TestModTidyWithBuildTags(t *testing.T) {
753 const mod = `
754 -- go.mod --
755 module mod.com
756
757 go 1.14
758 -- main.go --
759 // +build bob
760
761 package main
762
763 import "example.com/blah"
764
765 func main() {
766 blah.SaySomething()
767 }
768 `
769 WithOptions(
770 ProxyFiles(workspaceProxy),
771 Settings{"buildFlags": []string{"-tags", "bob"}},
772 ).Run(t, mod, func(t *testing.T, env *Env) {
773 env.OnceMet(
774 InitialWorkspaceLoad,
775 Diagnostics(env.AtRegexp("main.go", `"example.com/blah"`)),
776 )
777 })
778 }
779
780 func TestModTypoDiagnostic(t *testing.T) {
781 const mod = `
782 -- go.mod --
783 module mod.com
784
785 go 1.12
786 -- main.go --
787 package main
788
789 func main() {}
790 `
791 Run(t, mod, func(t *testing.T, env *Env) {
792 env.OpenFile("go.mod")
793 env.RegexpReplace("go.mod", "module", "modul")
794 env.AfterChange(
795 Diagnostics(env.AtRegexp("go.mod", "modul")),
796 )
797 })
798 }
799
800 func TestSumUpdateFixesDiagnostics(t *testing.T) {
801 const mod = `
802 -- go.mod --
803 module mod.com
804
805 go 1.12
806
807 require (
808 example.com v1.2.3
809 )
810 -- main.go --
811 package main
812
813 import (
814 "example.com/blah"
815 )
816
817 func main() {
818 println(blah.Name)
819 }
820 `
821 WithOptions(
822 ProxyFiles(workspaceProxy),
823 ).Run(t, mod, func(t *testing.T, env *Env) {
824 d := &protocol.PublishDiagnosticsParams{}
825 env.OpenFile("go.mod")
826 env.AfterChange(
827 Diagnostics(
828 env.AtRegexp("go.mod", `example.com v1.2.3`),
829 WithMessage("go.sum is out of sync"),
830 ),
831 ReadDiagnostics("go.mod", d),
832 )
833 env.ApplyQuickFixes("go.mod", d.Diagnostics)
834 env.SaveBuffer("go.mod") // Save to trigger diagnostics.
835 env.AfterChange(
836 NoDiagnostics(ForFile("go.mod")),
837 )
838 })
839 }
840
841 // This test confirms that editing a go.mod file only causes metadata
842 // to be invalidated when it's saved.
843 func TestGoModInvalidatesOnSave(t *testing.T) {
844 const mod = `
845 -- go.mod --
846 module mod.com
847
848 go 1.12
849 -- main.go --
850 package main
851
852 func main() {
853 hello()
854 }
855 -- hello.go --
856 package main
857
858 func hello() {}
859 `
860 WithOptions(
861 // TODO(rFindley) this doesn't work in multi-module workspace mode, because
862 // it keeps around the last parsing modfile. Update this test to also
863 // exercise the workspace module.
864 Modes(Default),
865 ).Run(t, mod, func(t *testing.T, env *Env) {
866 env.OpenFile("go.mod")
867 env.Await(env.DoneWithOpen())
868 env.RegexpReplace("go.mod", "module", "modul")
869 // Confirm that we still have metadata with only on-disk edits.
870 env.OpenFile("main.go")
871 loc := env.FirstDefinition(env.RegexpSearch("main.go", "hello"))
872 if loc.URI.Base() != "hello.go" {
873 t.Fatalf("expected definition in hello.go, got %s", loc.URI)
874 }
875 // Confirm that we no longer have metadata when the file is saved.
876 env.SaveBufferWithoutActions("go.mod")
877 _, err := env.Editor.Definitions(env.Ctx, env.RegexpSearch("main.go", "hello"))
878 if err == nil {
879 t.Fatalf("expected error, got none")
880 }
881 })
882 }
883
884 func TestRemoveUnusedDependency(t *testing.T) {
885 const proxy = `
886 -- [email protected]/go.mod --
887 module hasdep.com
888
889 go 1.12
890
891 require example.com v1.2.3
892 -- [email protected]/a/a.go --
893 package a
894 -- [email protected]/go.mod --
895 module example.com
896
897 go 1.12
898 -- [email protected]/blah/blah.go --
899 package blah
900
901 const Name = "Blah"
902 -- [email protected]/go.mod --
903 module random.com
904
905 go 1.12
906 -- [email protected]/blah/blah.go --
907 package blah
908
909 const Name = "Blah"
910 `
911 t.Run("almost tidied", func(t *testing.T) {
912 const mod = `
913 -- go.mod --
914 module mod.com
915
916 go 1.12
917
918 require hasdep.com v1.2.3
919 -- main.go --
920 package main
921
922 func main() {}
923 `
924 WithOptions(
925 ProxyFiles(proxy),
926 ).Run(t, mod, func(t *testing.T, env *Env) {
927 env.OpenFile("go.mod")
928 d := &protocol.PublishDiagnosticsParams{}
929 env.AfterChange(
930 Diagnostics(env.AtRegexp("go.mod", "require hasdep.com v1.2.3")),
931 ReadDiagnostics("go.mod", d),
932 )
933 const want = `module mod.com
934
935 go 1.12
936 `
937 env.ApplyQuickFixes("go.mod", d.Diagnostics)
938 if got := env.BufferText("go.mod"); got != want {
939 t.Fatalf("unexpected content in go.mod:\n%s", compare.Text(want, got))
940 }
941 })
942 })
943
944 t.Run("not tidied", func(t *testing.T) {
945 const mod = `
946 -- go.mod --
947 module mod.com
948
949 go 1.12
950
951 require hasdep.com v1.2.3
952 require random.com v1.2.3
953 -- main.go --
954 package main
955
956 func main() {}
957 `
958 WithOptions(
959 WriteGoSum("."),
960 ProxyFiles(proxy),
961 ).Run(t, mod, func(t *testing.T, env *Env) {
962 d := &protocol.PublishDiagnosticsParams{}
963 env.OpenFile("go.mod")
964 pos := env.RegexpSearch("go.mod", "require hasdep.com v1.2.3").Range.Start
965 env.AfterChange(
966 Diagnostics(AtPosition("go.mod", pos.Line, pos.Character)),
967 ReadDiagnostics("go.mod", d),
968 )
969 const want = `module mod.com
970
971 go 1.12
972
973 require random.com v1.2.3
974 `
975 var diagnostics []protocol.Diagnostic
976 for _, d := range d.Diagnostics {
977 if d.Range.Start.Line != pos.Line {
978 continue
979 }
980 diagnostics = append(diagnostics, d)
981 }
982 env.ApplyQuickFixes("go.mod", diagnostics)
983 if got := env.BufferText("go.mod"); got != want {
984 t.Fatalf("unexpected content in go.mod:\n%s", compare.Text(want, got))
985 }
986 })
987 })
988 }
989
990 func TestSumUpdateQuickFix(t *testing.T) {
991 const mod = `
992 -- go.mod --
993 module mod.com
994
995 go 1.12
996
997 require (
998 example.com v1.2.3
999 )
1000 -- main.go --
1001 package main
1002
1003 import (
1004 "example.com/blah"
1005 )
1006
1007 func main() {
1008 blah.Hello()
1009 }
1010 `
1011 WithOptions(
1012 ProxyFiles(workspaceProxy),
1013 Modes(Default),
1014 ).Run(t, mod, func(t *testing.T, env *Env) {
1015 env.OpenFile("go.mod")
1016 params := &protocol.PublishDiagnosticsParams{}
1017 env.AfterChange(
1018 Diagnostics(
1019 env.AtRegexp("go.mod", `example.com`),
1020 WithMessage("go.sum is out of sync"),
1021 ),
1022 ReadDiagnostics("go.mod", params),
1023 )
1024 env.ApplyQuickFixes("go.mod", params.Diagnostics)
1025 const want = `example.com v1.2.3 h1:Yryq11hF02fEf2JlOS2eph+ICE2/ceevGV3C9dl5V/c=
1026 example.com v1.2.3/go.mod h1:Y2Rc5rVWjWur0h3pd9aEvK5Pof8YKDANh9gHA2Maujo=
1027 `
1028 if got := env.ReadWorkspaceFile("go.sum"); got != want {
1029 t.Fatalf("unexpected go.sum contents:\n%s", compare.Text(want, got))
1030 }
1031 })
1032 }
1033
1034 func TestDownloadDeps(t *testing.T) {
1035 const proxy = `
1036 -- [email protected]/go.mod --
1037 module example.com
1038
1039 go 1.12
1040
1041 require random.org v1.2.3
1042 -- [email protected]/blah/blah.go --
1043 package blah
1044
1045 import "random.org/bye"
1046
1047 func SaySomething() {
1048 bye.Goodbye()
1049 }
1050 -- [email protected]/go.mod --
1051 module random.org
1052
1053 go 1.12
1054 -- [email protected]/bye/bye.go --
1055 package bye
1056
1057 func Goodbye() {
1058 println("Bye")
1059 }
1060 `
1061
1062 const mod = `
1063 -- go.mod --
1064 module mod.com
1065
1066 go 1.12
1067 -- main.go --
1068 package main
1069
1070 import (
1071 "example.com/blah"
1072 )
1073
1074 func main() {
1075 blah.SaySomething()
1076 }
1077 `
1078 WithOptions(
1079 ProxyFiles(proxy),
1080 Modes(Default),
1081 ).Run(t, mod, func(t *testing.T, env *Env) {
1082 env.OpenFile("main.go")
1083 d := &protocol.PublishDiagnosticsParams{}
1084 env.AfterChange(
1085 Diagnostics(
1086 env.AtRegexp("main.go", `"example.com/blah"`),
1087 WithMessage(`could not import example.com/blah (no required module provides package "example.com/blah")`),
1088 ),
1089 ReadDiagnostics("main.go", d),
1090 )
1091 env.ApplyQuickFixes("main.go", d.Diagnostics)
1092 env.AfterChange(
1093 NoDiagnostics(ForFile("main.go")),
1094 NoDiagnostics(ForFile("go.mod")),
1095 )
1096 })
1097 }
1098
1099 func TestInvalidGoVersion(t *testing.T) {
1100 const files = `
1101 -- go.mod --
1102 module mod.com
1103
1104 go foo
1105 -- main.go --
1106 package main
1107 `
1108 Run(t, files, func(t *testing.T, env *Env) {
1109 env.OnceMet(
1110 InitialWorkspaceLoad,
1111 Diagnostics(env.AtRegexp("go.mod", `go foo`), WithMessage("invalid go version")),
1112 )
1113 env.WriteWorkspaceFile("go.mod", "module mod.com \n\ngo 1.12\n")
1114 env.AfterChange(NoDiagnostics(ForFile("go.mod")))
1115 })
1116 }
1117
1118 // This is a regression test for a bug in the line-oriented implementation
1119 // of the "apply diffs" operation used by the fake editor.
1120 func TestIssue57627(t *testing.T) {
1121 const files = `
1122 -- go.work --
1123 package main
1124 `
1125 Run(t, files, func(t *testing.T, env *Env) {
1126 env.OpenFile("go.work")
1127 env.SetBufferContent("go.work", "go 1.18\nuse moda/a")
1128 env.SaveBuffer("go.work") // doesn't fail
1129 })
1130 }
1131
1132 func TestInconsistentMod(t *testing.T) {
1133 const proxy = `
1134 -- golang.org/x/[email protected]/go.mod --
1135 go 1.20
1136 module golang.org/x/mod
1137 -- golang.org/x/[email protected]/a.go --
1138 package mod
1139 func AutoQuote(string) string { return ""}
1140 -- golang.org/x/[email protected]/go.mod --
1141 go 1.20
1142 module golang.org/x/mod
1143 -- golang.org/x/[email protected]/a.go --
1144 package mod
1145 func AutoQuote(string) string { return ""}
1146 `
1147 const files = `
1148 -- go.work --
1149 go 1.20
1150 use (
1151 ./a
1152 ./b
1153 )
1154
1155 -- a/go.mod --
1156 module a.mod.com
1157 go 1.20
1158 require golang.org/x/mod v0.6.0 // yyy
1159 replace golang.org/x/mod v0.6.0 => golang.org/x/mod v0.7.0
1160 -- a/main.go --
1161 package main
1162 import "golang.org/x/mod"
1163 import "fmt"
1164 func main() {fmt.Println(mod.AutoQuote(""))}
1165
1166 -- b/go.mod --
1167 module b.mod.com
1168 go 1.20
1169 require golang.org/x/mod v0.9.0 // xxx
1170 -- b/main.go --
1171 package aaa
1172 import "golang.org/x/mod"
1173 import "fmt"
1174 func main() {fmt.Println(mod.AutoQuote(""))}
1175 var A int
1176
1177 -- b/c/go.mod --
1178 module c.b.mod.com
1179 go 1.20
1180 require b.mod.com v0.4.2
1181 replace b.mod.com => ../
1182 -- b/c/main.go --
1183 package main
1184 import "b.mod.com/aaa"
1185 import "fmt"
1186 func main() {fmt.Println(aaa.A)}
1187 `
1188 WithOptions(
1189 ProxyFiles(proxy),
1190 Modes(Default),
1191 ).Run(t, files, func(t *testing.T, env *Env) {
1192 env.OpenFile("a/go.mod")
1193 ahints := env.InlayHints("a/go.mod")
1194 if len(ahints) != 1 {
1195 t.Errorf("expected exactly one hint, got %d: %#v", len(ahints), ahints)
1196 }
1197 env.OpenFile("b/c/go.mod")
1198 bhints := env.InlayHints("b/c/go.mod")
1199 if len(bhints) != 0 {
1200 t.Errorf("expected no hints, got %d: %#v", len(bhints), bhints)
1201 }
1202 })
1203
1204 }