cmd/go: inject state parameter into clean.runClean

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>
This commit is contained in:
Ian Alexander 2025-10-08 12:07:44 -04:00
parent 58a8fdb6cf
commit f7eaea02fd

View 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)
}
}