cmd/go: inject State parameter into work.runInstall

This command modifies the call tree starting at `work.runInstall` 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/work
rf 'inject modload.LoaderState runInstall'
cd ..
./rf-cleanup.zsh

Change-Id: I038d2c4870d67835c165852b223eaad3e2496202
Reviewed-on: https://go-review.googlesource.com/c/go/+/710304
Reviewed-by: Michael Matloob <matloob@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Michael Matloob <matloob@golang.org>
This commit is contained in:
Ian Alexander 2025-10-08 15:05:14 -04:00
parent e94a5008f6
commit d57c3fd743

View file

@ -689,7 +689,7 @@ func libname(args []string, pkgs []*load.Package) (string, error) {
func runInstall(ctx context.Context, cmd *base.Command, args []string) { func runInstall(ctx context.Context, cmd *base.Command, args []string) {
for _, arg := range args { for _, arg := range args {
if strings.Contains(arg, "@") && !build.IsLocalImport(arg) && !filepath.IsAbs(arg) { if strings.Contains(arg, "@") && !build.IsLocalImport(arg) && !filepath.IsAbs(arg) {
installOutsideModule(ctx, args) installOutsideModule(modload.LoaderState, ctx, args)
return return
} }
} }
@ -725,7 +725,7 @@ func runInstall(ctx context.Context, cmd *base.Command, args []string) {
load.PrepareForCoverageBuild(modload.LoaderState, pkgs) load.PrepareForCoverageBuild(modload.LoaderState, pkgs)
} }
InstallPackages(ctx, args, pkgs) InstallPackages(modload.LoaderState, ctx, args, pkgs)
} }
// omitTestOnly returns pkgs with test-only packages removed. // omitTestOnly returns pkgs with test-only packages removed.
@ -745,7 +745,7 @@ func omitTestOnly(pkgs []*load.Package) []*load.Package {
return list return list
} }
func InstallPackages(ctx context.Context, patterns []string, pkgs []*load.Package) { func InstallPackages(loaderstate *modload.State, ctx context.Context, patterns []string, pkgs []*load.Package) {
ctx, span := trace.StartSpan(ctx, "InstallPackages "+strings.Join(patterns, " ")) ctx, span := trace.StartSpan(ctx, "InstallPackages "+strings.Join(patterns, " "))
defer span.Done() defer span.Done()
@ -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. // If p is a tool, delay the installation until the end of the build.
// This avoids installing assemblers/compilers that are being executed // This avoids installing assemblers/compilers that are being executed
// by other steps in the build. // by other steps in the build.
a1 := b.AutoAction(modload.LoaderState, ModeInstall, depMode, p) a1 := b.AutoAction(loaderstate, ModeInstall, depMode, p)
if load.InstallTargetDir(p) == load.ToTool { if load.InstallTargetDir(p) == load.ToTool {
a.Deps = append(a.Deps, a1.Deps...) a.Deps = append(a.Deps, a1.Deps...)
a1.Deps = append(a1.Deps, a) a1.Deps = append(a1.Deps, a)
@ -819,7 +819,7 @@ func InstallPackages(ctx context.Context, patterns []string, pkgs []*load.Packag
// tools above did not apply, and a is just a simple Action // tools above did not apply, and a is just a simple Action
// with a list of Deps, one per package named in pkgs, // with a list of Deps, one per package named in pkgs,
// the same as in runBuild. // the same as in runBuild.
a = b.buildmodeShared(modload.LoaderState, ModeInstall, ModeInstall, patterns, pkgs, a) a = b.buildmodeShared(loaderstate, ModeInstall, ModeInstall, patterns, pkgs, a)
} }
b.Do(ctx, a) b.Do(ctx, a)
@ -858,12 +858,12 @@ func InstallPackages(ctx context.Context, patterns []string, pkgs []*load.Packag
// in the current directory or parent directories. // in the current directory or parent directories.
// //
// See golang.org/issue/40276 for details and rationale. // See golang.org/issue/40276 for details and rationale.
func installOutsideModule(ctx context.Context, args []string) { func installOutsideModule(loaderstate *modload.State, ctx context.Context, args []string) {
modload.LoaderState.ForceUseModules = true loaderstate.ForceUseModules = true
modload.LoaderState.RootMode = modload.NoRoot loaderstate.RootMode = modload.NoRoot
modload.AllowMissingModuleImports(modload.LoaderState) modload.AllowMissingModuleImports(loaderstate)
modload.Init(modload.LoaderState) modload.Init(loaderstate)
BuildInit(modload.LoaderState) BuildInit(loaderstate)
// Load packages. Ignore non-main packages. // Load packages. Ignore non-main packages.
// Print a warning if an argument contains "..." and matches no main packages. // Print a warning if an argument contains "..." and matches no main packages.
@ -872,7 +872,7 @@ func installOutsideModule(ctx context.Context, args []string) {
// TODO(golang.org/issue/40276): don't report errors loading non-main packages // TODO(golang.org/issue/40276): don't report errors loading non-main packages
// matched by a pattern. // matched by a pattern.
pkgOpts := load.PackageOpts{MainOnly: true} pkgOpts := load.PackageOpts{MainOnly: true}
pkgs, err := load.PackagesAndErrorsOutsideModule(modload.LoaderState, ctx, pkgOpts, args) pkgs, err := load.PackagesAndErrorsOutsideModule(loaderstate, ctx, pkgOpts, args)
if err != nil { if err != nil {
base.Fatal(err) base.Fatal(err)
} }
@ -883,7 +883,7 @@ func installOutsideModule(ctx context.Context, args []string) {
} }
// Build and install the packages. // Build and install the packages.
InstallPackages(ctx, patterns, pkgs) InstallPackages(loaderstate, ctx, patterns, pkgs)
} }
// ExecCmd is the command to use to run user binaries. // ExecCmd is the command to use to run user binaries.