diff --git a/src/cmd/go/internal/load/pkg.go b/src/cmd/go/internal/load/pkg.go index 55496324004..70d3d2551dd 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 f9522860faf..95e94edabe8 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 0773a277990..7299452670c 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 a2a98289b0e..461c18ef419 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 21057f5c1ec..ee31f068059 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 20feb8fcacc..be0f2a5c116 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 32afc866fbc..fba508873fa 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 b4487eebb0d..a465fab5db3 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 5a0600a9654..86aa89dd7d4 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