2020-03-10 22:49:10 -06:00
|
|
|
// Copyright 2020 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-09-18 23:21:54 -06:00
|
|
|
package lsp
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2020-05-05 12:43:51 -06:00
|
|
|
"io"
|
|
|
|
"path/filepath"
|
2020-02-26 12:11:30 -07:00
|
|
|
"strings"
|
2019-09-18 23:21:54 -06:00
|
|
|
|
2020-05-05 12:43:51 -06:00
|
|
|
"golang.org/x/tools/internal/event"
|
2020-02-24 14:46:08 -07:00
|
|
|
"golang.org/x/tools/internal/gocommand"
|
2020-05-05 12:43:51 -06:00
|
|
|
"golang.org/x/tools/internal/lsp/debug/tag"
|
2019-09-18 23:21:54 -06:00
|
|
|
"golang.org/x/tools/internal/lsp/protocol"
|
|
|
|
"golang.org/x/tools/internal/lsp/source"
|
2020-03-23 06:35:36 -06:00
|
|
|
"golang.org/x/tools/internal/packagesinternal"
|
2020-03-10 22:49:10 -06:00
|
|
|
"golang.org/x/tools/internal/xcontext"
|
2019-09-18 23:21:54 -06:00
|
|
|
errors "golang.org/x/xerrors"
|
|
|
|
)
|
|
|
|
|
|
|
|
func (s *Server) executeCommand(ctx context.Context, params *protocol.ExecuteCommandParams) (interface{}, error) {
|
|
|
|
switch params.Command {
|
2020-05-05 12:43:51 -06:00
|
|
|
case source.CommandTest:
|
2020-06-02 08:57:20 -06:00
|
|
|
unsaved := false
|
|
|
|
for _, overlay := range s.session.Overlays() {
|
|
|
|
if !overlay.Saved() {
|
|
|
|
unsaved = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if unsaved {
|
2020-05-05 12:43:51 -06:00
|
|
|
return nil, s.client.ShowMessage(ctx, &protocol.ShowMessageParams{
|
|
|
|
Type: protocol.Error,
|
|
|
|
Message: "could not run tests, there are unsaved files in the view",
|
|
|
|
})
|
|
|
|
}
|
|
|
|
funcName, uri, err := getRunTestArguments(params.Arguments)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
internal/lsp: read files eagerly
We use file identities pervasively throughout gopls. Prior to this
change, the identity is the modification date of an unopened file, or
the hash of an opened file. That means that opening a file changes its
identity, which causes unnecessary churn in the cache.
Unfortunately, there isn't an easy way to fix this. Changing the
cache key to something else, such as the modification time, means that
we won't unify cache entries if a change is made and then undone. The
approach here is to read files eagerly in GetFile, so that we know their
hashes immediately. That resolves the churn, but means that we do a ton
of file IO at startup.
Incidental changes:
Remove the FileSystem interface; there was only one implementation and
it added a fair amount of cruft. We have many other places that assume
os.Stat and such work.
Add direct accessors to FileHandle for URI, Kind, and Version. Most uses
of (FileHandle).Identity were for stuff that we derive solely from the
URI, and this helped me disentangle them. It is a *ton* of churn,
though. I can revert it if you want.
Change-Id: Ia2133bc527f71daf81c9d674951726a232ca5bc9
Reviewed-on: https://go-review.googlesource.com/c/tools/+/237037
Run-TryBot: Heschi Kreinick <heschi@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Rebecca Stambler <rstambler@golang.org>
2020-06-08 13:21:24 -06:00
|
|
|
snapshot, fh, ok, err := s.beginFileRequest(ctx, protocol.DocumentURI(uri), source.Go)
|
2020-05-05 12:43:51 -06:00
|
|
|
if !ok {
|
|
|
|
return nil, err
|
|
|
|
}
|
internal/lsp: read files eagerly
We use file identities pervasively throughout gopls. Prior to this
change, the identity is the modification date of an unopened file, or
the hash of an opened file. That means that opening a file changes its
identity, which causes unnecessary churn in the cache.
Unfortunately, there isn't an easy way to fix this. Changing the
cache key to something else, such as the modification time, means that
we won't unify cache entries if a change is made and then undone. The
approach here is to read files eagerly in GetFile, so that we know their
hashes immediately. That resolves the churn, but means that we do a ton
of file IO at startup.
Incidental changes:
Remove the FileSystem interface; there was only one implementation and
it added a fair amount of cruft. We have many other places that assume
os.Stat and such work.
Add direct accessors to FileHandle for URI, Kind, and Version. Most uses
of (FileHandle).Identity were for stuff that we derive solely from the
URI, and this helped me disentangle them. It is a *ton* of churn,
though. I can revert it if you want.
Change-Id: Ia2133bc527f71daf81c9d674951726a232ca5bc9
Reviewed-on: https://go-review.googlesource.com/c/tools/+/237037
Run-TryBot: Heschi Kreinick <heschi@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Rebecca Stambler <rstambler@golang.org>
2020-06-08 13:21:24 -06:00
|
|
|
dir := filepath.Dir(fh.URI().Filename())
|
2020-05-05 12:43:51 -06:00
|
|
|
go s.runTest(ctx, funcName, dir, snapshot)
|
2020-05-06 20:54:50 -06:00
|
|
|
case source.CommandGenerate:
|
2020-03-10 22:49:10 -06:00
|
|
|
dir, recursive, err := getGenerateRequest(params.Arguments)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
go s.runGenerate(xcontext.Detach(ctx), dir, recursive)
|
2020-05-14 12:02:48 -06:00
|
|
|
case source.CommandRegenerateCgo:
|
|
|
|
mod := source.FileModification{
|
|
|
|
URI: protocol.DocumentURI(params.Arguments[0].(string)).SpanURI(),
|
|
|
|
Action: source.InvalidateMetadata,
|
|
|
|
}
|
|
|
|
_, err := s.didModifyFiles(ctx, []source.FileModification{mod}, FromRegenerateCgo)
|
|
|
|
return nil, err
|
2020-05-28 19:21:29 -06:00
|
|
|
case source.CommandTidy, source.CommandVendor:
|
2019-09-18 23:21:54 -06:00
|
|
|
if len(params.Arguments) == 0 || len(params.Arguments) > 1 {
|
2020-05-28 19:21:29 -06:00
|
|
|
return nil, errors.Errorf("expected 1 argument, got %v", params.Arguments)
|
2019-09-18 23:21:54 -06:00
|
|
|
}
|
2020-02-13 11:46:49 -07:00
|
|
|
uri := protocol.DocumentURI(params.Arguments[0].(string))
|
2020-05-28 19:21:29 -06:00
|
|
|
|
|
|
|
// The flow for `go mod tidy` and `go mod vendor` is almost identical,
|
|
|
|
// so we combine them into one case for convenience.
|
2020-06-07 14:04:51 -06:00
|
|
|
arg := "tidy"
|
2020-05-28 19:21:29 -06:00
|
|
|
if params.Command == source.CommandVendor {
|
2020-06-07 14:04:51 -06:00
|
|
|
arg = "vendor"
|
2019-09-18 23:21:54 -06:00
|
|
|
}
|
2020-06-07 14:04:51 -06:00
|
|
|
err := s.goModCommand(ctx, uri, "mod", []string{arg}...)
|
2020-05-28 19:21:29 -06:00
|
|
|
return nil, err
|
2020-05-06 20:54:50 -06:00
|
|
|
case source.CommandUpgradeDependency:
|
2020-02-06 12:50:26 -07:00
|
|
|
if len(params.Arguments) < 2 {
|
2020-05-28 19:21:29 -06:00
|
|
|
return nil, errors.Errorf("expected 2 arguments, got %v", params.Arguments)
|
2020-02-06 12:50:26 -07:00
|
|
|
}
|
2020-02-13 11:46:49 -07:00
|
|
|
uri := protocol.DocumentURI(params.Arguments[0].(string))
|
2020-02-26 12:11:30 -07:00
|
|
|
deps := params.Arguments[1].(string)
|
2020-05-28 19:21:29 -06:00
|
|
|
err := s.goModCommand(ctx, uri, "get", strings.Split(deps, " ")...)
|
|
|
|
return nil, err
|
2019-09-18 23:21:54 -06:00
|
|
|
}
|
|
|
|
return nil, nil
|
|
|
|
}
|
2020-03-10 22:49:10 -06:00
|
|
|
|
2020-05-28 19:21:29 -06:00
|
|
|
func (s *Server) goModCommand(ctx context.Context, uri protocol.DocumentURI, verb string, args ...string) error {
|
|
|
|
view, err := s.session.ViewOf(uri.SpanURI())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
snapshot := view.Snapshot()
|
|
|
|
cfg := snapshot.Config(ctx)
|
|
|
|
inv := gocommand.Invocation{
|
2020-06-07 14:04:51 -06:00
|
|
|
Verb: verb,
|
|
|
|
Args: args,
|
2020-05-28 19:21:29 -06:00
|
|
|
Env: cfg.Env,
|
|
|
|
WorkingDir: view.Folder().Filename(),
|
|
|
|
}
|
|
|
|
gocmdRunner := packagesinternal.GetGoCmdRunner(cfg)
|
|
|
|
_, err = gocmdRunner.Run(ctx, inv)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-05-05 12:43:51 -06:00
|
|
|
func (s *Server) runTest(ctx context.Context, funcName string, dir string, snapshot source.Snapshot) {
|
|
|
|
args := []string{"-run", funcName, dir}
|
|
|
|
inv := gocommand.Invocation{
|
|
|
|
Verb: "test",
|
|
|
|
Args: args,
|
|
|
|
Env: snapshot.Config(ctx).Env,
|
|
|
|
WorkingDir: dir,
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx, cancel := context.WithCancel(ctx)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
er := &eventWriter{ctx: ctx, operation: "test"}
|
|
|
|
wc := s.newProgressWriter(ctx, "test", "running "+funcName, cancel)
|
|
|
|
defer wc.Close()
|
|
|
|
|
|
|
|
messageType := protocol.Info
|
|
|
|
message := "test passed"
|
|
|
|
stderr := io.MultiWriter(er, wc)
|
|
|
|
if err := inv.RunPiped(ctx, er, stderr); err != nil {
|
|
|
|
event.Error(ctx, "test: command error", err, tag.Directory.Of(dir))
|
2020-05-28 18:38:19 -06:00
|
|
|
if !errors.Is(err, context.Canceled) {
|
2020-05-05 12:43:51 -06:00
|
|
|
messageType = protocol.Error
|
|
|
|
message = "test failed"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
s.client.ShowMessage(ctx, &protocol.ShowMessageParams{
|
|
|
|
Type: messageType,
|
|
|
|
Message: message,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func getRunTestArguments(args []interface{}) (string, string, error) {
|
|
|
|
if len(args) != 2 {
|
|
|
|
return "", "", errors.Errorf("expected one test func name and one file path, got %v", args)
|
|
|
|
}
|
|
|
|
|
|
|
|
funcName, ok := args[0].(string)
|
|
|
|
if !ok {
|
|
|
|
return "", "", errors.Errorf("expected func name to be a string, got %T", args[0])
|
|
|
|
}
|
|
|
|
|
|
|
|
file, ok := args[1].(string)
|
|
|
|
if !ok {
|
|
|
|
return "", "", errors.Errorf("expected file to be a string, got %T", args[1])
|
|
|
|
}
|
|
|
|
|
|
|
|
return funcName, file, nil
|
|
|
|
}
|
|
|
|
|
2020-03-10 22:49:10 -06:00
|
|
|
func getGenerateRequest(args []interface{}) (string, bool, error) {
|
|
|
|
if len(args) != 2 {
|
|
|
|
return "", false, errors.Errorf("expected exactly 2 arguments but got %d", len(args))
|
|
|
|
}
|
|
|
|
dir, ok := args[0].(string)
|
|
|
|
if !ok {
|
|
|
|
return "", false, errors.Errorf("expected dir to be a string value but got %T", args[0])
|
|
|
|
}
|
|
|
|
recursive, ok := args[1].(bool)
|
|
|
|
if !ok {
|
|
|
|
return "", false, errors.Errorf("expected recursive to be a boolean but got %T", args[1])
|
|
|
|
}
|
|
|
|
return dir, recursive, nil
|
|
|
|
}
|