From 296ecc918d6688b5eeb9abe32d919b0aaa76d798 Mon Sep 17 00:00:00 2001 From: Ian Alexander Date: Thu, 2 Oct 2025 22:52:08 -0400 Subject: [PATCH] cmd/go: add modload.State parameter to AllowedFunc This change makes the following functions methods on the State: * CheckAllowed * CheckExclusions * CheckRetractions Doing so allows us to reduce the use of the global state variable in downstream function calls. This commit is part of the overall effort to eliminate global modloader state. Change-Id: I97147311d9de16ecac8c122c2b6bdde94bad9d8f Reviewed-on: https://go-review.googlesource.com/c/go/+/711119 Reviewed-by: Michael Matloob Reviewed-by: Michael Matloob LUCI-TryBot-Result: Go LUCI --- src/cmd/go/internal/load/pkg.go | 2 +- src/cmd/go/internal/modget/get.go | 6 +++--- src/cmd/go/internal/modload/build.go | 8 ++++---- src/cmd/go/internal/modload/import.go | 2 +- src/cmd/go/internal/modload/list.go | 2 +- src/cmd/go/internal/modload/modfile.go | 20 ++++++++++---------- src/cmd/go/internal/modload/mvs.go | 2 +- src/cmd/go/internal/modload/query_test.go | 3 ++- src/cmd/go/internal/toolchain/select.go | 2 +- 9 files changed, 24 insertions(+), 23 deletions(-) diff --git a/src/cmd/go/internal/load/pkg.go b/src/cmd/go/internal/load/pkg.go index 5549632400..70d3d2551d 100644 --- a/src/cmd/go/internal/load/pkg.go +++ b/src/cmd/go/internal/load/pkg.go @@ -3398,7 +3398,7 @@ func PackagesAndErrorsOutsideModule(loaderstate *modload.State, ctx context.Cont // (first result). It's possible this module won't provide packages named by // later arguments, and other modules would. Let's not try to be too // magical though. - allowed := modload.CheckAllowed + allowed := loaderstate.CheckAllowed if modload.IsRevisionQuery(firstPath, version) { // Don't check for retractions if a specific revision is requested. allowed = nil diff --git a/src/cmd/go/internal/modget/get.go b/src/cmd/go/internal/modget/get.go index f9522860fa..95e94edabe 100644 --- a/src/cmd/go/internal/modget/get.go +++ b/src/cmd/go/internal/modget/get.go @@ -686,12 +686,12 @@ func (r *resolver) queryPattern(loaderstate *modload.State, ctx context.Context, func (r *resolver) checkAllowedOr(s *modload.State, requested string, selected func(string) string) modload.AllowedFunc { return func(ctx context.Context, m module.Version) error { if m.Version == requested { - return modload.CheckExclusions(ctx, m) + return s.CheckExclusions(ctx, m) } if (requested == "upgrade" || requested == "patch") && m.Version == selected(m.Path) { return nil } - return modload.CheckAllowed(ctx, m) + return s.CheckAllowed(ctx, m) } } @@ -1715,7 +1715,7 @@ func (r *resolver) checkPackageProblems(loaderstate *modload.State, ctx context. for i := range retractions { i := i r.work.Add(func() { - err := modload.CheckRetractions(loaderstate, ctx, retractions[i].m) + err := loaderstate.CheckRetractions(ctx, retractions[i].m) if _, ok := errors.AsType[*modload.ModuleRetractedError](err); ok { retractions[i].message = err.Error() } diff --git a/src/cmd/go/internal/modload/build.go b/src/cmd/go/internal/modload/build.go index 0773a27799..7299452670 100644 --- a/src/cmd/go/internal/modload/build.go +++ b/src/cmd/go/internal/modload/build.go @@ -128,7 +128,7 @@ func addUpdate(loaderstate *State, ctx context.Context, m *modinfo.ModulePublic) return } - info, err := Query(loaderstate, ctx, m.Path, "upgrade", m.Version, CheckAllowed) + info, err := Query(loaderstate, ctx, m.Path, "upgrade", m.Version, loaderstate.CheckAllowed) if _, ok := errors.AsType[*NoMatchingVersionError](err); ok || errors.Is(err, fs.ErrNotExist) || errors.Is(err, ErrDisallowed) { @@ -217,9 +217,9 @@ func addVersions(loaderstate *State, ctx context.Context, m *modinfo.ModulePubli // 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. - allowed := CheckAllowed + allowed := loaderstate.CheckAllowed if listRetracted { - allowed = CheckExclusions + allowed = loaderstate.CheckExclusions } v, origin, err := versions(loaderstate, ctx, m.Path, allowed) if err != nil && m.Error == nil { @@ -236,7 +236,7 @@ func addRetraction(loaderstate *State, ctx context.Context, m *modinfo.ModulePub return } - err := CheckRetractions(loaderstate, ctx, module.Version{Path: m.Path, Version: m.Version}) + err := loaderstate.CheckRetractions(ctx, module.Version{Path: m.Path, Version: m.Version}) if err == nil { return } else if _, ok := errors.AsType[*NoMatchingVersionError](err); ok || errors.Is(err, fs.ErrNotExist) { diff --git a/src/cmd/go/internal/modload/import.go b/src/cmd/go/internal/modload/import.go index a2a98289b0..461c18ef41 100644 --- a/src/cmd/go/internal/modload/import.go +++ b/src/cmd/go/internal/modload/import.go @@ -612,7 +612,7 @@ func queryImport(loaderstate *State, ctx context.Context, path string, rs *Requi return module.Version{}, err } - candidates, err := QueryPackages(loaderstate, ctx, path, "latest", mg.Selected, CheckAllowed) + candidates, err := QueryPackages(loaderstate, ctx, path, "latest", mg.Selected, loaderstate.CheckAllowed) if err != nil { if errors.Is(err, fs.ErrNotExist) { // Return "cannot find module providing package […]" instead of whatever diff --git a/src/cmd/go/internal/modload/list.go b/src/cmd/go/internal/modload/list.go index 21057f5c1e..ee31f06805 100644 --- a/src/cmd/go/internal/modload/list.go +++ b/src/cmd/go/internal/modload/list.go @@ -192,7 +192,7 @@ func listModules(loaderstate *State, ctx context.Context, rs *Requirements, args } } - allowed := CheckAllowed + allowed := loaderstate.CheckAllowed if IsRevisionQuery(path, vers) || mode&ListRetracted != 0 { // Allow excluded and retracted versions if the user asked for a // specific revision or used 'go list -retracted'. diff --git a/src/cmd/go/internal/modload/modfile.go b/src/cmd/go/internal/modload/modfile.go index 20feb8fcac..be0f2a5c11 100644 --- a/src/cmd/go/internal/modload/modfile.go +++ b/src/cmd/go/internal/modload/modfile.go @@ -138,11 +138,11 @@ func pruningForGoVersion(goVersion string) modPruning { // CheckAllowed returns an error equivalent to ErrDisallowed if m is excluded by // the main module's go.mod or retracted by its author. Most version queries use // this to filter out versions that should not be used. -func CheckAllowed(ctx context.Context, m module.Version) error { - if err := CheckExclusions(ctx, m); err != nil { +func (s *State) CheckAllowed(ctx context.Context, m module.Version) error { + if err := s.CheckExclusions(ctx, m); err != nil { return err } - if err := CheckRetractions(LoaderState, ctx, m); err != nil { + if err := s.CheckRetractions(ctx, m); err != nil { return err } return nil @@ -154,9 +154,9 @@ var ErrDisallowed = errors.New("disallowed module version") // CheckExclusions returns an error equivalent to ErrDisallowed if module m is // excluded by the main module's go.mod file. -func CheckExclusions(ctx context.Context, m module.Version) error { - for _, mainModule := range LoaderState.MainModules.Versions() { - if index := LoaderState.MainModules.Index(mainModule); index != nil && index.exclude[m] { +func (s *State) CheckExclusions(ctx context.Context, m module.Version) error { + for _, mainModule := range s.MainModules.Versions() { + if index := s.MainModules.Index(mainModule); index != nil && index.exclude[m] { return module.VersionError(m, errExcluded) } } @@ -172,7 +172,7 @@ func (e *excludedError) Is(err error) bool { return err == ErrDisallowed } // CheckRetractions returns an error if module m has been retracted by // its author. -func CheckRetractions(loaderstate *State, ctx context.Context, m module.Version) (err error) { +func (s *State) CheckRetractions(ctx context.Context, m module.Version) (err error) { defer func() { if err == nil { return @@ -193,7 +193,7 @@ func CheckRetractions(loaderstate *State, ctx context.Context, m module.Version) // Cannot be retracted. return nil } - if repl := Replacement(loaderstate, module.Version{Path: m.Path}); repl.Path != "" { + if repl := Replacement(s, module.Version{Path: m.Path}); repl.Path != "" { // All versions of the module were replaced. // Don't load retractions, since we'd just load the replacement. return nil @@ -210,11 +210,11 @@ func CheckRetractions(loaderstate *State, ctx context.Context, m module.Version) // We load the raw file here: the go.mod file may have a different module // path that we expect if the module or its repository was renamed. // We still want to apply retractions to other aliases of the module. - rm, err := queryLatestVersionIgnoringRetractions(loaderstate, ctx, m.Path) + rm, err := queryLatestVersionIgnoringRetractions(s, ctx, m.Path) if err != nil { return err } - summary, err := rawGoModSummary(loaderstate, rm) + summary, err := rawGoModSummary(s, rm) if err != nil && !errors.Is(err, gover.ErrTooNew) { return err } diff --git a/src/cmd/go/internal/modload/mvs.go b/src/cmd/go/internal/modload/mvs.go index 32afc866fb..fba508873f 100644 --- a/src/cmd/go/internal/modload/mvs.go +++ b/src/cmd/go/internal/modload/mvs.go @@ -116,7 +116,7 @@ func previousVersion(loaderstate *State, ctx context.Context, m module.Version) return module.Version{Path: m.Path, Version: "none"}, nil } - list, _, err := versions(loaderstate, ctx, m.Path, CheckAllowed) + list, _, err := versions(loaderstate, ctx, m.Path, loaderstate.CheckAllowed) if err != nil { if errors.Is(err, os.ErrNotExist) { return module.Version{Path: m.Path, Version: "none"}, nil diff --git a/src/cmd/go/internal/modload/query_test.go b/src/cmd/go/internal/modload/query_test.go index b4487eebb0..a465fab5db 100644 --- a/src/cmd/go/internal/modload/query_test.go +++ b/src/cmd/go/internal/modload/query_test.go @@ -168,6 +168,7 @@ func TestQuery(t *testing.T) { ctx := context.Background() for _, tt := range queryTests { + loaderstate := NewState() allow := tt.allow if allow == "" { allow = "*" @@ -182,7 +183,7 @@ func TestQuery(t *testing.T) { t.Run(strings.ReplaceAll(tt.path, "/", "_")+"/"+tt.query+"/"+tt.current+"/"+allow, func(t *testing.T) { t.Parallel() - info, err := Query(LoaderState, ctx, tt.path, tt.query, tt.current, allowed) + info, err := Query(loaderstate, ctx, tt.path, tt.query, tt.current, allowed) if tt.err != "" { if err == nil { t.Errorf("Query(_, %q, %q, %q, %v) = %v, want error %q", tt.path, tt.query, tt.current, allow, info.Version, tt.err) diff --git a/src/cmd/go/internal/toolchain/select.go b/src/cmd/go/internal/toolchain/select.go index 5a0600a965..86aa89dd7d 100644 --- a/src/cmd/go/internal/toolchain/select.go +++ b/src/cmd/go/internal/toolchain/select.go @@ -700,7 +700,7 @@ func maybeSwitchForGoInstallVersion(loaderstate *modload.State, minVers string) // See internal/load.PackagesAndErrorsOutsideModule ctx := context.Background() - allowed := modload.CheckAllowed + allowed := loaderstate.CheckAllowed if modload.IsRevisionQuery(path, version) { // Don't check for retractions if a specific revision is requested. allowed = nil -- 2.52.0