@@ -12,6 +12,7 @@ import (
12
12
"strings"
13
13
14
14
"golang.org/x/tools/go/ast/inspector"
15
+ "golang.org/x/tools/gopls/internal/bug"
15
16
"golang.org/x/tools/gopls/internal/lsp/analysis/fillstruct"
16
17
"golang.org/x/tools/gopls/internal/lsp/analysis/infertypeargs"
17
18
"golang.org/x/tools/gopls/internal/lsp/analysis/stubmethods"
@@ -198,26 +199,46 @@ func (s *Server) codeAction(ctx context.Context, params *protocol.CodeActionPara
198
199
if err != nil {
199
200
return nil , err
200
201
}
201
- for _ , pd := range diagnostics {
202
+ for _ , pd := range stubMethodsDiagnostics {
202
203
start , end , err := pgf .RangePos (pd .Range )
203
204
if err != nil {
204
205
return nil , err
205
206
}
206
- if d , ok := stubmethods .DiagnosticForError (pkg .FileSet (), pgf .File , start , end , pd .Message , pkg .GetTypesInfo ()); ok {
207
+ action , ok , err := func () (_ protocol.CodeAction , _ bool , rerr error ) {
208
+ // golang/go#61693: code actions were refactored to run outside of the
209
+ // analysis framework, but as a result they lost their panic recovery.
210
+ //
211
+ // Stubmethods "should never fail"", but put back the panic recovery as a
212
+ // defensive measure.
213
+ defer func () {
214
+ if r := recover (); r != nil {
215
+ rerr = bug .Errorf ("stubmethods panicked: %v" , r )
216
+ }
217
+ }()
218
+ d , ok := stubmethods .DiagnosticForError (pkg .FileSet (), pgf .File , start , end , pd .Message , pkg .GetTypesInfo ())
219
+ if ! ok {
220
+ return protocol.CodeAction {}, false , nil
221
+ }
207
222
cmd , err := command .NewApplyFixCommand (d .Message , command.ApplyFixArgs {
208
223
URI : protocol .URIFromSpanURI (pgf .URI ),
209
224
Fix : source .StubMethods ,
210
225
Range : pd .Range ,
211
226
})
212
227
if err != nil {
213
- return nil , err
228
+ return protocol. CodeAction {}, false , err
214
229
}
215
- actions = append ( actions , protocol.CodeAction {
230
+ return protocol.CodeAction {
216
231
Title : d .Message ,
217
232
Kind : protocol .QuickFix ,
218
233
Command : & cmd ,
219
234
Diagnostics : []protocol.Diagnostic {pd },
220
- })
235
+ }, true , nil
236
+ }()
237
+ if err != nil {
238
+ return nil , err
239
+ }
240
+ if ok {
241
+ actions = append (actions , action )
221
242
}
222
243
}
223
244
@@ -387,7 +408,17 @@ func refactorExtract(ctx context.Context, snapshot source.Snapshot, pgf *source.
387
408
return actions , nil
388
409
}
389
410
390
- func refactorRewrite (ctx context.Context , snapshot source.Snapshot , pkg source.Package , pgf * source.ParsedGoFile , fh source.FileHandle , rng protocol.Range ) ([]protocol.CodeAction , error ) {
411
+ func refactorRewrite (ctx context.Context , snapshot source.Snapshot , pkg source.Package , pgf * source.ParsedGoFile , fh source.FileHandle , rng protocol.Range ) (_ []protocol.CodeAction , rerr error ) {
412
+ // golang/go#61693: code actions were refactored to run outside of the
413
+ // analysis framework, but as a result they lost their panic recovery.
414
+ //
415
+ // These code actions should never fail, but put back the panic recovery as a
416
+ // defensive measure.
417
+ defer func () {
418
+ if r := recover (); r != nil {
419
+ rerr = bug .Errorf ("refactor.rewrite code actions panicked: %v" , r )
420
+ }
421
+ }()
391
422
start , end , err := pgf .RangePos (rng )
392
423
if err != nil {
393
424
return nil , err
0 commit comments