Skip to content

Commit 296ecc9

Browse files
committed
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 <[email protected]> Reviewed-by: Michael Matloob <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]>
1 parent c445a61 commit 296ecc9

File tree

9 files changed

+24
-23
lines changed

9 files changed

+24
-23
lines changed

src/cmd/go/internal/load/pkg.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3398,7 +3398,7 @@ func PackagesAndErrorsOutsideModule(loaderstate *modload.State, ctx context.Cont
33983398
// (first result). It's possible this module won't provide packages named by
33993399
// later arguments, and other modules would. Let's not try to be too
34003400
// magical though.
3401-
allowed := modload.CheckAllowed
3401+
allowed := loaderstate.CheckAllowed
34023402
if modload.IsRevisionQuery(firstPath, version) {
34033403
// Don't check for retractions if a specific revision is requested.
34043404
allowed = nil

src/cmd/go/internal/modget/get.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -686,12 +686,12 @@ func (r *resolver) queryPattern(loaderstate *modload.State, ctx context.Context,
686686
func (r *resolver) checkAllowedOr(s *modload.State, requested string, selected func(string) string) modload.AllowedFunc {
687687
return func(ctx context.Context, m module.Version) error {
688688
if m.Version == requested {
689-
return modload.CheckExclusions(ctx, m)
689+
return s.CheckExclusions(ctx, m)
690690
}
691691
if (requested == "upgrade" || requested == "patch") && m.Version == selected(m.Path) {
692692
return nil
693693
}
694-
return modload.CheckAllowed(ctx, m)
694+
return s.CheckAllowed(ctx, m)
695695
}
696696
}
697697

@@ -1715,7 +1715,7 @@ func (r *resolver) checkPackageProblems(loaderstate *modload.State, ctx context.
17151715
for i := range retractions {
17161716
i := i
17171717
r.work.Add(func() {
1718-
err := modload.CheckRetractions(loaderstate, ctx, retractions[i].m)
1718+
err := loaderstate.CheckRetractions(ctx, retractions[i].m)
17191719
if _, ok := errors.AsType[*modload.ModuleRetractedError](err); ok {
17201720
retractions[i].message = err.Error()
17211721
}

src/cmd/go/internal/modload/build.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ func addUpdate(loaderstate *State, ctx context.Context, m *modinfo.ModulePublic)
128128
return
129129
}
130130

131-
info, err := Query(loaderstate, ctx, m.Path, "upgrade", m.Version, CheckAllowed)
131+
info, err := Query(loaderstate, ctx, m.Path, "upgrade", m.Version, loaderstate.CheckAllowed)
132132
if _, ok := errors.AsType[*NoMatchingVersionError](err); ok ||
133133
errors.Is(err, fs.ErrNotExist) ||
134134
errors.Is(err, ErrDisallowed) {
@@ -217,9 +217,9 @@ func addVersions(loaderstate *State, ctx context.Context, m *modinfo.ModulePubli
217217
// Perhaps that doesn't buy us much, though: we would always have to fetch
218218
// all of the version tags to list the available versions anyway.
219219

220-
allowed := CheckAllowed
220+
allowed := loaderstate.CheckAllowed
221221
if listRetracted {
222-
allowed = CheckExclusions
222+
allowed = loaderstate.CheckExclusions
223223
}
224224
v, origin, err := versions(loaderstate, ctx, m.Path, allowed)
225225
if err != nil && m.Error == nil {
@@ -236,7 +236,7 @@ func addRetraction(loaderstate *State, ctx context.Context, m *modinfo.ModulePub
236236
return
237237
}
238238

239-
err := CheckRetractions(loaderstate, ctx, module.Version{Path: m.Path, Version: m.Version})
239+
err := loaderstate.CheckRetractions(ctx, module.Version{Path: m.Path, Version: m.Version})
240240
if err == nil {
241241
return
242242
} else if _, ok := errors.AsType[*NoMatchingVersionError](err); ok || errors.Is(err, fs.ErrNotExist) {

src/cmd/go/internal/modload/import.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -612,7 +612,7 @@ func queryImport(loaderstate *State, ctx context.Context, path string, rs *Requi
612612
return module.Version{}, err
613613
}
614614

615-
candidates, err := QueryPackages(loaderstate, ctx, path, "latest", mg.Selected, CheckAllowed)
615+
candidates, err := QueryPackages(loaderstate, ctx, path, "latest", mg.Selected, loaderstate.CheckAllowed)
616616
if err != nil {
617617
if errors.Is(err, fs.ErrNotExist) {
618618
// Return "cannot find module providing package […]" instead of whatever

src/cmd/go/internal/modload/list.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ func listModules(loaderstate *State, ctx context.Context, rs *Requirements, args
192192
}
193193
}
194194

195-
allowed := CheckAllowed
195+
allowed := loaderstate.CheckAllowed
196196
if IsRevisionQuery(path, vers) || mode&ListRetracted != 0 {
197197
// Allow excluded and retracted versions if the user asked for a
198198
// specific revision or used 'go list -retracted'.

src/cmd/go/internal/modload/modfile.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -138,11 +138,11 @@ func pruningForGoVersion(goVersion string) modPruning {
138138
// CheckAllowed returns an error equivalent to ErrDisallowed if m is excluded by
139139
// the main module's go.mod or retracted by its author. Most version queries use
140140
// this to filter out versions that should not be used.
141-
func CheckAllowed(ctx context.Context, m module.Version) error {
142-
if err := CheckExclusions(ctx, m); err != nil {
141+
func (s *State) CheckAllowed(ctx context.Context, m module.Version) error {
142+
if err := s.CheckExclusions(ctx, m); err != nil {
143143
return err
144144
}
145-
if err := CheckRetractions(LoaderState, ctx, m); err != nil {
145+
if err := s.CheckRetractions(ctx, m); err != nil {
146146
return err
147147
}
148148
return nil
@@ -154,9 +154,9 @@ var ErrDisallowed = errors.New("disallowed module version")
154154

155155
// CheckExclusions returns an error equivalent to ErrDisallowed if module m is
156156
// excluded by the main module's go.mod file.
157-
func CheckExclusions(ctx context.Context, m module.Version) error {
158-
for _, mainModule := range LoaderState.MainModules.Versions() {
159-
if index := LoaderState.MainModules.Index(mainModule); index != nil && index.exclude[m] {
157+
func (s *State) CheckExclusions(ctx context.Context, m module.Version) error {
158+
for _, mainModule := range s.MainModules.Versions() {
159+
if index := s.MainModules.Index(mainModule); index != nil && index.exclude[m] {
160160
return module.VersionError(m, errExcluded)
161161
}
162162
}
@@ -172,7 +172,7 @@ func (e *excludedError) Is(err error) bool { return err == ErrDisallowed }
172172

173173
// CheckRetractions returns an error if module m has been retracted by
174174
// its author.
175-
func CheckRetractions(loaderstate *State, ctx context.Context, m module.Version) (err error) {
175+
func (s *State) CheckRetractions(ctx context.Context, m module.Version) (err error) {
176176
defer func() {
177177
if err == nil {
178178
return
@@ -193,7 +193,7 @@ func CheckRetractions(loaderstate *State, ctx context.Context, m module.Version)
193193
// Cannot be retracted.
194194
return nil
195195
}
196-
if repl := Replacement(loaderstate, module.Version{Path: m.Path}); repl.Path != "" {
196+
if repl := Replacement(s, module.Version{Path: m.Path}); repl.Path != "" {
197197
// All versions of the module were replaced.
198198
// Don't load retractions, since we'd just load the replacement.
199199
return nil
@@ -210,11 +210,11 @@ func CheckRetractions(loaderstate *State, ctx context.Context, m module.Version)
210210
// We load the raw file here: the go.mod file may have a different module
211211
// path that we expect if the module or its repository was renamed.
212212
// We still want to apply retractions to other aliases of the module.
213-
rm, err := queryLatestVersionIgnoringRetractions(loaderstate, ctx, m.Path)
213+
rm, err := queryLatestVersionIgnoringRetractions(s, ctx, m.Path)
214214
if err != nil {
215215
return err
216216
}
217-
summary, err := rawGoModSummary(loaderstate, rm)
217+
summary, err := rawGoModSummary(s, rm)
218218
if err != nil && !errors.Is(err, gover.ErrTooNew) {
219219
return err
220220
}

src/cmd/go/internal/modload/mvs.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ func previousVersion(loaderstate *State, ctx context.Context, m module.Version)
116116
return module.Version{Path: m.Path, Version: "none"}, nil
117117
}
118118

119-
list, _, err := versions(loaderstate, ctx, m.Path, CheckAllowed)
119+
list, _, err := versions(loaderstate, ctx, m.Path, loaderstate.CheckAllowed)
120120
if err != nil {
121121
if errors.Is(err, os.ErrNotExist) {
122122
return module.Version{Path: m.Path, Version: "none"}, nil

src/cmd/go/internal/modload/query_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,7 @@ func TestQuery(t *testing.T) {
168168
ctx := context.Background()
169169

170170
for _, tt := range queryTests {
171+
loaderstate := NewState()
171172
allow := tt.allow
172173
if allow == "" {
173174
allow = "*"
@@ -182,7 +183,7 @@ func TestQuery(t *testing.T) {
182183
t.Run(strings.ReplaceAll(tt.path, "/", "_")+"/"+tt.query+"/"+tt.current+"/"+allow, func(t *testing.T) {
183184
t.Parallel()
184185

185-
info, err := Query(LoaderState, ctx, tt.path, tt.query, tt.current, allowed)
186+
info, err := Query(loaderstate, ctx, tt.path, tt.query, tt.current, allowed)
186187
if tt.err != "" {
187188
if err == nil {
188189
t.Errorf("Query(_, %q, %q, %q, %v) = %v, want error %q", tt.path, tt.query, tt.current, allow, info.Version, tt.err)

src/cmd/go/internal/toolchain/select.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -700,7 +700,7 @@ func maybeSwitchForGoInstallVersion(loaderstate *modload.State, minVers string)
700700

701701
// See internal/load.PackagesAndErrorsOutsideModule
702702
ctx := context.Background()
703-
allowed := modload.CheckAllowed
703+
allowed := loaderstate.CheckAllowed
704704
if modload.IsRevisionQuery(path, version) {
705705
// Don't check for retractions if a specific revision is requested.
706706
allowed = nil

0 commit comments

Comments
 (0)