From 9e60d4e5b9fd2c12a92aca3ec394793b33acf5d4 Mon Sep 17 00:00:00 2001 From: Rohan Challa Date: Wed, 5 Feb 2020 10:18:40 -0500 Subject: [PATCH] internal/lsp: continue diagnostics if diagnosing go.mod fails This change ensures that diagnostics for .go files get calculated and published regardless of whether or not the go.mod file diagnostics fail. This change also updates lsp_test.SuggestedFix to make use of the snapshot.Diagnose function vs the source.FileDiagnostics. Change-Id: I54c35106b1701b54cca9a413edfb253f3c4c5ab7 Reviewed-on: https://go-review.googlesource.com/c/tools/+/217839 Run-TryBot: Rohan Challa TryBot-Result: Gobot Gobot Reviewed-by: Rebecca Stambler --- internal/lsp/diagnostics.go | 1 - internal/lsp/lsp_test.go | 19 +++++++++++++------ 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/internal/lsp/diagnostics.go b/internal/lsp/diagnostics.go index b488f188e7..fcc777c1c7 100644 --- a/internal/lsp/diagnostics.go +++ b/internal/lsp/diagnostics.go @@ -55,7 +55,6 @@ func (s *Server) diagnose(ctx context.Context, snapshot source.Snapshot, alwaysA } if err != nil { log.Error(ctx, "diagnose: could not generate diagnostics for go.mod file", err) - return nil } // Ensure that the reports returned from mod.Diagnostics are only related to the // go.mod file for the module. diff --git a/internal/lsp/lsp_test.go b/internal/lsp/lsp_test.go index 1ee4eb994d..9fc082b503 100644 --- a/internal/lsp/lsp_test.go +++ b/internal/lsp/lsp_test.go @@ -343,18 +343,25 @@ func (r *runner) SuggestedFix(t *testing.T, spn span.Span) { if err != nil { t.Fatal(err) } - snapshot := view.Snapshot() - _, diagnostics, err := source.FileDiagnostics(r.ctx, snapshot, uri) - if err != nil { - t.Fatal(err) + + // Get the diagnostics for this view if we have not done it before. + if r.diagnostics == nil { + r.diagnostics = make(map[span.URI][]source.Diagnostic) + // Always run diagnostics with analysis. + reports := r.server.diagnose(r.ctx, view.Snapshot(), true) + for key, diags := range reports { + r.diagnostics[key.id.URI] = diags + } } + diags := r.diagnostics[uri] + actions, err := r.server.CodeAction(r.ctx, &protocol.CodeActionParams{ TextDocument: protocol.TextDocumentIdentifier{ URI: protocol.NewURI(uri), }, Context: protocol.CodeActionContext{ Only: []protocol.CodeActionKind{protocol.QuickFix}, - Diagnostics: toProtocolDiagnostics(diagnostics), + Diagnostics: toProtocolDiagnostics(diags), }, }) if err != nil { @@ -376,7 +383,7 @@ func (r *runner) SuggestedFix(t *testing.T, spn span.Span) { return []byte(got), nil })) if fixed != got { - t.Errorf("suggested fixes failed for %s, expected:\n%v\ngot:\n%v", filename, fixed, got) + t.Errorf("suggested fixes failed for %s, expected:\n%#v\ngot:\n%#v", filename, fixed, got) } }