Skip to content

Commit 3510b4f

Browse files
committed
gopls/internal/lsp: simplify mod update commands
There's no reason not to use a temp modfile for these commands, since they can instead mutate modfiles via use ApplyEdits. We should not support two ways to make the same change. For golang/go#63537 Change-Id: I2f9abbddb544e6cbf4bc2f98efd83c8cba038645 Reviewed-on: https://go-review.googlesource.com/c/tools/+/542641 Reviewed-by: Alan Donovan <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]>
1 parent fdf06f2 commit 3510b4f

File tree

3 files changed

+13
-22
lines changed

3 files changed

+13
-22
lines changed

gopls/internal/lsp/cache/snapshot.go

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -474,21 +474,16 @@ func (s *Snapshot) RunGoCommandPiped(ctx context.Context, mode source.Invocation
474474
return s.view.gocmdRunner.RunPiped(ctx, *inv, stdout, stderr)
475475
}
476476

477-
// RunGoCommands runs a series of `go` commands that updates the go.mod
477+
// RunGoModUpdateCommands runs a series of `go` commands that updates the go.mod
478478
// and go.sum file for wd, and returns their updated contents.
479-
func (s *Snapshot) RunGoCommands(ctx context.Context, allowNetwork bool, wd string, run func(invoke func(...string) (*bytes.Buffer, error)) error) (bool, []byte, []byte, error) {
480-
var flags source.InvocationFlags
481-
if s.workspaceMode()&tempModfile != 0 {
482-
flags = source.WriteTemporaryModFile
483-
} else {
484-
flags = source.Normal
485-
}
486-
if allowNetwork {
487-
flags |= source.AllowNetwork
488-
}
479+
//
480+
// TODO(rfindley): the signature of RunGoModUpdateCommands is very confusing.
481+
// Simplify it.
482+
func (s *Snapshot) RunGoModUpdateCommands(ctx context.Context, wd string, run func(invoke func(...string) (*bytes.Buffer, error)) error) ([]byte, []byte, error) {
483+
flags := source.WriteTemporaryModFile | source.AllowNetwork
489484
tmpURI, inv, cleanup, err := s.goCommandInvocation(ctx, flags, &gocommand.Invocation{WorkingDir: wd})
490485
if err != nil {
491-
return false, nil, nil, err
486+
return nil, nil, err
492487
}
493488
defer cleanup()
494489
invoke := func(args ...string) (*bytes.Buffer, error) {
@@ -497,21 +492,21 @@ func (s *Snapshot) RunGoCommands(ctx context.Context, allowNetwork bool, wd stri
497492
return s.view.gocmdRunner.Run(ctx, *inv)
498493
}
499494
if err := run(invoke); err != nil {
500-
return false, nil, nil, err
495+
return nil, nil, err
501496
}
502497
if flags.Mode() != source.WriteTemporaryModFile {
503-
return false, nil, nil, nil
498+
return nil, nil, nil
504499
}
505500
var modBytes, sumBytes []byte
506501
modBytes, err = os.ReadFile(tmpURI.Path())
507502
if err != nil && !os.IsNotExist(err) {
508-
return false, nil, nil, err
503+
return nil, nil, err
509504
}
510505
sumBytes, err = os.ReadFile(strings.TrimSuffix(tmpURI.Path(), ".mod") + ".sum")
511506
if err != nil && !os.IsNotExist(err) {
512-
return false, nil, nil, err
507+
return nil, nil, err
513508
}
514-
return true, modBytes, sumBytes, nil
509+
return modBytes, sumBytes, nil
515510
}
516511

517512
// goCommandInvocation populates inv with configuration for running go commands on the snapshot.

gopls/internal/lsp/command.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -628,13 +628,10 @@ func (c *commandHandler) GoGetPackage(ctx context.Context, args command.GoGetPac
628628
}
629629

630630
func (s *server) runGoModUpdateCommands(ctx context.Context, snapshot *cache.Snapshot, uri protocol.DocumentURI, run func(invoke func(...string) (*bytes.Buffer, error)) error) error {
631-
tmpModfile, newModBytes, newSumBytes, err := snapshot.RunGoCommands(ctx, true, filepath.Dir(uri.Path()), run)
631+
newModBytes, newSumBytes, err := snapshot.RunGoModUpdateCommands(ctx, filepath.Dir(uri.Path()), run)
632632
if err != nil {
633633
return err
634634
}
635-
if !tmpModfile {
636-
return nil
637-
}
638635
modURI := snapshot.GoModForFile(uri)
639636
sumURI := protocol.URIFromPath(strings.TrimSuffix(modURI.Path(), ".mod") + ".sum")
640637
modEdits, err := collectFileEdits(ctx, snapshot, modURI, newModBytes)

gopls/internal/lsp/source/snapshot.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,6 @@ const (
282282
// LoadWorkspace is for packages.Load, and other operations that should
283283
// consider the whole workspace at once.
284284
LoadWorkspace
285-
286285
// AllowNetwork is a flag bit that indicates the invocation should be
287286
// allowed to access the network.
288287
AllowNetwork InvocationFlags = 1 << 10

0 commit comments

Comments
 (0)