Debian Codegraph @

gopls/internal/test/integration/diagnostics/repro64235_test.go

raw symbols SCIP
1 // Copyright 2026 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 diagnostics
6
7 import (
8 "os"
9 "testing"
10 "time"
11
12 "golang.org/x/tools/gopls/internal/protocol"
13 "golang.org/x/tools/gopls/internal/server"
14 . "golang.org/x/tools/gopls/internal/test/integration"
15 )
16
17 // TestIssue64235 deterministically reproduces the importPackage
18 // "package name is %q, want %q" bug.Errorf reported by telemetry in
19 // golang/go#64235 (the dominant pkg.Name()=="" bucket).
20 //
21 // Mechanism: the snapshot caches file content, but go list reads disk
22 // directly. If a closed file's on-disk content diverges from the
23 // cached content (a missed or delayed file-watcher event), two
24 // consecutive loads can produce inconsistent metadata: the first
25 // installs mp_a.Name="" (and type-checks a from cached source,
26 // caching export data with manifest name "a"); the second, after a is
27 // restored on disk, gives a fresh importer b an edge to a while a's
28 // own update is discarded by the existing-metadata filter in
29 // Snapshot.load. importPackage(a) then reads the cached export data
30 // and observes the name mismatch.
31 //
32 // Within a single load, the guards in load.go (the existing-metadata
33 // filter and the imported.Name=="" edge drop) prevent this state; it
34 // requires two loads observing different disk states.
35 //
36 // See golang/go#NNNNN for the proposed fix direction (recover from
37 // snapshot/disk incoherence rather than patching this symptom).
38 func TestIssue64235(t *testing.T) {
39 t.Skip("golang/go#64235: deterministic repro of a known coherency bug; unskip when fixed")
40
41 const aOriginal = "package a\n\ntype T int\n"
42 const files = `
43 -- go.mod --
44 module mod.com
45
46 go 1.21
47 -- a/a.go --
48 ` + aOriginal + `
49 -- b/b.go --
50 package b
51
52 var V int
53 `
54 Run(t, files, func(t *testing.T, env *Env) {
55 env.OpenFile("b/b.go")
56 env.AfterChange(NoDiagnostics())
57
58 aPath := env.Sandbox.Workdir.AbsPath("a/a.go")
59 gomodPath := env.Sandbox.Workdir.AbsPath("go.mod")
60
61 // --- Phase 1: install mp_a.Name="" with cached a.go intact ---
62 // Truncate a/a.go on disk WITHOUT notifying gopls (simulating
63 // a missed file-watcher event), then trigger reinit via go.mod.
64 // go list sees an empty a.go and returns Name=""; go/packages
65 // falls back CompiledGoFiles=GoFiles, so a is still
66 // type-checked, but from the cached "package a" source.
67 // storePackageResults caches export data with manifest name
68 // "a" under ph_a.key (which incorporates Name="").
69 if err := os.WriteFile(aPath, nil, 0644); err != nil {
70 t.Fatal(err)
71 }
72 gomod, _ := os.ReadFile(gomodPath)
73 if err := os.WriteFile(gomodPath, append(gomod, []byte("\n// touched\n")...), 0644); err != nil {
74 t.Fatal(err)
75 }
76 if err := env.Editor.Server.DidChangeWatchedFiles(env.Ctx, &protocol.DidChangeWatchedFilesParams{
77 Changes: []protocol.FileEvent{{URI: env.Sandbox.Workdir.URI("go.mod"), Type: protocol.Changed}},
78 }); err != nil {
79 t.Fatal(err)
80 }
81 env.Await(CompletedWork(server.DiagnosticWorkTitle(server.FromDidChangeWatchedFiles), 1, true))
82
83 // storePackageResults runs asynchronously. In practice phase
84 // 2's own go-list latency provides the necessary wait (0ms
85 // passed 10/10 in testing), but a small margin avoids flakes
86 // on slow filesystems.
87 // TODO(rfindley): replace this sleep with a hook.
88 time.Sleep(100 * time.Millisecond)
89
90 // --- Phase 2: restore a on disk; b adds import "a" ---
91 // b is invalidated; a is not (b had no a-edge in the prior
92 // graph, so addRevDeps(b) doesn't reach a). go list for b sees
93 // the restored a (Name="a") so the b→a edge is kept, but a's
94 // fresh metadata is discarded by load.go's existing-metadata
95 // filter. Type-checking b calls importPackage(mp_a{Name=""},
96 // data{item.Name="a"}) and the bug.Errorf fires.
97 if err := os.WriteFile(aPath, []byte(aOriginal), 0644); err != nil {
98 t.Fatal(err)
99 }
100 env.SetBufferContent("b/b.go", "package b\n\nimport \"mod.com/a\"\n\nvar V a.T\n")
101
102 // The bug.Errorf panic (under PanicOnBugs) is recovered by
103 // iimportCommon's defer/recover and the resulting error is
104 // swallowed by getPackage's errgroup, so the only observable
105 // effect is the spurious "could not import" diagnostic on b.
106 // In this test that diagnostic can only arise from the
107 // importPackage failure: a is a valid package and b's import
108 // is well-formed.
109 //
110 // Once golang/go#64235 is fixed there should be no diagnostic
111 // at all here, so flip this assertion to NoDiagnostics().
112 env.AfterChange(
113 Diagnostics(env.AtRegexp("b/b.go", `"mod.com/a"`), WithMessage("could not import mod.com/a")),
114 )
115 })
116 }