Skip to content

Commit

Permalink
gopls/internal/cache: failure to extract diagnostic fixes is an error
Browse files Browse the repository at this point in the history
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 <adonovan@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
  • Loading branch information
findleyr authored and dennypenta committed Dec 3, 2024
1 parent e7e32d7 commit b4a7b92
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 7 deletions.
10 changes: 4 additions & 6 deletions gopls/internal/cache/diagnostics.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,28 +191,26 @@ 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)
}
}

var actions []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).
action.Diagnostics = []protocol.Diagnostic{diag}
actions = append(actions, action)
}

return actions
return actions, nil
}
5 changes: 4 additions & 1 deletion gopls/internal/server/code_action.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit b4a7b92

Please sign in to comment.