From 20e05c1c8ffa075386be4ad638812b3baace6619 Mon Sep 17 00:00:00 2001 From: Rebecca Stambler Date: Sun, 21 Jun 2020 23:21:15 -0400 Subject: [PATCH] internal/lsp: don't use -modfile for `go mod` commands These are commands whose changes should be reflected in the existing go.mod file, as they do not provide edits. Add a third way of running the go command, explicitly without -modfile. Update the regression test accordingly. Change-Id: I866b5db83b504fae190e58c306c01a7a4296672d Reviewed-on: https://go-review.googlesource.com/c/tools/+/239200 Run-TryBot: Rebecca Stambler TryBot-Result: Gobot Gobot Reviewed-by: Heschi Kreinick --- internal/lsp/cache/snapshot.go | 6 ++++++ internal/lsp/command.go | 9 ++++----- internal/lsp/diagnostics.go | 2 +- internal/lsp/regtest/codelens_test.go | 5 +++++ internal/lsp/source/view.go | 9 +++++++-- 5 files changed, 23 insertions(+), 8 deletions(-) diff --git a/internal/lsp/cache/snapshot.go b/internal/lsp/cache/snapshot.go index 32f0358ea5..27d9612681 100644 --- a/internal/lsp/cache/snapshot.go +++ b/internal/lsp/cache/snapshot.go @@ -133,6 +133,12 @@ func (s *snapshot) config(ctx context.Context) *packages.Config { return cfg } +func (s *snapshot) RunGoCommandDirect(ctx context.Context, verb string, args []string) error { + cfg := s.config(ctx) + _, _, err := runGoCommand(ctx, cfg, nil, nil, verb, args) + return err +} + func (s *snapshot) RunGoCommand(ctx context.Context, verb string, args []string) (*bytes.Buffer, error) { cfg := s.config(ctx) var modFH, sumFH source.FileHandle diff --git a/internal/lsp/command.go b/internal/lsp/command.go index 6693e30f09..98262586c9 100644 --- a/internal/lsp/command.go +++ b/internal/lsp/command.go @@ -66,7 +66,7 @@ func (s *Server) executeCommand(ctx context.Context, params *protocol.ExecuteCom if params.Command == source.CommandVendor { arg = "vendor" } - err := s.goModCommand(ctx, uri, "mod", []string{arg}...) + err := s.directGoModCommand(ctx, uri, "mod", []string{arg}...) return nil, err case source.CommandUpgradeDependency: if len(params.Arguments) < 2 { @@ -74,19 +74,18 @@ func (s *Server) executeCommand(ctx context.Context, params *protocol.ExecuteCom } uri := protocol.DocumentURI(params.Arguments[0].(string)) deps := params.Arguments[1].(string) - err := s.goModCommand(ctx, uri, "get", strings.Split(deps, " ")...) + err := s.directGoModCommand(ctx, uri, "get", strings.Split(deps, " ")...) return nil, err } return nil, nil } -func (s *Server) goModCommand(ctx context.Context, uri protocol.DocumentURI, verb string, args ...string) error { +func (s *Server) directGoModCommand(ctx context.Context, uri protocol.DocumentURI, verb string, args ...string) error { view, err := s.session.ViewOf(uri.SpanURI()) if err != nil { return err } - _, err = view.Snapshot().RunGoCommand(ctx, verb, args) - return err + return view.Snapshot().RunGoCommandDirect(ctx, verb, args) } func (s *Server) runTest(ctx context.Context, snapshot source.Snapshot, funcName string) error { diff --git a/internal/lsp/diagnostics.go b/internal/lsp/diagnostics.go index 4c9fe2556a..a2e653f4f0 100644 --- a/internal/lsp/diagnostics.go +++ b/internal/lsp/diagnostics.go @@ -101,7 +101,7 @@ See https://github.com/golang/go/issues/39164 for more detail on this issue.`, if item == nil || err != nil { return nil, nil } - if err := s.goModCommand(ctx, protocol.URIFromSpanURI(modURI), "mod", []string{"vendor"}...); err != nil { + if err := s.directGoModCommand(ctx, protocol.URIFromSpanURI(modURI), "mod", []string{"vendor"}...); err != nil { return nil, &protocol.ShowMessageParams{ Type: protocol.Error, Message: fmt.Sprintf(`"go mod vendor" failed with %v`, err), diff --git a/internal/lsp/regtest/codelens_test.go b/internal/lsp/regtest/codelens_test.go index 42c5f74216..14f5befb74 100644 --- a/internal/lsp/regtest/codelens_test.go +++ b/internal/lsp/regtest/codelens_test.go @@ -100,6 +100,7 @@ func main() { ` runner.Run(t, shouldUpdateDep, func(t *testing.T, env *Env) { env.OpenFile("go.mod") + before := env.ReadWorkspaceFile("go.mod") lenses := env.CodeLens("go.mod") want := "Upgrade dependency to v1.3.3" var found *protocol.CodeLens @@ -117,6 +118,10 @@ func main() { }); err != nil { t.Fatal(err) } + after := env.ReadWorkspaceFile("go.mod") + if before == after { + t.Fatalf("go.mod file was unchanged by upgrade command") + } }, WithProxy(proxyWithLatest)) } diff --git a/internal/lsp/source/view.go b/internal/lsp/source/view.go index f66f82d7d7..64d64a2006 100644 --- a/internal/lsp/source/view.go +++ b/internal/lsp/source/view.go @@ -47,12 +47,17 @@ type Snapshot interface { Analyze(ctx context.Context, pkgID string, analyzers ...*analysis.Analyzer) ([]*Error, error) // RunGoCommandPiped runs the given `go` command in the view, using the - // provided stdout and stderr. + // provided stdout and stderr. It will use the -modfile flag, if possible. RunGoCommandPiped(ctx context.Context, verb string, args []string, stdout, stderr io.Writer) error - // RunGoCommand runs the given `go` command in the view. + // RunGoCommand runs the given `go` command in the view. It will use the + // -modfile flag, if possible. RunGoCommand(ctx context.Context, verb string, args []string) (*bytes.Buffer, error) + // RunGoCommandDirect runs the given `go` command, never using the + // -modfile flag. + RunGoCommandDirect(ctx context.Context, verb string, args []string) error + // ModTidyHandle returns a ModTidyHandle for the given go.mod file handle. // This function can have no data or error if there is no modfile detected. ModTidyHandle(ctx context.Context, fh FileHandle) (ModTidyHandle, error)