2019-09-18 21:51:24 -06:00
|
|
|
// Copyright 2019 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 cmdtest contains the test suite for the command line behavior of gopls.
|
|
|
|
package cmdtest
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
2019-11-21 17:58:38 -07:00
|
|
|
"fmt"
|
2020-02-11 08:12:40 -07:00
|
|
|
"io"
|
2019-09-18 21:51:24 -06:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
2020-02-11 08:12:40 -07:00
|
|
|
"sync"
|
2019-09-18 21:51:24 -06:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"golang.org/x/tools/go/packages/packagestest"
|
2020-04-27 10:12:05 -06:00
|
|
|
"golang.org/x/tools/internal/jsonrpc2/servertest"
|
|
|
|
"golang.org/x/tools/internal/lsp/cache"
|
2019-11-21 17:58:38 -07:00
|
|
|
"golang.org/x/tools/internal/lsp/cmd"
|
2020-04-27 10:12:05 -06:00
|
|
|
"golang.org/x/tools/internal/lsp/debug"
|
|
|
|
"golang.org/x/tools/internal/lsp/lsprpc"
|
2020-01-05 03:46:20 -07:00
|
|
|
"golang.org/x/tools/internal/lsp/protocol"
|
2019-09-26 11:56:23 -06:00
|
|
|
"golang.org/x/tools/internal/lsp/source"
|
2019-09-18 21:51:24 -06:00
|
|
|
"golang.org/x/tools/internal/lsp/tests"
|
2019-09-26 11:56:23 -06:00
|
|
|
"golang.org/x/tools/internal/span"
|
2019-11-21 17:58:38 -07:00
|
|
|
"golang.org/x/tools/internal/tool"
|
2019-09-18 21:51:24 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
type runner struct {
|
2019-11-21 17:58:38 -07:00
|
|
|
exporter packagestest.Exporter
|
|
|
|
data *tests.Data
|
|
|
|
ctx context.Context
|
|
|
|
options func(*source.Options)
|
|
|
|
normalizers []normalizer
|
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 string
|
2019-11-21 17:58:38 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
type normalizer struct {
|
|
|
|
path string
|
|
|
|
slashed string
|
|
|
|
escaped string
|
|
|
|
fragment string
|
|
|
|
}
|
|
|
|
|
2020-04-27 10:12:05 -06:00
|
|
|
func TestCommandLine(testdata string, options func(*source.Options)) func(*testing.T, packagestest.Exporter) {
|
|
|
|
return func(t *testing.T, exporter packagestest.Exporter) {
|
|
|
|
if stat, err := os.Stat(testdata); err != nil || !stat.IsDir() {
|
|
|
|
t.Skip("testdata directory not present")
|
|
|
|
}
|
|
|
|
ctx := tests.Context(t)
|
|
|
|
ts := NewTestServer(ctx, options)
|
|
|
|
data := tests.Load(t, exporter, testdata)
|
|
|
|
for _, datum := range data {
|
|
|
|
defer datum.Exported.Cleanup()
|
|
|
|
t.Run(tests.FormatFolderName(datum.Folder), func(t *testing.T) {
|
|
|
|
t.Helper()
|
|
|
|
tests.Run(t, NewRunner(exporter, datum, ctx, ts.Addr, options), datum)
|
|
|
|
})
|
|
|
|
}
|
2020-06-17 19:45:33 -06:00
|
|
|
cmd.CloseTestConnections(ctx)
|
2020-04-27 10:12:05 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewTestServer(ctx context.Context, options func(*source.Options)) *servertest.TCPServer {
|
|
|
|
ctx = debug.WithInstance(ctx, "", "")
|
|
|
|
cache := cache.New(ctx, options)
|
2020-07-08 11:49:07 -06:00
|
|
|
ss := lsprpc.NewStreamServer(cache, false)
|
2020-04-30 09:05:58 -06:00
|
|
|
return servertest.NewTCPServer(ctx, ss, nil)
|
2020-04-27 10:12:05 -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
|
|
|
func NewRunner(exporter packagestest.Exporter, data *tests.Data, ctx context.Context, remote string, options func(*source.Options)) *runner {
|
2019-11-21 17:58:38 -07:00
|
|
|
r := &runner{
|
|
|
|
exporter: exporter,
|
|
|
|
data: data,
|
|
|
|
ctx: ctx,
|
|
|
|
options: options,
|
|
|
|
normalizers: make([]normalizer, 0, len(data.Exported.Modules)),
|
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: remote,
|
2019-09-18 21:51:24 -06:00
|
|
|
}
|
2019-11-21 17:58:38 -07:00
|
|
|
// build the path normalizing patterns
|
|
|
|
for _, m := range data.Exported.Modules {
|
|
|
|
for fragment := range m.Files {
|
|
|
|
n := normalizer{
|
|
|
|
path: data.Exported.File(m.Name, fragment),
|
|
|
|
fragment: fragment,
|
|
|
|
}
|
|
|
|
if n.slashed = filepath.ToSlash(n.path); n.slashed == n.path {
|
|
|
|
n.slashed = ""
|
|
|
|
}
|
|
|
|
quoted := strconv.Quote(n.path)
|
|
|
|
if n.escaped = quoted[1 : len(quoted)-1]; n.escaped == n.path {
|
|
|
|
n.escaped = ""
|
|
|
|
}
|
|
|
|
r.normalizers = append(r.normalizers, n)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return r
|
2019-09-18 21:51:24 -06:00
|
|
|
}
|
|
|
|
|
2020-02-26 12:11:30 -07:00
|
|
|
func (r *runner) CodeLens(t *testing.T, uri span.URI, want []protocol.CodeLens) {
|
2020-02-06 12:50:26 -07:00
|
|
|
//TODO: add command line completions tests when it works
|
|
|
|
}
|
|
|
|
|
2019-09-26 11:56:23 -06:00
|
|
|
func (r *runner) Completion(t *testing.T, src span.Span, test tests.Completion, items tests.CompletionItems) {
|
2019-09-17 09:10:48 -06:00
|
|
|
//TODO: add command line completions tests when it works
|
|
|
|
}
|
|
|
|
|
2019-09-26 11:56:23 -06:00
|
|
|
func (r *runner) CompletionSnippet(t *testing.T, src span.Span, expected tests.CompletionSnippet, placeholders bool, items tests.CompletionItems) {
|
2019-09-17 09:10:48 -06:00
|
|
|
//TODO: add command line completions tests when it works
|
|
|
|
}
|
|
|
|
|
2019-09-26 11:56:23 -06:00
|
|
|
func (r *runner) UnimportedCompletion(t *testing.T, src span.Span, test tests.Completion, items tests.CompletionItems) {
|
2019-09-17 09:10:48 -06:00
|
|
|
//TODO: add command line completions tests when it works
|
|
|
|
}
|
|
|
|
|
2019-09-26 11:56:23 -06:00
|
|
|
func (r *runner) DeepCompletion(t *testing.T, src span.Span, test tests.Completion, items tests.CompletionItems) {
|
2019-09-17 09:10:48 -06:00
|
|
|
//TODO: add command line completions tests when it works
|
|
|
|
}
|
|
|
|
|
2019-09-26 11:56:23 -06:00
|
|
|
func (r *runner) FuzzyCompletion(t *testing.T, src span.Span, test tests.Completion, items tests.CompletionItems) {
|
2019-09-17 09:10:48 -06:00
|
|
|
//TODO: add command line completions tests when it works
|
|
|
|
}
|
|
|
|
|
2019-09-26 11:56:23 -06:00
|
|
|
func (r *runner) CaseSensitiveCompletion(t *testing.T, src span.Span, test tests.Completion, items tests.CompletionItems) {
|
2019-09-26 05:59:06 -06:00
|
|
|
//TODO: add command line completions tests when it works
|
|
|
|
}
|
|
|
|
|
2019-09-26 11:56:23 -06:00
|
|
|
func (r *runner) RankCompletion(t *testing.T, src span.Span, test tests.Completion, items tests.CompletionItems) {
|
2019-09-18 21:51:24 -06:00
|
|
|
//TODO: add command line completions tests when it works
|
|
|
|
}
|
|
|
|
|
2020-07-10 10:13:40 -06:00
|
|
|
func (r *runner) FunctionExtraction(t *testing.T, start span.Span, end span.Span) {
|
|
|
|
//TODO: function extraction not supported on command line
|
|
|
|
}
|
|
|
|
|
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
|
|
|
func (r *runner) runGoplsCmd(t testing.TB, args ...string) (string, string) {
|
2019-11-21 17:58:38 -07:00
|
|
|
rStdout, wStdout, err := os.Pipe()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
oldStdout := os.Stdout
|
|
|
|
rStderr, wStderr, err := os.Pipe()
|
2019-09-18 21:51:24 -06:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2019-11-21 17:58:38 -07:00
|
|
|
oldStderr := os.Stderr
|
2020-02-11 08:12:40 -07:00
|
|
|
stdout, stderr := &bytes.Buffer{}, &bytes.Buffer{}
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
wg.Add(2)
|
|
|
|
go func() {
|
|
|
|
io.Copy(stdout, rStdout)
|
|
|
|
wg.Done()
|
2019-09-18 21:51:24 -06:00
|
|
|
}()
|
2020-02-11 08:12:40 -07:00
|
|
|
go func() {
|
|
|
|
io.Copy(stderr, rStderr)
|
|
|
|
wg.Done()
|
|
|
|
}()
|
|
|
|
os.Stdout, os.Stderr = wStdout, wStderr
|
2019-11-20 12:26:02 -07:00
|
|
|
app := cmd.New("gopls-test", r.data.Config.Dir, r.data.Exported.Config.Env, r.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
|
|
|
remote := r.remote
|
2019-11-21 17:58:38 -07:00
|
|
|
err = tool.Run(tests.Context(t),
|
2019-11-20 12:26:02 -07:00
|
|
|
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
|
|
|
append([]string{fmt.Sprintf("-remote=internal@%s", remote)}, args...))
|
2019-11-21 17:58:38 -07:00
|
|
|
if err != nil {
|
|
|
|
fmt.Fprint(os.Stderr, err)
|
|
|
|
}
|
|
|
|
wStdout.Close()
|
|
|
|
wStderr.Close()
|
2020-02-11 08:12:40 -07:00
|
|
|
wg.Wait()
|
|
|
|
os.Stdout, os.Stderr = oldStdout, oldStderr
|
|
|
|
rStdout.Close()
|
|
|
|
rStderr.Close()
|
|
|
|
return stdout.String(), stderr.String()
|
2019-11-21 17:58:38 -07:00
|
|
|
}
|
|
|
|
|
2020-01-21 14:01:59 -07:00
|
|
|
// NormalizeGoplsCmd runs the gopls command and normalizes its output.
|
2019-11-21 17:58:38 -07:00
|
|
|
func (r *runner) NormalizeGoplsCmd(t testing.TB, args ...string) (string, string) {
|
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
|
|
|
stdout, stderr := r.runGoplsCmd(t, args...)
|
2019-11-21 17:58:38 -07:00
|
|
|
return r.Normalize(stdout), r.Normalize(stderr)
|
|
|
|
}
|
|
|
|
|
|
|
|
// NormalizePrefix normalizes a single path at the front of the input string.
|
|
|
|
func (r *runner) NormalizePrefix(s string) string {
|
|
|
|
for _, n := range r.normalizers {
|
|
|
|
if t := strings.TrimPrefix(s, n.path); t != s {
|
|
|
|
return n.fragment + t
|
|
|
|
}
|
|
|
|
if t := strings.TrimPrefix(s, n.slashed); t != s {
|
|
|
|
return n.fragment + t
|
|
|
|
}
|
|
|
|
if t := strings.TrimPrefix(s, n.escaped); t != s {
|
|
|
|
return n.fragment + t
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return s
|
2019-09-18 21:51:24 -06:00
|
|
|
}
|
|
|
|
|
2019-11-21 17:58:38 -07:00
|
|
|
// Normalize replaces all paths present in s with just the fragment portion
|
2019-09-18 21:51:24 -06:00
|
|
|
// this is used to make golden files not depend on the temporary paths of the files
|
2019-11-21 17:58:38 -07:00
|
|
|
func (r *runner) Normalize(s string) string {
|
2019-09-18 21:51:24 -06:00
|
|
|
type entry struct {
|
|
|
|
path string
|
|
|
|
index int
|
|
|
|
fragment string
|
|
|
|
}
|
2019-11-21 17:58:38 -07:00
|
|
|
match := make([]entry, 0, len(r.normalizers))
|
2019-09-18 21:51:24 -06:00
|
|
|
// collect the initial state of all the matchers
|
2019-11-21 17:58:38 -07:00
|
|
|
for _, n := range r.normalizers {
|
|
|
|
index := strings.Index(s, n.path)
|
|
|
|
if index >= 0 {
|
|
|
|
match = append(match, entry{n.path, index, n.fragment})
|
|
|
|
}
|
|
|
|
if n.slashed != "" {
|
|
|
|
index := strings.Index(s, n.slashed)
|
2019-09-18 21:51:24 -06:00
|
|
|
if index >= 0 {
|
2019-11-21 17:58:38 -07:00
|
|
|
match = append(match, entry{n.slashed, index, n.fragment})
|
2019-09-18 21:51:24 -06:00
|
|
|
}
|
2019-11-21 17:58:38 -07:00
|
|
|
}
|
|
|
|
if n.escaped != "" {
|
|
|
|
index := strings.Index(s, n.escaped)
|
|
|
|
if index >= 0 {
|
|
|
|
match = append(match, entry{n.escaped, index, n.fragment})
|
2019-09-18 21:51:24 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// result should be the same or shorter than the input
|
|
|
|
buf := bytes.NewBuffer(make([]byte, 0, len(s)))
|
|
|
|
last := 0
|
|
|
|
for {
|
|
|
|
// find the nearest path match to the start of the buffer
|
|
|
|
next := -1
|
|
|
|
nearest := len(s)
|
|
|
|
for i, c := range match {
|
|
|
|
if c.index >= 0 && nearest > c.index {
|
|
|
|
nearest = c.index
|
|
|
|
next = i
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// if there are no matches, we copy the rest of the string and are done
|
|
|
|
if next < 0 {
|
|
|
|
buf.WriteString(s[last:])
|
|
|
|
return buf.String()
|
|
|
|
}
|
|
|
|
// we have a match
|
|
|
|
n := &match[next]
|
|
|
|
// copy up to the start of the match
|
|
|
|
buf.WriteString(s[last:n.index])
|
|
|
|
// skip over the filename
|
|
|
|
last = n.index + len(n.path)
|
|
|
|
// add in the fragment instead
|
|
|
|
buf.WriteString(n.fragment)
|
|
|
|
// see what the next match for this path is
|
|
|
|
n.index = strings.Index(s[last:], n.path)
|
|
|
|
if n.index >= 0 {
|
|
|
|
n.index += last
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|