cmd/go: inject State parameter into list.runList

This command modifies the call tree starting at `list.runList` to
inject a `State` parameter to every function that is currently using
the global `modload.LoaderState` variable.  By explicilty passing a
`State` parameter, we can begin to eliminate the usage of the global
`modload.LoaderState`.

This commit is part of the overall effort to eliminate global
modloader state.

[git-generate]
cd src/cmd/go/internal/list
rf 'inject modload.LoaderState runList'
cd ..
./rf-cleanup.zsh

Change-Id: I7274bc3dc6779bd8306fb79c158aa6f0473827a1
Reviewed-on: https://go-review.googlesource.com/c/go/+/709979
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Michael Matloob <matloob@google.com>
Reviewed-by: Michael Matloob <matloob@golang.org>
This commit is contained in:
Ian Alexander 2025-09-30 21:40:22 -04:00
parent 29a81624f7
commit 35e8309be2
12 changed files with 67 additions and 67 deletions

View file

@ -479,7 +479,7 @@ func runList(ctx context.Context, cmd *base.Command, args []string) {
fm := template.FuncMap{
"join": strings.Join,
"context": context,
"module": func(path string) *modinfo.ModulePublic { return modload.ModuleInfo(ctx, path) },
"module": func(path string) *modinfo.ModulePublic { return modload.ModuleInfo(modload.LoaderState, ctx, path) },
}
tmpl, err := template.New("main").Funcs(fm).Parse(*listFmt)
if err != nil {
@ -569,7 +569,7 @@ func runList(ctx context.Context, cmd *base.Command, args []string) {
if *listReuse != "" && len(args) == 0 {
base.Fatalf("go: list -m -reuse only has an effect with module@version arguments")
}
mods, err := modload.ListModules(ctx, args, mode, *listReuse)
mods, err := modload.ListModules(modload.LoaderState, ctx, args, mode, *listReuse)
if !*listE {
for _, m := range mods {
if m.Error != nil {
@ -648,10 +648,10 @@ func runList(ctx context.Context, cmd *base.Command, args []string) {
sema.Release(1)
wg.Done()
}
pmain, ptest, pxtest = load.TestPackagesAndErrors(ctx, done, pkgOpts, p, nil)
pmain, ptest, pxtest = load.TestPackagesAndErrors(modload.LoaderState, ctx, done, pkgOpts, p, nil)
} else {
var perr *load.Package
pmain, ptest, pxtest, perr = load.TestPackagesFor(ctx, pkgOpts, p, nil)
pmain, ptest, pxtest, perr = load.TestPackagesFor(modload.LoaderState, ctx, pkgOpts, p, nil)
if perr != nil {
base.Fatalf("go: can't load test package: %s", perr.Error)
}
@ -733,7 +733,7 @@ func runList(ctx context.Context, cmd *base.Command, args []string) {
// TODO: Use pkgsFilter?
for _, p := range pkgs {
if len(p.GoFiles)+len(p.CgoFiles) > 0 {
a.Deps = append(a.Deps, b.AutoAction(work.ModeInstall, work.ModeInstall, p))
a.Deps = append(a.Deps, b.AutoAction(modload.LoaderState, work.ModeInstall, work.ModeInstall, p))
}
}
b.Do(ctx, a)
@ -741,8 +741,8 @@ func runList(ctx context.Context, cmd *base.Command, args []string) {
for _, p := range pkgs {
// Show vendor-expanded paths in listing
p.TestImports = p.Resolve(p.TestImports)
p.XTestImports = p.Resolve(p.XTestImports)
p.TestImports = p.Resolve(modload.LoaderState, p.TestImports)
p.XTestImports = p.Resolve(modload.LoaderState, p.XTestImports)
p.DepOnly = !cmdline[p]
if *listCompiled {
@ -850,7 +850,7 @@ func runList(ctx context.Context, cmd *base.Command, args []string) {
if *listRetracted {
mode |= modload.ListRetracted
}
rmods, err := modload.ListModules(ctx, args, mode, *listReuse)
rmods, err := modload.ListModules(modload.LoaderState, ctx, args, mode, *listReuse)
if err != nil && !*listE {
base.Error(err)
}

View file

@ -355,14 +355,14 @@ func (p *Package) setLoadPackageDataError(err error, path string, stk *ImportSta
// can produce better error messages if it starts with the original paths.
// The initial load of p loads all the non-test imports and rewrites
// the vendored paths, so nothing should ever call p.vendored(p.Imports).
func (p *Package) Resolve(imports []string) []string {
func (p *Package) Resolve(loaderstate *modload.State, imports []string) []string {
if len(imports) > 0 && len(p.Imports) > 0 && &imports[0] == &p.Imports[0] {
panic("internal error: p.Resolve(p.Imports) called")
}
seen := make(map[string]bool)
var all []string
for _, path := range imports {
path = ResolveImportPath(p, path)
path = ResolveImportPath(loaderstate, p, path)
if !seen[path] {
seen[path] = true
all = append(all, path)
@ -1151,7 +1151,7 @@ func isDir(path string) bool {
// First, there is Go 1.5 vendoring (golang.org/s/go15vendor).
// If vendor expansion doesn't trigger, then the path is also subject to
// Go 1.11 module legacy conversion (golang.org/issue/25069).
func ResolveImportPath(parent *Package, path string) (found string) {
func ResolveImportPath(loaderstate *modload.State, parent *Package, path string) (found string) {
var parentPath, parentDir, parentRoot string
parentIsStd := false
if parent != nil {
@ -1160,7 +1160,7 @@ func ResolveImportPath(parent *Package, path string) (found string) {
parentRoot = parent.Root
parentIsStd = parent.Standard
}
return resolveImportPath(modload.LoaderState, path, parentPath, parentDir, parentRoot, parentIsStd)
return resolveImportPath(loaderstate, path, parentPath, parentDir, parentRoot, parentIsStd)
}
func resolveImportPath(loaderstate *modload.State, path, parentPath, parentDir, parentRoot string, parentIsStd bool) (found string) {

View file

@ -48,8 +48,8 @@ type TestCover struct {
// the package containing an error if the test packages or
// their dependencies have errors.
// Only test packages without errors are returned.
func TestPackagesFor(ctx context.Context, opts PackageOpts, p *Package, cover *TestCover) (pmain, ptest, pxtest, perr *Package) {
pmain, ptest, pxtest = TestPackagesAndErrors(ctx, nil, opts, p, cover)
func TestPackagesFor(loaderstate *modload.State, ctx context.Context, opts PackageOpts, p *Package, cover *TestCover) (pmain, ptest, pxtest, perr *Package) {
pmain, ptest, pxtest = TestPackagesAndErrors(loaderstate, ctx, nil, opts, p, cover)
for _, p1 := range []*Package{ptest, pxtest, pmain} {
if p1 == nil {
// pxtest may be nil
@ -99,7 +99,7 @@ func TestPackagesFor(ctx context.Context, opts PackageOpts, p *Package, cover *T
//
// The caller is expected to have checked that len(p.TestGoFiles)+len(p.XTestGoFiles) > 0,
// or else there's no point in any of this.
func TestPackagesAndErrors(ctx context.Context, done func(), opts PackageOpts, p *Package, cover *TestCover) (pmain, ptest, pxtest *Package) {
func TestPackagesAndErrors(loaderstate *modload.State, ctx context.Context, done func(), opts PackageOpts, p *Package, cover *TestCover) (pmain, ptest, pxtest *Package) {
ctx, span := trace.StartSpan(ctx, "load.TestPackagesAndErrors")
defer span.Done()
@ -107,7 +107,7 @@ func TestPackagesAndErrors(ctx context.Context, done func(), opts PackageOpts, p
defer pre.flush()
allImports := append([]string{}, p.TestImports...)
allImports = append(allImports, p.XTestImports...)
pre.preloadImports(modload.LoaderState, ctx, opts, allImports, p.Internal.Build)
pre.preloadImports(loaderstate, ctx, opts, allImports, p.Internal.Build)
var ptestErr, pxtestErr *PackageError
var imports, ximports []*Package
@ -117,7 +117,7 @@ func TestPackagesAndErrors(ctx context.Context, done func(), opts PackageOpts, p
stk.Push(ImportInfo{Pkg: p.ImportPath + " (test)"})
rawTestImports := str.StringList(p.TestImports)
for i, path := range p.TestImports {
p1, err := loadImport(modload.LoaderState, ctx, opts, pre, path, p.Dir, p, &stk, p.Internal.Build.TestImportPos[path], ResolveImport)
p1, err := loadImport(loaderstate, ctx, opts, pre, path, p.Dir, p, &stk, p.Internal.Build.TestImportPos[path], ResolveImport)
if err != nil && ptestErr == nil {
ptestErr = err
incomplete = true
@ -146,7 +146,7 @@ func TestPackagesAndErrors(ctx context.Context, done func(), opts PackageOpts, p
var pxtestIncomplete bool
rawXTestImports := str.StringList(p.XTestImports)
for i, path := range p.XTestImports {
p1, err := loadImport(modload.LoaderState, ctx, opts, pre, path, p.Dir, p, &stk, p.Internal.Build.XTestImportPos[path], ResolveImport)
p1, err := loadImport(loaderstate, ctx, opts, pre, path, p.Dir, p, &stk, p.Internal.Build.XTestImportPos[path], ResolveImport)
if err != nil && pxtestErr == nil {
pxtestErr = err
}
@ -293,7 +293,7 @@ func TestPackagesAndErrors(ctx context.Context, done func(), opts PackageOpts, p
}
pb := p.Internal.Build
pmain.DefaultGODEBUG = defaultGODEBUG(modload.LoaderState, pmain, pb.Directives, pb.TestDirectives, pb.XTestDirectives)
pmain.DefaultGODEBUG = defaultGODEBUG(loaderstate, pmain, pb.Directives, pb.TestDirectives, pb.XTestDirectives)
if pmain.Internal.BuildInfo == nil || pmain.DefaultGODEBUG != p.DefaultGODEBUG {
// Either we didn't generate build info for the package under test (because it wasn't package main), or
// the DefaultGODEBUG used to build the test main package is different from the DefaultGODEBUG
@ -322,7 +322,7 @@ func TestPackagesAndErrors(ctx context.Context, done func(), opts PackageOpts, p
if dep == ptest.ImportPath {
pmain.Internal.Imports = append(pmain.Internal.Imports, ptest)
} else {
p1, err := loadImport(modload.LoaderState, ctx, opts, pre, dep, "", nil, &stk, nil, 0)
p1, err := loadImport(loaderstate, ctx, opts, pre, dep, "", nil, &stk, nil, 0)
if err != nil && pmain.Error == nil {
pmain.Error = err
pmain.Incomplete = true

View file

@ -184,7 +184,7 @@ func runDownload(ctx context.Context, cmd *base.Command, args []string) {
var mods []*ModuleJSON
type token struct{}
sem := make(chan token, runtime.GOMAXPROCS(0))
infos, infosErr := modload.ListModules(ctx, args, 0, *downloadReuse)
infos, infosErr := modload.ListModules(modload.LoaderState, ctx, args, 0, *downloadReuse)
// There is a bit of a chicken-and-egg problem here: ideally we need to know
// which Go version to switch to download the requested modules, but if we

View file

@ -169,7 +169,7 @@ func RunVendor(ctx context.Context, vendorE bool, vendorO string, args []string)
goVersion := ""
if includeGoVersions {
goVersion = modload.ModuleInfo(ctx, m.Path).GoVersion
goVersion = modload.ModuleInfo(modload.LoaderState, ctx, m.Path).GoVersion
}
switch {
case isExplicit[m] && goVersion != "":

View file

@ -83,7 +83,7 @@ func runWhy(ctx context.Context, cmd *base.Command, args []string) {
}
}
mods, err := modload.ListModules(ctx, args, 0, "")
mods, err := modload.ListModules(modload.LoaderState, ctx, args, 0, "")
if err != nil {
base.Fatal(err)
}

View file

@ -83,27 +83,27 @@ func PackageModRoot(loaderstate *State, ctx context.Context, pkgpath string) str
return root
}
func ModuleInfo(ctx context.Context, path string) *modinfo.ModulePublic {
if !Enabled(LoaderState) {
func ModuleInfo(loaderstate *State, ctx context.Context, path string) *modinfo.ModulePublic {
if !Enabled(loaderstate) {
return nil
}
if path, vers, found := strings.Cut(path, "@"); found {
m := module.Version{Path: path, Version: vers}
return moduleInfo(LoaderState, ctx, nil, m, 0, nil)
return moduleInfo(loaderstate, ctx, nil, m, 0, nil)
}
rs := LoadModFile(LoaderState, ctx)
rs := LoadModFile(loaderstate, ctx)
var (
v string
ok bool
)
if rs.pruning == pruned {
v, ok = rs.rootSelected(LoaderState, path)
v, ok = rs.rootSelected(loaderstate, path)
}
if !ok {
mg, err := rs.Graph(LoaderState, ctx)
mg, err := rs.Graph(loaderstate, ctx)
if err != nil {
base.Fatal(err)
}
@ -119,16 +119,16 @@ func ModuleInfo(ctx context.Context, path string) *modinfo.ModulePublic {
}
}
return moduleInfo(LoaderState, ctx, rs, module.Version{Path: path, Version: v}, 0, nil)
return moduleInfo(loaderstate, ctx, rs, module.Version{Path: path, Version: v}, 0, nil)
}
// addUpdate fills in m.Update if an updated version is available.
func addUpdate(ctx context.Context, m *modinfo.ModulePublic) {
func addUpdate(loaderstate *State, ctx context.Context, m *modinfo.ModulePublic) {
if m.Version == "" {
return
}
info, err := Query(LoaderState, ctx, m.Path, "upgrade", m.Version, CheckAllowed)
info, err := Query(loaderstate, ctx, m.Path, "upgrade", m.Version, CheckAllowed)
if _, ok := errors.AsType[*NoMatchingVersionError](err); ok ||
errors.Is(err, fs.ErrNotExist) ||
errors.Is(err, ErrDisallowed) {
@ -212,7 +212,7 @@ func mergeOrigin(m1, m2 *codehost.Origin) *codehost.Origin {
// addVersions fills in m.Versions with the list of known versions.
// Excluded versions will be omitted. If listRetracted is false, retracted
// versions will also be omitted.
func addVersions(ctx context.Context, m *modinfo.ModulePublic, listRetracted bool) {
func addVersions(loaderstate *State, ctx context.Context, m *modinfo.ModulePublic, listRetracted bool) {
// TODO(bcmills): Would it make sense to check for reuse here too?
// Perhaps that doesn't buy us much, though: we would always have to fetch
// all of the version tags to list the available versions anyway.
@ -221,7 +221,7 @@ func addVersions(ctx context.Context, m *modinfo.ModulePublic, listRetracted boo
if listRetracted {
allowed = CheckExclusions
}
v, origin, err := versions(LoaderState, ctx, m.Path, allowed)
v, origin, err := versions(loaderstate, ctx, m.Path, allowed)
if err != nil && m.Error == nil {
m.Error = &modinfo.ModuleError{Err: err.Error()}
}
@ -262,8 +262,8 @@ func addRetraction(loaderstate *State, ctx context.Context, m *modinfo.ModulePub
// addDeprecation fills in m.Deprecated if the module was deprecated by its
// author. m.Error is set if there's an error loading deprecation information.
func addDeprecation(ctx context.Context, m *modinfo.ModulePublic) {
deprecation, err := CheckDeprecation(LoaderState, ctx, module.Version{Path: m.Path, Version: m.Version})
func addDeprecation(loaderstate *State, ctx context.Context, m *modinfo.ModulePublic) {
deprecation, err := CheckDeprecation(loaderstate, ctx, module.Version{Path: m.Path, Version: m.Version})
if _, ok := errors.AsType[*NoMatchingVersionError](err); ok || errors.Is(err, fs.ErrNotExist) {
// Ignore "no matching version" and "not found" errors.
// This means the proxy has no matching version or no versions at all.

View file

@ -41,7 +41,7 @@ const (
// along with any error preventing additional matches from being identified.
//
// The returned slice can be nonempty even if the error is non-nil.
func ListModules(ctx context.Context, args []string, mode ListMode, reuseFile string) ([]*modinfo.ModulePublic, error) {
func ListModules(loaderstate *State, ctx context.Context, args []string, mode ListMode, reuseFile string) ([]*modinfo.ModulePublic, error) {
var reuse map[module.Version]*modinfo.ModulePublic
if reuseFile != "" {
data, err := os.ReadFile(reuseFile)
@ -69,7 +69,7 @@ func ListModules(ctx context.Context, args []string, mode ListMode, reuseFile st
}
}
rs, mods, err := listModules(ctx, LoadModFile(LoaderState, ctx), args, mode, reuse)
rs, mods, err := listModules(loaderstate, ctx, LoadModFile(loaderstate, ctx), args, mode, reuse)
type token struct{}
sem := make(chan token, runtime.GOMAXPROCS(0))
@ -82,16 +82,16 @@ func ListModules(ctx context.Context, args []string, mode ListMode, reuseFile st
sem <- token{}
go func() {
if mode&ListU != 0 {
addUpdate(ctx, m)
addUpdate(loaderstate, ctx, m)
}
if mode&ListVersions != 0 {
addVersions(ctx, m, mode&ListRetractedVersions != 0)
addVersions(loaderstate, ctx, m, mode&ListRetractedVersions != 0)
}
if mode&ListRetracted != 0 {
addRetraction(LoaderState, ctx, m)
addRetraction(loaderstate, ctx, m)
}
if mode&ListDeprecated != 0 {
addDeprecation(ctx, m)
addDeprecation(loaderstate, ctx, m)
}
<-sem
}()
@ -109,7 +109,7 @@ func ListModules(ctx context.Context, args []string, mode ListMode, reuseFile st
}
if err == nil {
LoaderState.requirements = rs
loaderstate.requirements = rs
// TODO(#61605): The extra ListU clause fixes a problem with Go 1.21rc3
// where "go mod tidy" and "go list -m -u all" fight over whether the go.sum
// should be considered up-to-date. The fix for now is to always treat the
@ -117,20 +117,20 @@ func ListModules(ctx context.Context, args []string, mode ListMode, reuseFile st
// but in general list -u is looking up other checksums in the checksum database
// that won't be necessary later, so it makes sense not to write the go.sum back out.
if !ExplicitWriteGoMod && mode&ListU == 0 {
err = commitRequirements(LoaderState, ctx, WriteOpts{})
err = commitRequirements(loaderstate, ctx, WriteOpts{})
}
}
return mods, err
}
func listModules(ctx context.Context, rs *Requirements, args []string, mode ListMode, reuse map[module.Version]*modinfo.ModulePublic) (_ *Requirements, mods []*modinfo.ModulePublic, mgErr error) {
func listModules(loaderstate *State, ctx context.Context, rs *Requirements, args []string, mode ListMode, reuse map[module.Version]*modinfo.ModulePublic) (_ *Requirements, mods []*modinfo.ModulePublic, mgErr error) {
if len(args) == 0 {
var ms []*modinfo.ModulePublic
for _, m := range LoaderState.MainModules.Versions() {
for _, m := range loaderstate.MainModules.Versions() {
if gover.IsToolchain(m.Path) {
continue
}
ms = append(ms, moduleInfo(LoaderState, ctx, rs, m, mode, reuse))
ms = append(ms, moduleInfo(loaderstate, ctx, rs, m, mode, reuse))
}
return rs, ms, nil
}
@ -145,25 +145,25 @@ func listModules(ctx context.Context, rs *Requirements, args []string, mode List
}
if arg == "all" || strings.Contains(arg, "...") {
needFullGraph = true
if !HasModRoot(LoaderState) {
if !HasModRoot(loaderstate) {
base.Fatalf("go: cannot match %q: %v", arg, ErrNoModRoot)
}
continue
}
if path, vers, found := strings.Cut(arg, "@"); found {
if vers == "upgrade" || vers == "patch" {
if _, ok := rs.rootSelected(LoaderState, path); !ok || rs.pruning == unpruned {
if _, ok := rs.rootSelected(loaderstate, path); !ok || rs.pruning == unpruned {
needFullGraph = true
if !HasModRoot(LoaderState) {
if !HasModRoot(loaderstate) {
base.Fatalf("go: cannot match %q: %v", arg, ErrNoModRoot)
}
}
}
continue
}
if _, ok := rs.rootSelected(LoaderState, arg); !ok || rs.pruning == unpruned {
if _, ok := rs.rootSelected(loaderstate, arg); !ok || rs.pruning == unpruned {
needFullGraph = true
if mode&ListVersions == 0 && !HasModRoot(LoaderState) {
if mode&ListVersions == 0 && !HasModRoot(loaderstate) {
base.Fatalf("go: cannot match %q without -versions or an explicit version: %v", arg, ErrNoModRoot)
}
}
@ -171,7 +171,7 @@ func listModules(ctx context.Context, rs *Requirements, args []string, mode List
var mg *ModuleGraph
if needFullGraph {
rs, mg, mgErr = expandGraph(LoaderState, ctx, rs)
rs, mg, mgErr = expandGraph(loaderstate, ctx, rs)
}
matchedModule := map[module.Version]bool{}
@ -179,7 +179,7 @@ func listModules(ctx context.Context, rs *Requirements, args []string, mode List
if path, vers, found := strings.Cut(arg, "@"); found {
var current string
if mg == nil {
current, _ = rs.rootSelected(LoaderState, path)
current, _ = rs.rootSelected(loaderstate, path)
} else {
current = mg.Selected(path)
}
@ -198,7 +198,7 @@ func listModules(ctx context.Context, rs *Requirements, args []string, mode List
// specific revision or used 'go list -retracted'.
allowed = nil
}
info, err := queryReuse(LoaderState, ctx, path, vers, current, allowed, reuse)
info, err := queryReuse(loaderstate, ctx, path, vers, current, allowed, reuse)
if err != nil {
var origin *codehost.Origin
if info != nil {
@ -217,7 +217,7 @@ func listModules(ctx context.Context, rs *Requirements, args []string, mode List
// *Requirements instead.
var noRS *Requirements
mod := moduleInfo(LoaderState, ctx, noRS, module.Version{Path: path, Version: info.Version}, mode, reuse)
mod := moduleInfo(loaderstate, ctx, noRS, module.Version{Path: path, Version: info.Version}, mode, reuse)
if vers != mod.Version {
mod.Query = vers
}
@ -237,7 +237,7 @@ func listModules(ctx context.Context, rs *Requirements, args []string, mode List
var v string
if mg == nil {
var ok bool
v, ok = rs.rootSelected(LoaderState, arg)
v, ok = rs.rootSelected(loaderstate, arg)
if !ok {
// We checked rootSelected(arg) in the earlier args loop, so if there
// is no such root we should have loaded a non-nil mg.
@ -251,7 +251,7 @@ func listModules(ctx context.Context, rs *Requirements, args []string, mode List
continue
}
if v != "none" {
mods = append(mods, moduleInfo(LoaderState, ctx, rs, module.Version{Path: arg, Version: v}, mode, reuse))
mods = append(mods, moduleInfo(loaderstate, ctx, rs, module.Version{Path: arg, Version: v}, mode, reuse))
} else if cfg.BuildMod == "vendor" {
// In vendor mode, we can't determine whether a missing module is “a
// known dependency” because the module graph is incomplete.
@ -292,7 +292,7 @@ func listModules(ctx context.Context, rs *Requirements, args []string, mode List
fetchedMods := make([]*modinfo.ModulePublic, len(matches))
for i, m := range matches {
q.Add(func() {
fetchedMods[i] = moduleInfo(LoaderState, ctx, rs, m, mode, reuse)
fetchedMods[i] = moduleInfo(loaderstate, ctx, rs, m, mode, reuse)
})
}
<-q.Idle()

View file

@ -1181,7 +1181,7 @@ func builderTest(b *work.Builder, ctx context.Context, pkgOpts load.PackageOpts,
Paths: cfg.BuildCoverPkg,
}
}
pmain, ptest, pxtest, perr := load.TestPackagesFor(ctx, pkgOpts, p, cover)
pmain, ptest, pxtest, perr := load.TestPackagesFor(modload.LoaderState, ctx, pkgOpts, p, cover)
if perr != nil {
return nil, nil, nil, perr, perr.Error
}

View file

@ -249,7 +249,7 @@ func run(ctx context.Context, cmd *base.Command, args []string) {
root := &work.Action{Mode: "go " + cmd.Name()}
for _, p := range pkgs {
_, ptest, pxtest, perr := load.TestPackagesFor(ctx, pkgOpts, p, nil)
_, ptest, pxtest, perr := load.TestPackagesFor(modload.LoaderState, ctx, pkgOpts, p, nil)
if perr != nil {
base.Errorf("%v", perr.Error)
continue

View file

@ -444,9 +444,9 @@ func (b *Builder) cacheAction(mode string, p *load.Package, f func() *Action) *A
}
// AutoAction returns the "right" action for go build or go install of p.
func (b *Builder) AutoAction(mode, depMode BuildMode, p *load.Package) *Action {
func (b *Builder) AutoAction(loaderstate *modload.State, mode, depMode BuildMode, p *load.Package) *Action {
if p.Name == "main" {
return b.LinkAction(modload.LoaderState, mode, depMode, p)
return b.LinkAction(loaderstate, mode, depMode, p)
}
return b.CompileAction(mode, depMode, p)
}

View file

@ -527,7 +527,7 @@ func runBuild(ctx context.Context, cmd *base.Command, args []string) {
p.Target += cfg.ExeSuffix
p.Stale = true
p.StaleReason = "build -o flag in use"
a.Deps = append(a.Deps, b.AutoAction(ModeInstall, depMode, p))
a.Deps = append(a.Deps, b.AutoAction(modload.LoaderState, ModeInstall, depMode, p))
}
if len(a.Deps) == 0 {
base.Fatalf("go: no main packages to build")
@ -544,14 +544,14 @@ func runBuild(ctx context.Context, cmd *base.Command, args []string) {
p.Target = cfg.BuildO
p.Stale = true // must build - not up to date
p.StaleReason = "build -o flag in use"
a := b.AutoAction(ModeInstall, depMode, p)
a := b.AutoAction(modload.LoaderState, ModeInstall, depMode, p)
b.Do(ctx, a)
return
}
a := &Action{Mode: "go build"}
for _, p := range pkgs {
a.Deps = append(a.Deps, b.AutoAction(ModeBuild, depMode, p))
a.Deps = append(a.Deps, b.AutoAction(modload.LoaderState, ModeBuild, depMode, p))
}
if cfg.BuildBuildmode == "shared" {
a = b.buildmodeShared(ModeBuild, depMode, args, pkgs, a)
@ -797,7 +797,7 @@ func InstallPackages(ctx context.Context, patterns []string, pkgs []*load.Packag
// If p is a tool, delay the installation until the end of the build.
// This avoids installing assemblers/compilers that are being executed
// by other steps in the build.
a1 := b.AutoAction(ModeInstall, depMode, p)
a1 := b.AutoAction(modload.LoaderState, ModeInstall, depMode, p)
if load.InstallTargetDir(p) == load.ToTool {
a.Deps = append(a.Deps, a1.Deps...)
a1.Deps = append(a1.Deps, a)