diff --git a/internal/lsp/diagnostics.go b/internal/lsp/diagnostics.go index b488f188e79..fcc777c1c71 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 1ee4eb994d9..9fc082b503e 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) } }