internal/lsp: correctly apply file edits for edit go directive

Fixes a bug where the go mod edit -go=version file edits
are never actually applied.

Change-Id: Id055b0fad3399a00e1245556eb39073f15be09d9
Reviewed-on: https://go-review.googlesource.com/c/tools/+/389216
Trust: Suzy Mueller <suzmue@golang.org>
Run-TryBot: Suzy Mueller <suzmue@golang.org>
gopls-CI: kokoro <noreply+kokoro@google.com>
Reviewed-by: Robert Findley <rfindley@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
This commit is contained in:
Suzy Mueller 2022-03-02 11:22:35 -07:00
parent 6a6eb596e7
commit 4a5e7f0db6
1 changed files with 12 additions and 6 deletions

View File

@ -266,12 +266,18 @@ func (c *commandHandler) EditGoDirective(ctx context.Context, args command.EditG
requireSave: true, // if go.mod isn't saved it could cause a problem
forURI: args.URI,
}, func(ctx context.Context, deps commandDeps) error {
_, err := deps.snapshot.RunGoCommandDirect(ctx, source.Normal, &gocommand.Invocation{
Verb: "mod",
Args: []string{"edit", "-go", args.Version},
WorkingDir: filepath.Dir(args.URI.SpanURI().Filename()),
})
return err
snapshot, fh, ok, release, err := c.s.beginFileRequest(ctx, args.URI, source.UnknownKind)
defer release()
if !ok {
return err
}
if err := c.s.runGoModUpdateCommands(ctx, snapshot, fh.URI(), func(invoke func(...string) (*bytes.Buffer, error)) error {
_, err := invoke("mod", "edit", "-go", args.Version)
return err
}); err != nil {
return err
}
return nil
})
}