]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/go: inject state parameter into `clean.runClean`
authorIan Alexander <jitsu@google.com>
Wed, 8 Oct 2025 16:07:44 +0000 (12:07 -0400)
committerIan Alexander <jitsu@google.com>
Mon, 20 Oct 2025 19:50:25 +0000 (12:50 -0700)
This command modifies the call tree starting at `clean.runClean` 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/clean
rf '
  inject modload.LoaderState runClean
  add clean.go var moduleLoaderState *modload.State
  ex {
    import "cmd/go/internal/modload";
    modload.LoaderState -> moduleLoaderState
  }
  add runClean://+0 moduleLoaderState := modload.NewState()
  rm clean.go:/var moduleLoaderState \*modload.State/
'
cd ..
./rf-cleanup.zsh

Change-Id: I2e30e44cfff7e533801dabd7159fa760ac6bb824
Reviewed-on: https://go-review.googlesource.com/c/go/+/710296
Reviewed-by: Michael Matloob <matloob@google.com>
Reviewed-by: Michael Matloob <matloob@golang.org>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>

src/cmd/go/internal/clean/clean.go

index 1c05977de554f8eed298f3032353108c983398c3..ae744e13bc79b51a1b87f445254efdc64c255dba 100644 (file)
@@ -120,7 +120,8 @@ func init() {
 }
 
 func runClean(ctx context.Context, cmd *base.Command, args []string) {
-       modload.InitWorkfile(modload.LoaderState)
+       moduleLoaderState := modload.NewState()
+       modload.InitWorkfile(moduleLoaderState)
        if len(args) > 0 {
                cacheFlag := ""
                switch {
@@ -142,13 +143,13 @@ func runClean(ctx context.Context, cmd *base.Command, args []string) {
        // either the flags and arguments explicitly imply a package,
        // or no other target (such as a cache) was requested to be cleaned.
        cleanPkg := len(args) > 0 || cleanI || cleanR
-       if (!modload.Enabled(modload.LoaderState) || modload.HasModRoot(modload.LoaderState)) &&
+       if (!modload.Enabled(moduleLoaderState) || modload.HasModRoot(moduleLoaderState)) &&
                !cleanCache && !cleanModcache && !cleanTestcache && !cleanFuzzcache {
                cleanPkg = true
        }
 
        if cleanPkg {
-               for _, pkg := range load.PackagesAndErrors(modload.LoaderState, ctx, load.PackageOpts{}, args) {
+               for _, pkg := range load.PackagesAndErrors(moduleLoaderState, ctx, load.PackageOpts{}, args) {
                        clean(pkg)
                }
        }