2018-12-17 11:52:58 -07: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 cmd
import (
"context"
"flag"
"fmt"
"os"
2020-02-10 09:34:13 -07:00
"strings"
2020-02-19 16:48:38 -07:00
"time"
2018-12-17 11:52:58 -07:00
"golang.org/x/tools/internal/jsonrpc2"
2019-11-19 14:45:03 -07:00
"golang.org/x/tools/internal/lsp/cache"
2020-02-28 08:30:03 -07:00
"golang.org/x/tools/internal/lsp/debug"
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
"golang.org/x/tools/internal/lsp/lsprpc"
2019-08-27 10:52:32 -06:00
"golang.org/x/tools/internal/lsp/protocol"
2018-12-17 11:52:58 -07:00
"golang.org/x/tools/internal/tool"
)
2019-02-07 15:05:20 -07:00
// Serve is a struct that exposes the configurable parts of the LSP server as
2018-12-17 11:52:58 -07:00
// flags, in the right form for tool.Main to consume.
2019-02-07 15:05:20 -07:00
type Serve struct {
2020-02-19 16:48:38 -07:00
Logfile string ` flag:"logfile" help:"filename to log to. if value is \"auto\", then logging to a default output file is enabled" `
Mode string ` flag:"mode" help:"no effect" `
Port int ` flag:"port" help:"port on which to run gopls for debugging purposes" `
Address string ` flag:"listen" help:"address on which to listen for remote connections. If prefixed by 'unix;', the subsequent address is assumed to be a unix domain socket. Otherwise, TCP is used." `
IdleTimeout time . Duration ` flag:"listen.timeout" help:"when used with -listen, shut down the server when there are no connected clients for this duration" `
Trace bool ` flag:"rpc.trace" help:"print the full rpc trace in lsp inspector format" `
Debug string ` flag:"debug" help:"serve debug information on the supplied address" `
2019-02-07 15:05:20 -07:00
app * Application
2018-12-17 11:52:58 -07:00
}
2019-02-07 15:05:20 -07:00
func ( s * Serve ) Name ( ) string { return "serve" }
func ( s * Serve ) Usage ( ) string { return "" }
func ( s * Serve ) ShortHelp ( ) string {
2018-12-17 11:52:58 -07:00
return "run a server for Go code using the Language Server Protocol"
}
2019-02-07 15:05:20 -07:00
func ( s * Serve ) DetailedHelp ( f * flag . FlagSet ) {
2018-12-17 11:52:58 -07:00
fmt . Fprint ( f . Output ( ) , `
The server communicates using JSONRPC2 on stdin and stdout , and is intended to be run directly as
a child of an editor process .
2018-12-14 13:46:12 -07:00
gopls server flags are :
2018-12-17 11:52:58 -07:00
` )
2018-12-14 13:46:12 -07:00
f . PrintDefaults ( )
2018-12-17 11:52:58 -07:00
}
// Run configures a server based on the flags, and then runs it.
// It blocks until the server shuts down.
2019-02-07 15:05:20 -07:00
func ( s * Serve ) Run ( ctx context . Context , args ... string ) error {
2018-12-17 11:52:58 -07:00
if len ( args ) > 0 {
return tool . CommandLineErrorf ( "server does not take arguments, got %v" , args )
}
2019-05-28 23:38:51 -06:00
2020-02-28 08:30:03 -07:00
di := debug . GetInstance ( ctx )
if di != nil {
closeLog , err := di . SetLogFile ( s . Logfile )
if err != nil {
return err
}
defer closeLog ( )
di . ServerAddress = s . Address
di . DebugAddress = s . Debug
di . Serve ( ctx )
di . MonitorMemory ( ctx )
2020-02-06 06:27:27 -07:00
}
2020-02-06 17:50:37 -07:00
var ss jsonrpc2 . StreamServer
2019-02-07 15:05:20 -07:00
if s . app . Remote != "" {
2020-02-10 09:34:13 -07:00
network , addr := parseAddr ( s . app . Remote )
2020-02-28 08:30:03 -07:00
ss = lsprpc . NewForwarder ( network , addr , true )
2020-02-06 17:50:37 -07:00
} else {
2020-02-28 08:30:03 -07:00
ss = lsprpc . NewStreamServer ( cache . New ( ctx , s . app . options ) , true )
2019-02-07 15:05:20 -07:00
}
2019-03-29 17:04:29 -06:00
if s . Address != "" {
2020-02-10 09:34:13 -07:00
network , addr := parseAddr ( s . Address )
2020-02-19 16:48:38 -07:00
return jsonrpc2 . ListenAndServe ( ctx , network , addr , ss , s . IdleTimeout )
2019-03-29 17:04:29 -06:00
}
if s . Port != 0 {
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
addr := fmt . Sprintf ( ":%v" , s . Port )
2020-02-19 16:48:38 -07:00
return jsonrpc2 . ListenAndServe ( ctx , "tcp" , addr , ss , s . IdleTimeout )
2019-03-29 17:04:29 -06:00
}
stream := jsonrpc2 . NewHeaderStream ( os . Stdin , os . Stdout )
2020-02-28 08:30:03 -07:00
if s . Trace && di != nil {
stream = protocol . LoggingStream ( stream , di . LogWriter )
2019-08-27 10:52:32 -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
return ss . ServeStream ( ctx , stream )
2019-03-29 17:04:29 -06:00
}
2020-02-10 09:34:13 -07:00
// parseAddr parses the -listen flag in to a network, and address.
func parseAddr ( listen string ) ( network string , address string ) {
2020-02-18 10:47:19 -07:00
// Allow passing just -remote=auto, as a shorthand for using automatic remote
// resolution.
if listen == lsprpc . AutoNetwork {
return lsprpc . AutoNetwork , ""
}
2020-02-10 09:34:13 -07:00
if parts := strings . SplitN ( listen , ";" , 2 ) ; len ( parts ) == 2 {
return parts [ 0 ] , parts [ 1 ]
}
return "tcp" , listen
}