]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/go: inject State parameter into `work.runInstall`
authorIan Alexander <jitsu@google.com>
Wed, 8 Oct 2025 19:05:14 +0000 (15:05 -0400)
committerIan Alexander <jitsu@google.com>
Mon, 20 Oct 2025 20:04:01 +0000 (13:04 -0700)
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>
src/cmd/go/internal/work/build.go

index 496247b9a7a7d460b223433e5a311135b2cc96a4..d5687956b4124f62a5544dfd6806a851eb0bcc1f 100644 (file)
@@ -689,7 +689,7 @@ func libname(args []string, pkgs []*load.Package) (string, error) {
 func runInstall(ctx context.Context, cmd *base.Command, args []string) {
        for _, arg := range args {
                if strings.Contains(arg, "@") && !build.IsLocalImport(arg) && !filepath.IsAbs(arg) {
-                       installOutsideModule(ctx, args)
+                       installOutsideModule(modload.LoaderState, ctx, args)
                        return
                }
        }
@@ -725,7 +725,7 @@ func runInstall(ctx context.Context, cmd *base.Command, args []string) {
                load.PrepareForCoverageBuild(modload.LoaderState, pkgs)
        }
 
-       InstallPackages(ctx, args, pkgs)
+       InstallPackages(modload.LoaderState, ctx, args, pkgs)
 }
 
 // omitTestOnly returns pkgs with test-only packages removed.
@@ -745,7 +745,7 @@ func omitTestOnly(pkgs []*load.Package) []*load.Package {
        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, " "))
        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.
                // This avoids installing assemblers/compilers that are being executed
                // 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 {
                        a.Deps = append(a.Deps, a1.Deps...)
                        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
                // with a list of Deps, one per package named in pkgs,
                // 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)
@@ -858,12 +858,12 @@ func InstallPackages(ctx context.Context, patterns []string, pkgs []*load.Packag
 // in the current directory or parent directories.
 //
 // See golang.org/issue/40276 for details and rationale.
-func installOutsideModule(ctx context.Context, args []string) {
-       modload.LoaderState.ForceUseModules = true
-       modload.LoaderState.RootMode = modload.NoRoot
-       modload.AllowMissingModuleImports(modload.LoaderState)
-       modload.Init(modload.LoaderState)
-       BuildInit(modload.LoaderState)
+func installOutsideModule(loaderstate *modload.State, ctx context.Context, args []string) {
+       loaderstate.ForceUseModules = true
+       loaderstate.RootMode = modload.NoRoot
+       modload.AllowMissingModuleImports(loaderstate)
+       modload.Init(loaderstate)
+       BuildInit(loaderstate)
 
        // Load packages. Ignore non-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
        // matched by a pattern.
        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 {
                base.Fatal(err)
        }
@@ -883,7 +883,7 @@ func installOutsideModule(ctx context.Context, args []string) {
        }
 
        // Build and install the packages.
-       InstallPackages(ctx, patterns, pkgs)
+       InstallPackages(loaderstate, ctx, patterns, pkgs)
 }
 
 // ExecCmd is the command to use to run user binaries.