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 <matloob@golang.org>
Reviewed-by: Michael Matloob <matloob@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
This commit is contained in:
Ian Alexander 2025-10-02 22:52:08 -04:00
parent c445a61e52
commit 296ecc918d
9 changed files with 24 additions and 23 deletions

View file

@ -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 // (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 // later arguments, and other modules would. Let's not try to be too
// magical though. // magical though.
allowed := modload.CheckAllowed allowed := loaderstate.CheckAllowed
if modload.IsRevisionQuery(firstPath, version) { if modload.IsRevisionQuery(firstPath, version) {
// Don't check for retractions if a specific revision is requested. // Don't check for retractions if a specific revision is requested.
allowed = nil allowed = nil

View file

@ -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 { func (r *resolver) checkAllowedOr(s *modload.State, requested string, selected func(string) string) modload.AllowedFunc {
return func(ctx context.Context, m module.Version) error { return func(ctx context.Context, m module.Version) error {
if m.Version == requested { if m.Version == requested {
return modload.CheckExclusions(ctx, m) return s.CheckExclusions(ctx, m)
} }
if (requested == "upgrade" || requested == "patch") && m.Version == selected(m.Path) { if (requested == "upgrade" || requested == "patch") && m.Version == selected(m.Path) {
return nil 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 { for i := range retractions {
i := i i := i
r.work.Add(func() { 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 { if _, ok := errors.AsType[*modload.ModuleRetractedError](err); ok {
retractions[i].message = err.Error() retractions[i].message = err.Error()
} }

View file

@ -128,7 +128,7 @@ func addUpdate(loaderstate *State, ctx context.Context, m *modinfo.ModulePublic)
return 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 || if _, ok := errors.AsType[*NoMatchingVersionError](err); ok ||
errors.Is(err, fs.ErrNotExist) || errors.Is(err, fs.ErrNotExist) ||
errors.Is(err, ErrDisallowed) { 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 // 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. // all of the version tags to list the available versions anyway.
allowed := CheckAllowed allowed := loaderstate.CheckAllowed
if listRetracted { if listRetracted {
allowed = CheckExclusions allowed = loaderstate.CheckExclusions
} }
v, origin, err := versions(loaderstate, ctx, m.Path, allowed) v, origin, err := versions(loaderstate, ctx, m.Path, allowed)
if err != nil && m.Error == nil { if err != nil && m.Error == nil {
@ -236,7 +236,7 @@ func addRetraction(loaderstate *State, ctx context.Context, m *modinfo.ModulePub
return 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 { if err == nil {
return return
} else if _, ok := errors.AsType[*NoMatchingVersionError](err); ok || errors.Is(err, fs.ErrNotExist) { } else if _, ok := errors.AsType[*NoMatchingVersionError](err); ok || errors.Is(err, fs.ErrNotExist) {

View file

@ -612,7 +612,7 @@ func queryImport(loaderstate *State, ctx context.Context, path string, rs *Requi
return module.Version{}, err 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 err != nil {
if errors.Is(err, fs.ErrNotExist) { if errors.Is(err, fs.ErrNotExist) {
// Return "cannot find module providing package […]" instead of whatever // Return "cannot find module providing package […]" instead of whatever

View file

@ -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 { if IsRevisionQuery(path, vers) || mode&ListRetracted != 0 {
// Allow excluded and retracted versions if the user asked for a // Allow excluded and retracted versions if the user asked for a
// specific revision or used 'go list -retracted'. // specific revision or used 'go list -retracted'.

View file

@ -138,11 +138,11 @@ func pruningForGoVersion(goVersion string) modPruning {
// CheckAllowed returns an error equivalent to ErrDisallowed if m is excluded by // 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 // 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. // this to filter out versions that should not be used.
func CheckAllowed(ctx context.Context, m module.Version) error { func (s *State) CheckAllowed(ctx context.Context, m module.Version) error {
if err := CheckExclusions(ctx, m); err != nil { if err := s.CheckExclusions(ctx, m); err != nil {
return err return err
} }
if err := CheckRetractions(LoaderState, ctx, m); err != nil { if err := s.CheckRetractions(ctx, m); err != nil {
return err return err
} }
return nil return nil
@ -154,9 +154,9 @@ var ErrDisallowed = errors.New("disallowed module version")
// CheckExclusions returns an error equivalent to ErrDisallowed if module m is // CheckExclusions returns an error equivalent to ErrDisallowed if module m is
// excluded by the main module's go.mod file. // excluded by the main module's go.mod file.
func CheckExclusions(ctx context.Context, m module.Version) error { func (s *State) CheckExclusions(ctx context.Context, m module.Version) error {
for _, mainModule := range LoaderState.MainModules.Versions() { for _, mainModule := range s.MainModules.Versions() {
if index := LoaderState.MainModules.Index(mainModule); index != nil && index.exclude[m] { if index := s.MainModules.Index(mainModule); index != nil && index.exclude[m] {
return module.VersionError(m, errExcluded) 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 // CheckRetractions returns an error if module m has been retracted by
// its author. // 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() { defer func() {
if err == nil { if err == nil {
return return
@ -193,7 +193,7 @@ func CheckRetractions(loaderstate *State, ctx context.Context, m module.Version)
// Cannot be retracted. // Cannot be retracted.
return nil 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. // All versions of the module were replaced.
// Don't load retractions, since we'd just load the replacement. // Don't load retractions, since we'd just load the replacement.
return nil 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 // 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. // path that we expect if the module or its repository was renamed.
// We still want to apply retractions to other aliases of the module. // 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 { if err != nil {
return err return err
} }
summary, err := rawGoModSummary(loaderstate, rm) summary, err := rawGoModSummary(s, rm)
if err != nil && !errors.Is(err, gover.ErrTooNew) { if err != nil && !errors.Is(err, gover.ErrTooNew) {
return err return err
} }

View file

@ -116,7 +116,7 @@ func previousVersion(loaderstate *State, ctx context.Context, m module.Version)
return module.Version{Path: m.Path, Version: "none"}, nil 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 err != nil {
if errors.Is(err, os.ErrNotExist) { if errors.Is(err, os.ErrNotExist) {
return module.Version{Path: m.Path, Version: "none"}, nil return module.Version{Path: m.Path, Version: "none"}, nil

View file

@ -168,6 +168,7 @@ func TestQuery(t *testing.T) {
ctx := context.Background() ctx := context.Background()
for _, tt := range queryTests { for _, tt := range queryTests {
loaderstate := NewState()
allow := tt.allow allow := tt.allow
if allow == "" { if allow == "" {
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.Run(strings.ReplaceAll(tt.path, "/", "_")+"/"+tt.query+"/"+tt.current+"/"+allow, func(t *testing.T) {
t.Parallel() 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 tt.err != "" {
if err == nil { 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) t.Errorf("Query(_, %q, %q, %q, %v) = %v, want error %q", tt.path, tt.query, tt.current, allow, info.Version, tt.err)

View file

@ -700,7 +700,7 @@ func maybeSwitchForGoInstallVersion(loaderstate *modload.State, minVers string)
// See internal/load.PackagesAndErrorsOutsideModule // See internal/load.PackagesAndErrorsOutsideModule
ctx := context.Background() ctx := context.Background()
allowed := modload.CheckAllowed allowed := loaderstate.CheckAllowed
if modload.IsRevisionQuery(path, version) { if modload.IsRevisionQuery(path, version) {
// Don't check for retractions if a specific revision is requested. // Don't check for retractions if a specific revision is requested.
allowed = nil allowed = nil