2018-09-24 15:24:58 -06:00
|
|
|
// Copyright 2018 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package protocol
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2019-10-11 14:08:39 -06:00
|
|
|
"encoding/json"
|
2019-11-17 12:29:15 -07:00
|
|
|
"fmt"
|
2018-09-24 15:24:58 -06:00
|
|
|
|
2020-04-17 07:32:56 -06:00
|
|
|
"golang.org/x/tools/internal/event"
|
2018-09-24 15:24:58 -06:00
|
|
|
"golang.org/x/tools/internal/jsonrpc2"
|
2019-07-10 19:11:23 -06:00
|
|
|
"golang.org/x/tools/internal/xcontext"
|
2018-09-24 15:24:58 -06:00
|
|
|
)
|
|
|
|
|
2020-04-09 20:37:52 -06:00
|
|
|
var (
|
2019-10-11 14:08:39 -06:00
|
|
|
// RequestCancelledError should be used when a request is cancelled early.
|
2020-04-09 20:37:52 -06:00
|
|
|
RequestCancelledError = jsonrpc2.NewError(-32800, "JSON RPC cancelled")
|
2019-10-11 14:08:39 -06:00
|
|
|
)
|
|
|
|
|
internal/lsp: refactor LSP server instantiation
Previously, the process of instantiating and running the LSP server was
sharded across the lsp, protocol, and cmd packages, and this resulted in
some APIs that are hard to work with. For example, it's hard to guess
the difference between lsp.NewClientServer, lsp.NewServer,
protocol.NewServer (which returns a client), and protocol.NewClient
(which returns a server).
This change reorganizes Server instantiation as follows:
+ The lsp.Server is now purely an implementation of the protocol.Server
interface. It is no longer responsible for installing itself into the
jsonrpc2 Stream, nor for running itself.
+ A new package 'lsprpc' is added, to implement the logic of binding an
incoming connection to an LSP server session. This is put in a
separate package for lack of a clear home: it didn't really
philosophically belong in any of the lsp, cmd, or protocol packages.
We can perhaps move it to cmd in the future, but I'd like to keep it
as a separate package while I develop request forwarding.
simplified import graph:
jsonrpc2 ⭠ lsprpc ⭠ cmd
⭩ ⭦
lsp (t.b.d. client tests)
⭩ ⭨
protocol source
+ The jsonrpc2 package is extended to have a minimal API for running a
'StreamServer': something analogous to an HTTP server that listens
for new connections and delegates to a handler (but we couldn't use
the word 'Handler' for this delegate as it was already taken).
After these changes, I hope that the concerns of "serving the LSP",
"serving jsonrpc2", and "installing the LSP on jsonrpc2" are more
logically organized, though one legitimate criticism is that the word
'Server' is still heavily overloaded.
This change prepares a subsequent change which hijacks the jsonrpc2
connection when forwarding messages to a shared gopls instance.
To test this change, the following improvements are made:
+ A servertest package is added to make it easier to run a test against
an in-process jsonrpc2 server. For now, this uses TCP but it could
easily be modified to use io.Pipe.
+ cmd tests are updated to use the servertest package. Unfortunately it
wasn't yet possible to eliminate the concept of `remote=internal` in
favor of just using multiple sessions, because view initialization
involves calling both `go env` and `packages.Load`, which slow down
session startup significantly. See also golang.org/issue/35968.
Instead, the syntax for `-remote=internal` is modified to be
`-remote=internal@127.0.0.1:12345`.
+ An additional test for request cancellation is added for the
sessionserver package. This test uncovered a bug: when calling
Canceller.Cancel, we were using id rather than &id, which resulted in
incorrect json serialization (as only the pointer receiver implements
the json.Marshaller interface).
Updates golang/go#34111
Change-Id: I75c219df634348cdf53a9e57839b98588311a9ef
Reviewed-on: https://go-review.googlesource.com/c/tools/+/215742
Run-TryBot: Robert Findley <rfindley@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Rebecca Stambler <rstambler@golang.org>
Reviewed-by: Heschi Kreinick <heschi@google.com>
2020-01-21 17:34:50 -07:00
|
|
|
// ClientDispatcher returns a Client that dispatches LSP requests across the
|
|
|
|
// given jsonrpc2 connection.
|
|
|
|
func ClientDispatcher(conn *jsonrpc2.Conn) Client {
|
|
|
|
return &clientDispatcher{Conn: conn}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ServerDispatcher returns a Server that dispatches LSP requests across the
|
|
|
|
// given jsonrpc2 connection.
|
|
|
|
func ServerDispatcher(conn *jsonrpc2.Conn) Server {
|
|
|
|
return &serverDispatcher{Conn: conn}
|
|
|
|
}
|
|
|
|
|
2020-04-02 10:43:08 -06:00
|
|
|
func Handlers(handler jsonrpc2.Handler) jsonrpc2.Handler {
|
|
|
|
return CancelHandler(
|
2020-05-05 08:50:13 -06:00
|
|
|
jsonrpc2.AsyncHandler(
|
|
|
|
jsonrpc2.MustReplyHandler(handler)))
|
2020-04-02 10:43:08 -06:00
|
|
|
}
|
|
|
|
|
2020-04-01 11:19:48 -06:00
|
|
|
func CancelHandler(handler jsonrpc2.Handler) jsonrpc2.Handler {
|
|
|
|
handler, canceller := jsonrpc2.CancelHandler(handler)
|
2020-04-12 20:47:10 -06:00
|
|
|
return func(ctx context.Context, reply jsonrpc2.Replier, req jsonrpc2.Request) error {
|
|
|
|
if req.Method() != "$/cancelRequest" {
|
2020-05-05 08:50:13 -06:00
|
|
|
// TODO(iancottrell): See if we can generate a reply for the request to be cancelled
|
|
|
|
// at the point of cancellation rather than waiting for gopls to naturally reply.
|
|
|
|
// To do that, we need to keep track of whether a reply has been sent already and
|
|
|
|
// be careful about racing between the two paths.
|
|
|
|
// TODO(iancottrell): Add a test that watches the stream and verifies the response
|
|
|
|
// for the cancelled request flows.
|
|
|
|
replyWithDetachedContext := func(ctx context.Context, resp interface{}, err error) error {
|
|
|
|
// https://microsoft.github.io/language-server-protocol/specifications/specification-current/#cancelRequest
|
|
|
|
if ctx.Err() != nil && err == nil {
|
|
|
|
err = RequestCancelledError
|
|
|
|
}
|
|
|
|
ctx = xcontext.Detach(ctx)
|
|
|
|
return reply(ctx, resp, err)
|
|
|
|
}
|
|
|
|
return handler(ctx, replyWithDetachedContext, req)
|
2020-04-01 11:19:48 -06:00
|
|
|
}
|
2019-10-11 14:08:39 -06:00
|
|
|
var params CancelParams
|
2020-04-12 20:47:10 -06:00
|
|
|
if err := json.Unmarshal(req.Params(), ¶ms); err != nil {
|
|
|
|
return sendParseError(ctx, reply, err)
|
2020-04-01 11:19:48 -06:00
|
|
|
}
|
|
|
|
if n, ok := params.ID.(float64); ok {
|
2020-04-21 09:47:21 -06:00
|
|
|
canceller(jsonrpc2.NewIntID(int64(n)))
|
2020-04-01 11:19:48 -06:00
|
|
|
} else if s, ok := params.ID.(string); ok {
|
2020-04-21 09:47:21 -06:00
|
|
|
canceller(jsonrpc2.NewStringID(s))
|
2019-10-11 14:08:39 -06:00
|
|
|
} else {
|
2020-04-12 20:47:10 -06:00
|
|
|
return sendParseError(ctx, reply, fmt.Errorf("request ID %v malformed", params.ID))
|
2019-10-11 14:08:39 -06:00
|
|
|
}
|
2020-04-09 21:54:23 -06:00
|
|
|
return reply(ctx, nil, nil)
|
2019-10-11 14:08:39 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-07 19:35:47 -06:00
|
|
|
func Call(ctx context.Context, conn *jsonrpc2.Conn, method string, params interface{}, result interface{}) error {
|
|
|
|
id, err := conn.Call(ctx, method, params, result)
|
|
|
|
if ctx.Err() != nil {
|
|
|
|
cancelCall(ctx, conn, id)
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-04-01 11:19:48 -06:00
|
|
|
func cancelCall(ctx context.Context, conn *jsonrpc2.Conn, id jsonrpc2.ID) {
|
2019-07-10 19:11:23 -06:00
|
|
|
ctx = xcontext.Detach(ctx)
|
2020-04-20 10:14:12 -06:00
|
|
|
ctx, done := event.Start(ctx, "protocol.canceller")
|
2019-06-26 20:46:12 -06:00
|
|
|
defer done()
|
internal/lsp: refactor LSP server instantiation
Previously, the process of instantiating and running the LSP server was
sharded across the lsp, protocol, and cmd packages, and this resulted in
some APIs that are hard to work with. For example, it's hard to guess
the difference between lsp.NewClientServer, lsp.NewServer,
protocol.NewServer (which returns a client), and protocol.NewClient
(which returns a server).
This change reorganizes Server instantiation as follows:
+ The lsp.Server is now purely an implementation of the protocol.Server
interface. It is no longer responsible for installing itself into the
jsonrpc2 Stream, nor for running itself.
+ A new package 'lsprpc' is added, to implement the logic of binding an
incoming connection to an LSP server session. This is put in a
separate package for lack of a clear home: it didn't really
philosophically belong in any of the lsp, cmd, or protocol packages.
We can perhaps move it to cmd in the future, but I'd like to keep it
as a separate package while I develop request forwarding.
simplified import graph:
jsonrpc2 ⭠ lsprpc ⭠ cmd
⭩ ⭦
lsp (t.b.d. client tests)
⭩ ⭨
protocol source
+ The jsonrpc2 package is extended to have a minimal API for running a
'StreamServer': something analogous to an HTTP server that listens
for new connections and delegates to a handler (but we couldn't use
the word 'Handler' for this delegate as it was already taken).
After these changes, I hope that the concerns of "serving the LSP",
"serving jsonrpc2", and "installing the LSP on jsonrpc2" are more
logically organized, though one legitimate criticism is that the word
'Server' is still heavily overloaded.
This change prepares a subsequent change which hijacks the jsonrpc2
connection when forwarding messages to a shared gopls instance.
To test this change, the following improvements are made:
+ A servertest package is added to make it easier to run a test against
an in-process jsonrpc2 server. For now, this uses TCP but it could
easily be modified to use io.Pipe.
+ cmd tests are updated to use the servertest package. Unfortunately it
wasn't yet possible to eliminate the concept of `remote=internal` in
favor of just using multiple sessions, because view initialization
involves calling both `go env` and `packages.Load`, which slow down
session startup significantly. See also golang.org/issue/35968.
Instead, the syntax for `-remote=internal` is modified to be
`-remote=internal@127.0.0.1:12345`.
+ An additional test for request cancellation is added for the
sessionserver package. This test uncovered a bug: when calling
Canceller.Cancel, we were using id rather than &id, which resulted in
incorrect json serialization (as only the pointer receiver implements
the json.Marshaller interface).
Updates golang/go#34111
Change-Id: I75c219df634348cdf53a9e57839b98588311a9ef
Reviewed-on: https://go-review.googlesource.com/c/tools/+/215742
Run-TryBot: Robert Findley <rfindley@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Rebecca Stambler <rstambler@golang.org>
Reviewed-by: Heschi Kreinick <heschi@google.com>
2020-01-21 17:34:50 -07:00
|
|
|
// Note that only *jsonrpc2.ID implements json.Marshaler.
|
|
|
|
conn.Notify(ctx, "$/cancelRequest", &CancelParams{ID: &id})
|
2020-01-21 13:48:02 -07:00
|
|
|
}
|
|
|
|
|
2020-04-12 20:47:10 -06:00
|
|
|
func sendParseError(ctx context.Context, reply jsonrpc2.Replier, err error) error {
|
2020-04-09 21:54:23 -06:00
|
|
|
return reply(ctx, nil, fmt.Errorf("%w: %s", jsonrpc2.ErrParse, err))
|
2018-09-24 15:24:58 -06:00
|
|
|
}
|