MCPcopy
hub / github.com/golang/tools / RunGoModUpdateCommands

Method RunGoModUpdateCommands

gopls/internal/cache/snapshot.go:364–400  ·  view source on GitHub ↗

RunGoModUpdateCommands runs a series of `go` commands that updates the go.mod and go.sum file for wd, and returns their updated contents. TODO(rfindley): the signature of RunGoModUpdateCommands is very confusing, and is the only thing forcing the ModFlag and ModFile indirection. Simplify it.

(ctx context.Context, modURI protocol.DocumentURI, run func(invoke func(...string) (*bytes.Buffer, error)) error)

Source from the content-addressed store, hash-verified

362// and is the only thing forcing the ModFlag and ModFile indirection.
363// Simplify it.
364func (s *Snapshot) RunGoModUpdateCommands(ctx context.Context, modURI protocol.DocumentURI, run func(invoke func(...string) (*bytes.Buffer, error)) error) ([]byte, []byte, error) {
365 tempDir, cleanupModDir, err := TempModDir(ctx, s, modURI)
366 if err != nil {
367 return nil, nil, err
368 }
369 defer cleanupModDir()
370
371 // TODO(rfindley): we must use ModFlag and ModFile here (rather than simply
372 // setting Args), because without knowing the verb, we can't know whether
373 // ModFlag is appropriate. Refactor so that args can be set by the caller.
374 inv, cleanupInvocation, err := s.GoCommandInvocation(NetworkOK, modURI.DirPath(), "", nil, "GOWORK=off")
375 if err != nil {
376 return nil, nil, err
377 }
378 defer cleanupInvocation()
379
380 inv.ModFlag = "mod"
381 inv.ModFile = filepath.Join(tempDir, "go.mod")
382 invoke := func(args ...string) (*bytes.Buffer, error) {
383 inv.Verb = args[0]
384 inv.Args = args[1:]
385 return s.view.gocmdRunner.Run(ctx, *inv)
386 }
387 if err := run(invoke); err != nil {
388 return nil, nil, err
389 }
390 var modBytes, sumBytes []byte
391 modBytes, err = os.ReadFile(filepath.Join(tempDir, "go.mod"))
392 if err != nil && !os.IsNotExist(err) {
393 return nil, nil, err
394 }
395 sumBytes, err = os.ReadFile(filepath.Join(tempDir, "go.sum"))
396 if err != nil && !os.IsNotExist(err) {
397 return nil, nil, err
398 }
399 return modBytes, sumBytes, nil
400}
401
402// TempModDir creates a temporary directory with the contents of the provided
403// modURI, as well as its corresponding go.sum file, if it exists. On success,

Callers 1

Calls 6

GoCommandInvocationMethod · 0.95
TempModDirFunction · 0.85
DirPathMethod · 0.80
RunMethod · 0.65
ReadFileMethod · 0.65
runFunction · 0.50

Tested by

no test coverage detected