From b4a7b92df5cca63666a8374536cade4489ee251f Mon Sep 17 00:00:00 2001 From: Rob Findley Date: Fri, 15 Nov 2024 00:01:55 +0000 Subject: [PATCH] gopls/internal/cache: failure to extract diagnostic fixes is an error Although the LSP specifies that diagnostic data should be transmitted unmodified to codeAction requests, we can't control whether clients abide by this rule. Therefore, a failure to extract fixes should be treated as a user-facing error, not a bug. Fixes golang/go#68819 Change-Id: I57e629cf381ee1112d98d22e728449995679b05f Reviewed-on: https://go-review.googlesource.com/c/tools/+/628237 Reviewed-by: Alan Donovan LUCI-TryBot-Result: Go LUCI --- gopls/internal/cache/diagnostics.go | 10 ++++------ gopls/internal/server/code_action.go | 5 ++++- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/gopls/internal/cache/diagnostics.go b/gopls/internal/cache/diagnostics.go index 95b1b9f1c18..0adbcb495db 100644 --- a/gopls/internal/cache/diagnostics.go +++ b/gopls/internal/cache/diagnostics.go @@ -191,13 +191,12 @@ func bundleLazyFixes(sd *Diagnostic) bool { // BundledLazyFixes extracts any bundled codeActions from the // diag.Data field. -func BundledLazyFixes(diag protocol.Diagnostic) []protocol.CodeAction { +func BundledLazyFixes(diag protocol.Diagnostic) ([]protocol.CodeAction, error) { var fix lazyFixesJSON if diag.Data != nil { err := protocol.UnmarshalJSON(*diag.Data, &fix) if err != nil { - bug.Reportf("unmarshalling lazy fix: %v", err) - return nil + return nil, fmt.Errorf("unmarshalling fix from diagnostic data: %v", err) } } @@ -205,8 +204,7 @@ func BundledLazyFixes(diag protocol.Diagnostic) []protocol.CodeAction { for _, action := range fix.Actions { // See bundleLazyFixes: for now we only support bundling commands. if action.Edit != nil { - bug.Reportf("bundled fix %q includes workspace edits", action.Title) - continue + return nil, fmt.Errorf("bundled fix %q includes workspace edits", action.Title) } // associate the action with the incoming diagnostic // (Note that this does not mutate the fix.Fixes slice). @@ -214,5 +212,5 @@ func BundledLazyFixes(diag protocol.Diagnostic) []protocol.CodeAction { actions = append(actions, action) } - return actions + return actions, nil } diff --git a/gopls/internal/server/code_action.go b/gopls/internal/server/code_action.go index e00e343850d..2e1c83f407f 100644 --- a/gopls/internal/server/code_action.go +++ b/gopls/internal/server/code_action.go @@ -265,7 +265,10 @@ func (s *server) codeActionsMatchingDiagnostics(ctx context.Context, uri protoco var actions []protocol.CodeAction var unbundled []protocol.Diagnostic // diagnostics without bundled code actions in their Data field for _, pd := range pds { - bundled := cache.BundledLazyFixes(pd) + bundled, err := cache.BundledLazyFixes(pd) + if err != nil { + return nil, err + } if len(bundled) > 0 { for _, fix := range bundled { if enabled(fix.Kind) {