diff --git a/gopls/internal/lsp/protocol/generate/main.go b/gopls/internal/lsp/protocol/generate/main.go index d2897130176..9d5dc897355 100644 --- a/gopls/internal/lsp/protocol/generate/main.go +++ b/gopls/internal/lsp/protocol/generate/main.go @@ -101,6 +101,7 @@ func writeclient() { "context" "encoding/json" + "golang.org/x/tools/gopls/internal/util/bug" "golang.org/x/tools/internal/jsonrpc2" ) `) @@ -109,8 +110,15 @@ func writeclient() { out.WriteString(cdecls[k]) } out.WriteString("}\n\n") - out.WriteString("func clientDispatch(ctx context.Context, client Client, reply jsonrpc2.Replier, r jsonrpc2.Request) (bool, error) {\n") - out.WriteString("\tswitch r.Method() {\n") + out.WriteString(`func clientDispatch(ctx context.Context, client Client, reply jsonrpc2.Replier, r jsonrpc2.Request) (bool, error) { + defer func() { + if x := recover(); x != nil { + bug.Reportf("client panic in %s request", r.Method()) + panic(x) + } + }() + switch r.Method() { +`) for _, k := range ccases.keys() { out.WriteString(ccases[k]) } @@ -138,6 +146,7 @@ func writeserver() { "context" "encoding/json" + "golang.org/x/tools/gopls/internal/util/bug" "golang.org/x/tools/internal/jsonrpc2" ) `) @@ -149,6 +158,12 @@ func writeserver() { } func serverDispatch(ctx context.Context, server Server, reply jsonrpc2.Replier, r jsonrpc2.Request) (bool, error) { + defer func() { + if x := recover(); x != nil { + bug.Reportf("server panic in %s request", r.Method()) + panic(x) + } + }() switch r.Method() { `) for _, k := range scases.keys() { diff --git a/gopls/internal/lsp/protocol/tsclient.go b/gopls/internal/lsp/protocol/tsclient.go index 9821170de75..2900162f2e6 100644 --- a/gopls/internal/lsp/protocol/tsclient.go +++ b/gopls/internal/lsp/protocol/tsclient.go @@ -14,6 +14,7 @@ import ( "context" "encoding/json" + "golang.org/x/tools/gopls/internal/util/bug" "golang.org/x/tools/internal/jsonrpc2" ) @@ -41,6 +42,12 @@ type Client interface { } func clientDispatch(ctx context.Context, client Client, reply jsonrpc2.Replier, r jsonrpc2.Request) (bool, error) { + defer func() { + if x := recover(); x != nil { + bug.Reportf("client panic in %s request", r.Method()) + panic(x) + } + }() switch r.Method() { case "$/logTrace": var params LogTraceParams diff --git a/gopls/internal/lsp/protocol/tsserver.go b/gopls/internal/lsp/protocol/tsserver.go index 9f58a9aec28..9be44103682 100644 --- a/gopls/internal/lsp/protocol/tsserver.go +++ b/gopls/internal/lsp/protocol/tsserver.go @@ -14,6 +14,7 @@ import ( "context" "encoding/json" + "golang.org/x/tools/gopls/internal/util/bug" "golang.org/x/tools/internal/jsonrpc2" ) @@ -95,6 +96,12 @@ type Server interface { } func serverDispatch(ctx context.Context, server Server, reply jsonrpc2.Replier, r jsonrpc2.Request) (bool, error) { + defer func() { + if x := recover(); x != nil { + bug.Reportf("server panic in %s request", r.Method()) + panic(x) + } + }() switch r.Method() { case "$/progress": var params ProgressParams