Skip to content

Commit 633b092

Browse files
committed
internal/lsp: add recency check to avoid sending old diagnostics
This change uses snapshot IDs instead of file versions to determine if diagnostics are stale. Fixes golang/go#36601. Change-Id: I7727a30222fecf2d7733fa013c6cee6338ffd968 Reviewed-on: https://go-review.googlesource.com/c/tools/+/215298 Run-TryBot: Rebecca Stambler <rstambler@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Heschi Kreinick <heschi@google.com>
1 parent 351fb22 commit 633b092

File tree

1 file changed

+19
-4
lines changed

1 file changed

+19
-4
lines changed

internal/lsp/diagnostics.go

+19-4
Original file line numberDiff line numberDiff line change
@@ -113,20 +113,35 @@ func (s *Server) publishReports(ctx context.Context, snapshot source.Snapshot, r
113113
withAnalysis: withAnalysis,
114114
snapshotID: snapshot.ID(),
115115
}
116+
116117
// We use the zero values if this is an unknown file.
117118
delivered := s.delivered[fileID.URI]
118119

119-
// Reuse cached diagnostics and update the delivered map.
120-
if fileID.Version >= delivered.version && equalDiagnostics(delivered.sorted, diagnostics) {
120+
// Snapshot IDs are always increasing, so we use them instead of file
121+
// versions to create the correct order for diagnostics.
122+
123+
// If we've already delivered diagnostics for a future snapshot for this file,
124+
// do not deliver them.
125+
if delivered.snapshotID > toSend.snapshotID {
126+
// Do not update the delivered map since it already contains newer diagnostics.
127+
continue
128+
}
129+
130+
// Check if we should reuse the cached diagnostics.
131+
if equalDiagnostics(delivered.sorted, diagnostics) {
132+
// Make sure to update the delivered map.
121133
s.delivered[fileID.URI] = toSend
122134
continue
123135
}
124-
// If we've already delivered diagnostics with analyses for this file, for this snapshot,
125-
// at this version, do not send diagnostics without analyses.
136+
137+
// If we've already delivered diagnostics for this file, at this
138+
// snapshot, with analyses, do not send diagnostics without analyses.
126139
if delivered.snapshotID == toSend.snapshotID && delivered.version == toSend.version &&
127140
delivered.withAnalysis && !toSend.withAnalysis {
141+
// Do not update the delivered map since it already contains better diagnostics.
128142
continue
129143
}
144+
130145
if err := s.client.PublishDiagnostics(ctx, &protocol.PublishDiagnosticsParams{
131146
Diagnostics: toProtocolDiagnostics(diagnostics),
132147
URI: protocol.NewURI(fileID.URI),

0 commit comments

Comments
 (0)