From 6de373a2766cf6891613ba19eb8bb06227ba7273 Mon Sep 17 00:00:00 2001 From: Rebecca Stambler Date: Thu, 2 Jan 2020 12:45:15 -0500 Subject: [PATCH] internal/lsp: fix minor issues in diagnostic caching 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 TryBot-Result: Gobot Gobot Reviewed-by: Heschi Kreinick --- internal/lsp/diagnostics.go | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/internal/lsp/diagnostics.go b/internal/lsp/diagnostics.go index 283db56224c..36d4adbeea9 100644 --- a/internal/lsp/diagnostics.go +++ b/internal/lsp/diagnostics.go @@ -97,10 +97,6 @@ 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{ @@ -108,19 +104,25 @@ func (s *Server) publishReports(ctx context.Context, reports map[source.FileIden 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),