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.
2019-01-16 15:01:06 -07:00
// Package cmd handles the gopls command line.
2018-12-17 11:52:58 -07:00
// It contains a handler for each of the modes, along with all the flag handling
// and the command line output format.
package cmd
import (
"context"
"flag"
"fmt"
2018-12-14 13:46:12 -07:00
"go/token"
2019-04-01 18:08:14 -06:00
"io/ioutil"
2019-03-29 17:04:29 -06:00
"log"
2019-02-08 14:16:57 -07:00
"net"
"os"
"strings"
2019-05-07 11:54:07 -06:00
"sync"
2020-02-07 06:30:31 -07:00
"time"
2019-02-05 15:27:35 -07:00
2019-02-08 14:16:57 -07:00
"golang.org/x/tools/internal/jsonrpc2"
"golang.org/x/tools/internal/lsp"
2019-05-15 10:24:49 -06:00
"golang.org/x/tools/internal/lsp/cache"
2020-02-07 06:30:31 -07:00
"golang.org/x/tools/internal/lsp/debug"
2019-02-08 14:16:57 -07:00
"golang.org/x/tools/internal/lsp/protocol"
2019-05-15 10:24:49 -06:00
"golang.org/x/tools/internal/lsp/source"
2019-02-08 14:16:57 -07:00
"golang.org/x/tools/internal/span"
2018-12-17 11:52:58 -07:00
"golang.org/x/tools/internal/tool"
2019-07-10 19:11:23 -06:00
"golang.org/x/tools/internal/xcontext"
2019-08-06 13:13:11 -06:00
errors "golang.org/x/xerrors"
2018-12-17 11:52:58 -07:00
)
// Application is the main application as passed to tool.Main
// It handles the main command line parsing and dispatch to the sub commands.
type Application struct {
2018-12-14 13:46:12 -07:00
// Core application flags
2018-12-17 11:52:58 -07:00
// Embed the basic profiling flags supported by the tool package
tool . Profile
2019-02-07 15:05:20 -07:00
// We include the server configuration directly for now, so the flags work
// even without the verb.
// TODO: Remove this when we stop allowing the serve verb by default.
Serve Serve
2018-12-14 13:46:12 -07:00
2019-11-19 14:45:03 -07:00
// the options configuring function to invoke when building a server
options func ( * source . Options )
2019-05-17 08:51:19 -06:00
2019-07-17 18:30:54 -06:00
// The name of the binary, used in help and telemetry.
name string
2019-05-17 08:51:19 -06:00
// The working directory to run commands in.
wd string
// The environment variables to use.
env [ ] string
2019-05-15 10:24:49 -06:00
2019-02-07 15:05:20 -07:00
// Support for remote lsp server
2020-02-10 09:34:13 -07:00
Remote string ` flag:"remote" help:"*EXPERIMENTAL* - forward all commands to a remote lsp specified by this flag. If prefixed by 'unix;', the subsequent address is assumed to be a unix domain socket. Otherwise, TCP is used." `
2019-05-10 16:50:29 -06:00
// Enable verbose logging
2019-12-07 06:10:13 -07:00
Verbose bool ` flag:"v" help:"verbose output" `
2019-07-17 17:17:43 -06:00
// Control ocagent export of telemetry
2019-12-07 06:10:13 -07:00
OCAgent string ` flag:"ocagent" help:"the address of the ocagent, or off" `
2019-10-10 18:48:16 -06:00
// PrepareOptions is called to update the options when a new view is built.
// It is primarily to allow the behavior of gopls to be modified by hooks.
PrepareOptions func ( * source . Options )
2020-02-07 06:30:31 -07:00
debug debug . Instance
2018-12-17 11:52:58 -07:00
}
2020-01-01 05:14:58 -07:00
// New returns a new Application ready to run.
2019-10-10 18:48:16 -06:00
func New ( name , wd string , env [ ] string , options func ( * source . Options ) ) * Application {
2019-05-17 08:51:19 -06:00
if wd == "" {
wd , _ = os . Getwd ( )
2019-05-15 10:24:49 -06:00
}
2019-05-17 08:51:19 -06:00
app := & Application {
2019-11-19 14:45:03 -07:00
options : options ,
2019-07-17 17:17:43 -06:00
name : name ,
wd : wd ,
env : env ,
OCAgent : "off" , //TODO: Remove this line to default the exporter to on
2019-05-15 10:24:49 -06:00
}
return app
}
2018-12-17 11:52:58 -07:00
// Name implements tool.Application returning the binary name.
2019-07-17 18:30:54 -06:00
func ( app * Application ) Name ( ) string { return app . name }
2018-12-17 11:52:58 -07:00
// Usage implements tool.Application returning empty extra argument usage.
2018-12-14 13:46:12 -07:00
func ( app * Application ) Usage ( ) string { return "<command> [command-flags] [command-args]" }
2018-12-17 11:52:58 -07:00
// ShortHelp implements tool.Application returning the main binary help.
func ( app * Application ) ShortHelp ( ) string {
2018-12-14 13:46:12 -07:00
return "The Go Language source tools."
2018-12-17 11:52:58 -07:00
}
// DetailedHelp implements tool.Application returning the main binary help.
// This includes the short help for all the sub commands.
func ( app * Application ) DetailedHelp ( f * flag . FlagSet ) {
fmt . Fprint ( f . Output ( ) , `
2019-12-07 06:10:13 -07:00
gopls is a Go language server . It is typically used with an editor to provide
language features . When no command is specified , gopls will default to the ' serve '
command . The language features can also be accessed via the gopls command - line interface .
2018-12-14 13:46:12 -07:00
Available commands are :
2019-12-25 12:51:36 -07:00
` )
2019-12-07 06:10:13 -07:00
fmt . Fprint ( f . Output ( ) , `
main :
2018-12-17 11:52:58 -07:00
` )
2019-12-07 06:10:13 -07:00
for _ , c := range app . mainCommands ( ) {
fmt . Fprintf ( f . Output ( ) , " %s : %v\n" , c . Name ( ) , c . ShortHelp ( ) )
}
fmt . Fprint ( f . Output ( ) , `
features :
` )
for _ , c := range app . featureCommands ( ) {
2018-12-17 11:52:58 -07:00
fmt . Fprintf ( f . Output ( ) , " %s : %v\n" , c . Name ( ) , c . ShortHelp ( ) )
}
fmt . Fprint ( f . Output ( ) , `
2019-01-16 15:01:06 -07:00
gopls flags are :
2018-12-17 11:52:58 -07:00
` )
f . PrintDefaults ( )
}
// Run takes the args after top level flag processing, and invokes the correct
// sub command as specified by the first argument.
// If no arguments are passed it will invoke the server sub command, as a
2019-01-05 12:20:03 -07:00
// temporary measure for compatibility.
2018-12-17 11:52:58 -07:00
func ( app * Application ) Run ( ctx context . Context , args ... string ) error {
2020-02-07 06:30:31 -07:00
app . debug = debug . Instance {
StartTime : time . Now ( ) ,
Workdir : app . wd ,
OCAgentConfig : app . OCAgent ,
}
app . debug . Prepare ( ctx )
2019-02-12 12:23:47 -07:00
app . Serve . app = app
2018-12-17 11:52:58 -07:00
if len ( args ) == 0 {
2019-09-19 15:50:14 -06:00
return tool . Run ( ctx , & app . Serve , args )
2018-12-17 11:52:58 -07:00
}
2018-12-14 13:46:12 -07:00
command , args := args [ 0 ] , args [ 1 : ]
for _ , c := range app . commands ( ) {
if c . Name ( ) == command {
2019-09-19 15:50:14 -06:00
return tool . Run ( ctx , c , args )
2018-12-17 11:52:58 -07:00
}
}
2018-12-14 13:46:12 -07:00
return tool . CommandLineErrorf ( "Unknown command %v" , command )
2018-12-17 11:52:58 -07:00
}
2018-12-14 13:46:12 -07:00
// commands returns the set of commands supported by the gopls tool on the
2018-12-17 11:52:58 -07:00
// command line.
2018-12-14 13:46:12 -07:00
// The command is specified by the first non flag argument.
func ( app * Application ) commands ( ) [ ] tool . Application {
2019-12-07 06:10:13 -07:00
var commands [ ] tool . Application
commands = append ( commands , app . mainCommands ( ) ... )
commands = append ( commands , app . featureCommands ( ) ... )
return commands
}
func ( app * Application ) mainCommands ( ) [ ] tool . Application {
2018-12-17 11:52:58 -07:00
return [ ] tool . Application {
2019-02-07 15:05:20 -07:00
& app . Serve ,
2019-12-07 06:10:13 -07:00
& version { app : app } ,
2019-05-03 12:31:03 -06:00
& bug { } ,
2019-12-07 06:10:13 -07:00
}
}
func ( app * Application ) featureCommands ( ) [ ] tool . Application {
return [ ] tool . Application {
2019-02-08 14:16:57 -07:00
& check { app : app } ,
2019-11-10 07:43:06 -07:00
& foldingRanges { app : app } ,
2019-04-07 21:08:47 -06:00
& format { app : app } ,
2019-11-18 00:14:06 -07:00
& highlight { app : app } ,
2019-11-21 14:28:59 -07:00
& implementation { app : app } ,
2019-10-29 02:41:12 -06:00
& imports { app : app } ,
2019-11-18 00:14:06 -07:00
& links { app : app } ,
2020-02-03 23:09:33 -07:00
& prepareRename { app : app } ,
2019-04-07 21:08:47 -06:00
& query { app : app } ,
2019-10-19 15:26:56 -06:00
& references { app : app } ,
2019-09-20 16:38:42 -06:00
& rename { app : app } ,
2019-10-29 02:55:42 -06:00
& signature { app : app } ,
2019-10-29 02:38:47 -06:00
& suggestedfix { app : app } ,
2019-10-27 11:41:48 -06:00
& symbols { app : app } ,
2019-02-08 14:16:57 -07:00
}
}
2019-05-07 11:54:07 -06:00
var (
internalMu sync . Mutex
internalConnections = make ( map [ string ] * connection )
)
2019-04-01 18:08:14 -06:00
2019-05-07 11:54:07 -06:00
func ( app * Application ) connect ( ctx context . Context ) ( * connection , error ) {
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
switch {
case app . Remote == "" :
2019-05-07 11:54:07 -06:00
connection := newConnection ( app )
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
connection . Server = lsp . NewServer ( cache . New ( app . options ) . NewSession ( ) , connection . Client )
ctx = protocol . WithClient ( ctx , connection . Client )
2019-12-06 00:44:16 -07:00
return connection , connection . initialize ( ctx , app . options )
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
case strings . HasPrefix ( app . Remote , "internal@" ) :
2019-05-07 11:54:07 -06:00
internalMu . Lock ( )
defer internalMu . Unlock ( )
2019-05-17 08:51:19 -06:00
if c := internalConnections [ app . wd ] ; c != nil {
2019-05-07 11:54:07 -06:00
return c , nil
}
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
remote := app . Remote [ len ( "internal@" ) : ]
2019-07-10 19:11:23 -06:00
ctx := xcontext . Detach ( ctx ) //TODO:a way of shutting down the internal server
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
connection , err := app . connectRemote ( ctx , remote )
if err != nil {
2019-05-07 11:54:07 -06:00
return nil , err
}
2019-05-17 08:51:19 -06:00
internalConnections [ app . wd ] = connection
2019-05-07 11:54:07 -06:00
return connection , nil
2019-03-28 15:17:40 -06:00
default :
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 app . connectRemote ( ctx , app . Remote )
}
}
func ( app * Application ) connectRemote ( ctx context . Context , remote string ) ( * connection , error ) {
connection := newConnection ( app )
conn , err := net . Dial ( "tcp" , remote )
if err != nil {
return nil , err
2019-02-08 14:16:57 -07: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
stream := jsonrpc2 . NewHeaderStream ( conn , conn )
cc := jsonrpc2 . NewConn ( stream )
connection . Server = protocol . ServerDispatcher ( cc )
cc . AddHandler ( protocol . ClientHandler ( connection . Client ) )
cc . AddHandler ( protocol . Canceller { } )
ctx = protocol . WithClient ( ctx , connection . Client )
go cc . Run ( ctx )
return connection , connection . initialize ( ctx , app . options )
2019-05-07 11:54:07 -06:00
}
2019-04-17 12:47:10 -06:00
2019-12-06 00:44:16 -07:00
func ( c * connection ) initialize ( ctx context . Context , options func ( * source . Options ) ) error {
2019-11-17 12:29:15 -07:00
params := & protocol . ParamInitialize { }
2020-02-12 14:36:46 -07:00
params . RootURI = protocol . URIFromPath ( c . Client . app . wd )
2019-04-08 07:16:48 -06:00
params . Capabilities . Workspace . Configuration = true
2019-12-06 00:44:16 -07:00
// Make sure to respect configured options when sending initialize request.
2020-01-30 10:25:09 -07:00
opts := source . DefaultOptions ( )
2019-12-06 00:44:16 -07:00
if options != nil {
options ( & opts )
}
2019-11-17 12:29:15 -07:00
params . Capabilities . TextDocument . Hover = protocol . HoverClientCapabilities {
2019-12-06 00:44:16 -07:00
ContentFormat : [ ] protocol . MarkupKind { opts . PreferredContentFormat } ,
2019-09-24 15:51:00 -06:00
}
2019-12-06 00:44:16 -07:00
2019-05-07 11:54:07 -06:00
if _ , err := c . Server . Initialize ( ctx , params ) ; err != nil {
return err
2019-02-08 14:16:57 -07:00
}
2019-05-07 11:54:07 -06:00
if err := c . Server . Initialized ( ctx , & protocol . InitializedParams { } ) ; err != nil {
return err
2019-02-08 14:16:57 -07:00
}
2019-05-07 11:54:07 -06:00
return nil
}
type connection struct {
protocol . Server
Client * cmdClient
2019-02-08 14:16:57 -07:00
}
2019-05-07 11:54:07 -06:00
type cmdClient struct {
2019-02-08 14:16:57 -07:00
protocol . Server
2019-05-07 11:54:07 -06:00
app * Application
fset * token . FileSet
2020-01-10 15:54:47 -07:00
diagnosticsMu sync . Mutex
diagnosticsDone chan struct { }
2019-05-07 11:54:07 -06:00
filesMu sync . Mutex
files map [ span . URI ] * cmdFile
}
type cmdFile struct {
2020-01-10 15:54:47 -07:00
uri span . URI
mapper * protocol . ColumnMapper
err error
added bool
diagnostics [ ] protocol . Diagnostic
2019-02-08 14:16:57 -07:00
}
2019-05-07 11:54:07 -06:00
func newConnection ( app * Application ) * connection {
return & connection {
Client : & cmdClient {
app : app ,
fset : token . NewFileSet ( ) ,
files : make ( map [ span . URI ] * cmdFile ) ,
} ,
}
}
2020-02-13 11:46:49 -07:00
// fileURI converts a DocumentURI to a file:// span.URI, panicking if it's not a file.
func fileURI ( uri protocol . DocumentURI ) span . URI {
sURI := uri . SpanURI ( )
if ! sURI . IsFile ( ) {
panic ( fmt . Sprintf ( "%q is not a file URI" , uri ) )
}
return sURI
}
2019-05-07 11:54:07 -06:00
func ( c * cmdClient ) ShowMessage ( ctx context . Context , p * protocol . ShowMessageParams ) error { return nil }
func ( c * cmdClient ) ShowMessageRequest ( ctx context . Context , p * protocol . ShowMessageRequestParams ) ( * protocol . MessageActionItem , error ) {
2019-02-08 14:16:57 -07:00
return nil , nil
}
2019-05-07 11:54:07 -06:00
func ( c * cmdClient ) LogMessage ( ctx context . Context , p * protocol . LogMessageParams ) error {
2019-03-29 17:04:29 -06:00
switch p . Type {
case protocol . Error :
log . Print ( "Error:" , p . Message )
case protocol . Warning :
log . Print ( "Warning:" , p . Message )
case protocol . Info :
2019-05-10 16:50:29 -06:00
if c . app . Verbose {
log . Print ( "Info:" , p . Message )
}
2019-03-29 17:04:29 -06:00
case protocol . Log :
2019-05-10 16:50:29 -06:00
if c . app . Verbose {
log . Print ( "Log:" , p . Message )
}
2019-03-29 17:04:29 -06:00
default :
2019-05-10 16:50:29 -06:00
if c . app . Verbose {
log . Print ( p . Message )
}
2019-03-29 17:04:29 -06:00
}
return nil
}
2019-05-07 11:54:07 -06:00
func ( c * cmdClient ) Event ( ctx context . Context , t * interface { } ) error { return nil }
func ( c * cmdClient ) RegisterCapability ( ctx context . Context , p * protocol . RegistrationParams ) error {
2019-02-08 14:16:57 -07:00
return nil
}
2019-05-07 11:54:07 -06:00
func ( c * cmdClient ) UnregisterCapability ( ctx context . Context , p * protocol . UnregistrationParams ) error {
2019-02-08 14:16:57 -07:00
return nil
}
2019-05-07 11:54:07 -06:00
func ( c * cmdClient ) WorkspaceFolders ( ctx context . Context ) ( [ ] protocol . WorkspaceFolder , error ) {
2019-02-08 14:16:57 -07:00
return nil , nil
}
2019-05-07 11:54:07 -06:00
2019-11-17 12:29:15 -07:00
func ( c * cmdClient ) Configuration ( ctx context . Context , p * protocol . ParamConfiguration ) ( [ ] interface { } , error ) {
2019-02-08 14:16:57 -07:00
results := make ( [ ] interface { } , len ( p . Items ) )
for i , item := range p . Items {
if item . Section != "gopls" {
continue
}
env := map [ string ] interface { } { }
2019-05-17 08:51:19 -06:00
for _ , value := range c . app . env {
2019-02-08 14:16:57 -07:00
l := strings . SplitN ( value , "=" , 2 )
if len ( l ) != 2 {
continue
}
env [ l [ 0 ] ] = l [ 1 ]
}
2019-04-27 20:45:06 -06:00
results [ i ] = map [ string ] interface { } {
2019-10-22 15:15:19 -06:00
"env" : env ,
"go-diff" : true ,
2019-04-27 20:45:06 -06:00
}
2018-12-17 11:52:58 -07:00
}
2019-02-08 14:16:57 -07:00
return results , nil
}
2019-05-07 11:54:07 -06:00
func ( c * cmdClient ) ApplyEdit ( ctx context . Context , p * protocol . ApplyWorkspaceEditParams ) ( * protocol . ApplyWorkspaceEditResponse , error ) {
2019-04-23 08:05:23 -06:00
return & protocol . ApplyWorkspaceEditResponse { Applied : false , FailureReason : "not implemented" } , nil
2019-02-08 14:16:57 -07:00
}
2019-04-01 18:08:14 -06:00
2019-05-07 11:54:07 -06:00
func ( c * cmdClient ) PublishDiagnostics ( ctx context . Context , p * protocol . PublishDiagnosticsParams ) error {
2020-01-10 15:54:47 -07:00
if p . URI == "gopls://diagnostics-done" {
close ( c . diagnosticsDone )
}
2019-11-20 12:26:02 -07:00
// Don't worry about diagnostics without versions.
if p . Version == 0 {
return nil
}
2019-05-07 11:54:07 -06:00
c . filesMu . Lock ( )
defer c . filesMu . Unlock ( )
2019-11-20 12:26:02 -07:00
2020-02-13 11:46:49 -07:00
file := c . getFile ( ctx , fileURI ( p . URI ) )
2019-05-07 11:54:07 -06:00
file . diagnostics = p . Diagnostics
return nil
2019-04-01 18:08:14 -06:00
}
2019-05-07 11:54:07 -06:00
func ( c * cmdClient ) getFile ( ctx context . Context , uri span . URI ) * cmdFile {
file , found := c . files [ uri ]
2019-08-01 14:31:02 -06:00
if ! found || file . err != nil {
2019-05-07 11:54:07 -06:00
file = & cmdFile {
2020-01-10 15:54:47 -07:00
uri : uri ,
2019-05-07 11:54:07 -06:00
}
c . files [ uri ] = file
2019-04-01 18:08:14 -06:00
}
2019-05-07 11:54:07 -06:00
if file . mapper == nil {
2019-06-06 11:51:00 -06:00
fname := uri . Filename ( )
2019-05-07 11:54:07 -06:00
content , err := ioutil . ReadFile ( fname )
if err != nil {
2019-08-06 13:13:11 -06:00
file . err = errors . Errorf ( "getFile: %v: %v" , uri , err )
2019-05-07 11:54:07 -06:00
return file
}
f := c . fset . AddFile ( fname , - 1 , len ( content ) )
f . SetLinesForContent ( content )
2019-09-09 22:36:39 -06:00
converter := span . NewContentConverter ( fname , content )
file . mapper = & protocol . ColumnMapper {
URI : uri ,
Converter : converter ,
Content : content ,
}
2019-04-01 18:08:14 -06:00
}
2019-05-07 11:54:07 -06:00
return file
}
func ( c * connection ) AddFile ( ctx context . Context , uri span . URI ) * cmdFile {
c . Client . filesMu . Lock ( )
defer c . Client . filesMu . Unlock ( )
2019-06-27 12:59:09 -06:00
2019-05-07 11:54:07 -06:00
file := c . Client . getFile ( ctx , uri )
2019-08-01 14:31:02 -06:00
// This should never happen.
if file == nil {
return & cmdFile {
uri : uri ,
err : fmt . Errorf ( "no file found for %s" , uri ) ,
}
}
2019-08-03 11:21:48 -06:00
if file . err != nil || file . added {
return file
}
file . added = true
2019-11-20 12:26:02 -07:00
p := & protocol . DidOpenTextDocumentParams {
TextDocument : protocol . TextDocumentItem {
2020-02-12 14:36:46 -07:00
URI : protocol . URIFromSpanURI ( uri ) ,
2019-11-20 12:26:02 -07:00
LanguageID : source . DetectLanguage ( "" , file . uri . Filename ( ) ) . String ( ) ,
Version : 1 ,
Text : string ( file . mapper . Content ) ,
} ,
}
2019-08-03 11:21:48 -06:00
if err := c . Server . DidOpen ( ctx , p ) ; err != nil {
2019-08-06 13:13:11 -06:00
file . err = errors . Errorf ( "%v: %v" , uri , err )
2019-04-01 18:08:14 -06:00
}
2019-05-07 11:54:07 -06:00
return file
2019-04-01 18:08:14 -06:00
}
2019-05-02 08:55:04 -06:00
2020-01-10 15:54:47 -07:00
func ( c * connection ) diagnoseFiles ( ctx context . Context , files [ ] span . URI ) error {
2020-01-27 12:51:22 -07:00
var untypedFiles [ ] interface { }
for _ , file := range files {
untypedFiles = append ( untypedFiles , string ( file ) )
}
2020-01-10 15:54:47 -07:00
c . Client . diagnosticsMu . Lock ( )
defer c . Client . diagnosticsMu . Unlock ( )
c . Client . diagnosticsDone = make ( chan struct { } )
2020-01-27 12:51:22 -07:00
_ , err := c . Server . NonstandardRequest ( ctx , "gopls/diagnoseFiles" , map [ string ] interface { } { "files" : untypedFiles } )
2020-01-10 15:54:47 -07:00
<- c . Client . diagnosticsDone
return err
}
2019-05-02 08:55:04 -06:00
func ( c * connection ) terminate ( ctx context . Context ) {
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
if strings . HasPrefix ( c . Client . app . Remote , "internal@" ) {
2019-05-02 08:55:04 -06:00
// internal connections need to be left alive for the next test
return
}
//TODO: do we need to handle errors on these calls?
c . Shutdown ( ctx )
//TODO: right now calling exit terminates the process, we should rethink that
//server.Exit(ctx)
}