Skip to content

Commit

Permalink
cmd/go: propagate Context arguments through modfetch methods
Browse files Browse the repository at this point in the history
For #56886.
For #38714.

Change-Id: I15c4a8673407d3423d7e203d645c6d0fb780d192
Reviewed-on: https://go-review.googlesource.com/c/go/+/452456
Reviewed-by: Michael Matloob <[email protected]>
TryBot-Result: Gopher Robot <[email protected]>
Auto-Submit: Bryan Mills <[email protected]>
Run-TryBot: Bryan Mills <[email protected]>
  • Loading branch information
Bryan C. Mills authored and gopherbot committed May 18, 2023
1 parent 72f448c commit ebfd80b
Show file tree
Hide file tree
Showing 24 changed files with 500 additions and 401 deletions.
21 changes: 21 additions & 0 deletions src/cmd/go/internal/cfg/cfg.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ package cfg

import (
"bytes"
"context"
"fmt"
"go/build"
"internal/buildcfg"
Expand Down Expand Up @@ -573,3 +574,23 @@ func gopath(ctxt build.Context) string {
GoPathError = fmt.Sprintf("%s is not set", env)
return ""
}

// WithBuildXWriter returns a Context in which BuildX output is written
// to given io.Writer.
func WithBuildXWriter(ctx context.Context, xLog io.Writer) context.Context {
return context.WithValue(ctx, buildXContextKey{}, xLog)
}

type buildXContextKey struct{}

// BuildXWriter returns nil if BuildX is false, or
// the writer to which BuildX output should be written otherwise.
func BuildXWriter(ctx context.Context) (io.Writer, bool) {
if !BuildX {
return nil, false
}
if v := ctx.Value(buildXContextKey{}); v != nil {
return v.(io.Writer), true
}
return os.Stderr, true
}
8 changes: 4 additions & 4 deletions src/cmd/go/internal/load/pkg.go
Original file line number Diff line number Diff line change
Expand Up @@ -2024,7 +2024,7 @@ func (p *Package) load(ctx context.Context, opts PackageOpts, path string, stk *
// Consider starting this as a background goroutine and retrieving the result
// asynchronously when we're actually ready to build the package, or when we
// actually need to evaluate whether the package's metadata is stale.
p.setBuildInfo(opts.AutoVCS)
p.setBuildInfo(ctx, opts.AutoVCS)
}

// If cgo is not enabled, ignore cgo supporting sources
Expand Down Expand Up @@ -2267,7 +2267,7 @@ var vcsStatusCache par.ErrCache[string, vcs.Status]
//
// Note that the GoVersion field is not set here to avoid encoding it twice.
// It is stored separately in the binary, mostly for historical reasons.
func (p *Package) setBuildInfo(autoVCS bool) {
func (p *Package) setBuildInfo(ctx context.Context, autoVCS bool) {
setPkgErrorf := func(format string, args ...any) {
if p.Error == nil {
p.Error = &PackageError{Err: fmt.Errorf(format, args...)}
Expand All @@ -2288,7 +2288,7 @@ func (p *Package) setBuildInfo(autoVCS bool) {
if mi.Replace != nil {
dm.Replace = debugModFromModinfo(mi.Replace)
} else if mi.Version != "" {
dm.Sum = modfetch.Sum(module.Version{Path: mi.Path, Version: mi.Version})
dm.Sum = modfetch.Sum(ctx, module.Version{Path: mi.Path, Version: mi.Version})
}
return dm
}
Expand Down Expand Up @@ -3280,7 +3280,7 @@ func PackagesAndErrorsOutsideModule(ctx context.Context, opts PackageOpts, args
return nil, fmt.Errorf("%s: %w", args[0], err)
}
rootMod := qrs[0].Mod
data, err := modfetch.GoMod(rootMod.Path, rootMod.Version)
data, err := modfetch.GoMod(ctx, rootMod.Path, rootMod.Version)
if err != nil {
return nil, fmt.Errorf("%s: %w", args[0], err)
}
Expand Down
8 changes: 4 additions & 4 deletions src/cmd/go/internal/modcmd/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -283,18 +283,18 @@ func runDownload(ctx context.Context, cmd *base.Command, args []string) {
// leaving the results (including any error) in m itself.
func DownloadModule(ctx context.Context, m *ModuleJSON) {
var err error
_, file, err := modfetch.InfoFile(m.Path, m.Version)
_, file, err := modfetch.InfoFile(ctx, m.Path, m.Version)
if err != nil {
m.Error = err.Error()
return
}
m.Info = file
m.GoMod, err = modfetch.GoModFile(m.Path, m.Version)
m.GoMod, err = modfetch.GoModFile(ctx, m.Path, m.Version)
if err != nil {
m.Error = err.Error()
return
}
m.GoModSum, err = modfetch.GoModSum(m.Path, m.Version)
m.GoModSum, err = modfetch.GoModSum(ctx, m.Path, m.Version)
if err != nil {
m.Error = err.Error()
return
Expand All @@ -305,7 +305,7 @@ func DownloadModule(ctx context.Context, m *ModuleJSON) {
m.Error = err.Error()
return
}
m.Sum = modfetch.Sum(mod)
m.Sum = modfetch.Sum(ctx, mod)
m.Dir, err = modfetch.Download(ctx, mod)
if err != nil {
m.Error = err.Error()
Expand Down
2 changes: 1 addition & 1 deletion src/cmd/go/internal/modcmd/edit.go
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ func runEdit(ctx context.Context, cmd *base.Command, args []string) {

// Make a best-effort attempt to acquire the side lock, only to exclude
// previous versions of the 'go' command from making simultaneous edits.
if unlock, err := modfetch.SideLock(); err == nil {
if unlock, err := modfetch.SideLock(ctx); err == nil {
defer unlock()
}

Expand Down
8 changes: 4 additions & 4 deletions src/cmd/go/internal/modcmd/verify.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func runVerify(ctx context.Context, cmd *base.Command, args []string) {
errsChans[i] = errsc
mod := mod // use a copy to avoid data races
go func() {
errsc <- verifyMod(mod)
errsc <- verifyMod(ctx, mod)
<-sem
}()
}
Expand All @@ -85,13 +85,13 @@ func runVerify(ctx context.Context, cmd *base.Command, args []string) {
}
}

func verifyMod(mod module.Version) []error {
func verifyMod(ctx context.Context, mod module.Version) []error {
var errs []error
zip, zipErr := modfetch.CachePath(mod, "zip")
zip, zipErr := modfetch.CachePath(ctx, mod, "zip")
if zipErr == nil {
_, zipErr = os.Stat(zip)
}
dir, dirErr := modfetch.DownloadDir(mod)
dir, dirErr := modfetch.DownloadDir(ctx, mod)
data, err := os.ReadFile(zip + "hash")
if err != nil {
if zipErr != nil && errors.Is(zipErr, fs.ErrNotExist) &&
Expand Down
Loading

0 comments on commit ebfd80b

Please sign in to comment.