1
0
mirror of https://github.com/golang/go synced 2024-11-18 09:14:43 -07:00

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 <rstambler@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Heschi Kreinick <heschi@google.com>
This commit is contained in:
Rebecca Stambler 2020-06-21 23:21:15 -04:00
parent 4fd1c64487
commit 20e05c1c8f
5 changed files with 23 additions and 8 deletions

View File

@ -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

View File

@ -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 {

View File

@ -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),

View File

@ -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))
}

View File

@ -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)