]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/go: add modload.State parameter to AllowedFunc
authorIan Alexander <jitsu@google.com>
Fri, 3 Oct 2025 02:52:08 +0000 (22:52 -0400)
committerIan Alexander <jitsu@google.com>
Fri, 24 Oct 2025 14:24:18 +0000 (07:24 -0700)
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>

src/cmd/go/internal/load/pkg.go
src/cmd/go/internal/modget/get.go
src/cmd/go/internal/modload/build.go
src/cmd/go/internal/modload/import.go
src/cmd/go/internal/modload/list.go
src/cmd/go/internal/modload/modfile.go
src/cmd/go/internal/modload/mvs.go
src/cmd/go/internal/modload/query_test.go
src/cmd/go/internal/toolchain/select.go

index 554963240049e4d64ad5640767f567c7ad96a0b7..70d3d2551ddb9ad5de0ab9c23c97fc0f2ba3b80d 100644 (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
        // 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
index f9522860faf5f89ad7a48669b0da7d706a9c480f..95e94edabe8092224c53b38ef439a73bfaf9421f 100644 (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 {
        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()
                        }
index 0773a2779906499c3c1f2fcf8a2bda483d6da01f..7299452670c13ff2703356bf679b0e97c9088ae6 100644 (file)
@@ -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) {
index a2a98289b0ed77cfdb8d1c139de5ed81f94467f2..461c18ef419351730390dbddfe9dec07591ed07f 100644 (file)
@@ -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
index 21057f5c1ecad0589c0551bafa28c7b0769e032e..ee31f06805941a53d205138a586e3d1772fb6e1e 100644 (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 {
                                // Allow excluded and retracted versions if the user asked for a
                                // specific revision or used 'go list -retracted'.
index 20feb8fcacc784b8a5cc37905944c2f611c9a95c..be0f2a5c1166fc1af79ec006712f17ad5eaefda1 100644 (file)
@@ -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
        }
index 32afc866fbcc14fc8d29e3595764d2855e79b9c6..fba508873faee1bb1e0f7c21119d0fd412d71e18 100644 (file)
@@ -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
index b4487eebb0d9e957f23c250e24fd68b3b62d888c..a465fab5db39338520bd3e33a8226e25a796838b 100644 (file)
@@ -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)
index 5a0600a9654ce081502d9b09bae3684bb1e40ab4..86aa89dd7d4a1d3613513e65887cd0b92b3cab97 100644 (file)
@@ -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