Skip to content

Commit ef3185b

Browse files
committed
internal/lsp/cache: add a check for snapshot invariants
Check that some invariants are met when cloning the snapshot, in an attempt to catch golang/go#43347 For golang/go#43347 Change-Id: I7404509027a1b0b0085133cba4d21d1006a52a57 Reviewed-on: https://go-review.googlesource.com/c/tools/+/280698 Run-TryBot: Robert Findley <rfindley@google.com> gopls-CI: kokoro <noreply+kokoro@google.com> TryBot-Result: Go Bot <gobot@golang.org> Trust: Robert Findley <rfindley@google.com> Reviewed-by: Rebecca Stambler <rstambler@golang.org>
1 parent b841374 commit ef3185b

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

internal/lsp/cache/snapshot.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525
"golang.org/x/tools/go/packages"
2626
"golang.org/x/tools/internal/event"
2727
"golang.org/x/tools/internal/gocommand"
28+
"golang.org/x/tools/internal/lsp/debug/log"
2829
"golang.org/x/tools/internal/lsp/debug/tag"
2930
"golang.org/x/tools/internal/lsp/source"
3031
"golang.org/x/tools/internal/memoize"
@@ -1231,13 +1232,38 @@ func generationName(v *View, snapshotID uint64) string {
12311232
return fmt.Sprintf("v%v/%v", v.id, snapshotID)
12321233
}
12331234

1235+
// checkSnapshotLocked verifies that some invariants are preserved on the
1236+
// snapshot.
1237+
func checkSnapshotLocked(ctx context.Context, s *snapshot) {
1238+
// Check that every go file for a workspace package is identified as
1239+
// belonging to that workspace package.
1240+
for wsID := range s.workspacePackages {
1241+
if m, ok := s.metadata[wsID]; ok {
1242+
for _, uri := range m.goFiles {
1243+
found := false
1244+
for _, id := range s.ids[uri] {
1245+
if id == wsID {
1246+
found = true
1247+
break
1248+
}
1249+
}
1250+
if !found {
1251+
log.Error.Logf(ctx, "workspace package %v not associated with %v", wsID, uri)
1252+
}
1253+
}
1254+
}
1255+
}
1256+
}
1257+
12341258
func (s *snapshot) clone(ctx, bgCtx context.Context, changes map[span.URI]*fileChange, forceReloadMetadata bool) (*snapshot, bool) {
12351259
var vendorChanged bool
12361260
newWorkspace, workspaceChanged, workspaceReload := s.workspace.invalidate(ctx, changes)
12371261

12381262
s.mu.Lock()
12391263
defer s.mu.Unlock()
12401264

1265+
checkSnapshotLocked(ctx, s)
1266+
12411267
newGen := s.view.session.cache.store.Generation(generationName(s.view, s.id+1))
12421268
bgCtx, cancel := context.WithCancel(bgCtx)
12431269
result := &snapshot{

0 commit comments

Comments
 (0)