Skip to content

Commit

Permalink
internal/lsp: fix minor issues in diagnostic caching
Browse files Browse the repository at this point in the history
There were a few cases that we were missing when caching diagnostics.
The first was caching empty diagnostics from the initial workspace load,
so we were resending old empty diagnostics. The second missing case was
resending diagnostics for the same version of a file.

Fixes golang/go#36340.

Change-Id: I5c55b47a60fe94b10e76be99714cb983f9b84b54
Reviewed-on: https://go-review.googlesource.com/c/tools/+/213122
Run-TryBot: Rebecca Stambler <rstambler@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Heschi Kreinick <heschi@google.com>
  • Loading branch information
stamblerre committed Jan 2, 2020
1 parent 81ef4df commit 6de373a
Showing 1 changed file with 13 additions and 11 deletions.
24 changes: 13 additions & 11 deletions internal/lsp/diagnostics.go
Expand Up @@ -97,30 +97,32 @@ func (s *Server) publishReports(ctx context.Context, reports map[source.FileIden
if ctx.Err() != nil {
break
}
// Don't publish empty diagnostics unless specified.
if len(diagnostics) == 0 && !publishEmpty {
continue
}
// Pre-sort diagnostics to avoid extra work when we compare them.
source.SortDiagnostics(diagnostics)
toSend := sentDiagnostics{
version: fileID.Version,
identifier: fileID.Identifier,
sorted: diagnostics,
}

if delivered, ok := s.delivered[fileID.URI]; ok {
// We only reuse cached diagnostics in two cases:
// 1. This file is at a greater version than that of the previously sent diagnostics.
// 2. There are no known versions for the file.
greaterVersion := fileID.Version > delivered.version && delivered.version > 0
delivered, ok := s.delivered[fileID.URI]
// Reuse equivalent cached diagnostics for subsequent file versions (if known),
// or identical files (if versions are not known).
if ok {
geqVersion := fileID.Version >= delivered.version && delivered.version > 0
noVersions := (fileID.Version == 0 && delivered.version == 0) && delivered.identifier == fileID.Identifier
if (greaterVersion || noVersions) && equalDiagnostics(delivered.sorted, diagnostics) {
if (geqVersion || noVersions) && equalDiagnostics(delivered.sorted, diagnostics) {
// Update the delivered map even if we reuse cached diagnostics.
s.delivered[fileID.URI] = toSend
continue
}
}
// If diagnostics are empty and not previously delivered,
// only send them if we are publishing empty diagnostics.
if !ok && len(diagnostics) == 0 && !publishEmpty {
// Update the delivered map to cache the diagnostics.
s.delivered[fileID.URI] = toSend
continue
}
if err := s.client.PublishDiagnostics(ctx, &protocol.PublishDiagnosticsParams{
Diagnostics: toProtocolDiagnostics(ctx, diagnostics),
URI: protocol.NewURI(fileID.URI),
Expand Down

0 comments on commit 6de373a

Please sign in to comment.