Skip to content

Commit c445a61

Browse files
committed
cmd/go: add loaderstate as field on QueryMatchesMainModulesError
This change modifies the type `QueryMatchesMainModulesError` to have an additional field to store the current module loader state. The field is used to break the dependency on the global `modload.LoaderState` variable. This commit is part of the overall effort to eliminate global modloader state. Change-Id: Ia2066fab9f3ec4ffdd3d7393dacdf65c0fd35b92 Reviewed-on: https://go-review.googlesource.com/c/go/+/711118 LUCI-TryBot-Result: Go LUCI <[email protected]> Reviewed-by: David Chase <[email protected]> Reviewed-by: Michael Matloob <[email protected]>
1 parent 6ac4005 commit c445a61

File tree

2 files changed

+26
-19
lines changed

2 files changed

+26
-19
lines changed

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

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -683,7 +683,7 @@ func (r *resolver) queryPattern(loaderstate *modload.State, ctx context.Context,
683683

684684
// checkAllowedOr is like modload.CheckAllowed, but it always allows the requested
685685
// and current versions (even if they are retracted or otherwise excluded).
686-
func (r *resolver) checkAllowedOr(loaderstate *modload.State, requested string, selected func(string) string) modload.AllowedFunc {
686+
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 {
689689
return modload.CheckExclusions(ctx, m)
@@ -734,7 +734,7 @@ func (r *resolver) queryNone(loaderstate *modload.State, ctx context.Context, q
734734
// However, neither of those behaviors would be consistent with the
735735
// plain meaning of the query. To try to reduce confusion, reject the
736736
// query explicitly.
737-
return errSet(&modload.QueryMatchesMainModulesError{MainModules: []module.Version{v}, Pattern: q.pattern, Query: q.version})
737+
return errSet(&modload.QueryMatchesMainModulesError{LoaderState: loaderstate, MainModules: []module.Version{v}, Pattern: q.pattern, Query: q.version})
738738
}
739739

740740
return pathSet{mod: module.Version{Path: q.pattern, Version: "none"}}
@@ -747,7 +747,7 @@ func (r *resolver) queryNone(loaderstate *modload.State, ctx context.Context, q
747747
}
748748
q.pathOnce(curM.Path, func() pathSet {
749749
if modload.HasModRoot(loaderstate) && curM.Version == "" && loaderstate.MainModules.Contains(curM.Path) {
750-
return errSet(&modload.QueryMatchesMainModulesError{MainModules: []module.Version{curM}, Pattern: q.pattern, Query: q.version})
750+
return errSet(&modload.QueryMatchesMainModulesError{LoaderState: loaderstate, MainModules: []module.Version{curM}, Pattern: q.pattern, Query: q.version})
751751
}
752752
return pathSet{mod: module.Version{Path: curM.Path, Version: "none"}}
753753
})
@@ -851,6 +851,7 @@ func (r *resolver) queryWildcard(loaderstate *modload.State, ctx context.Context
851851
if loaderstate.MainModules.Contains(curM.Path) && !versionOkForMainModule(q.version) {
852852
if q.matchesPath(curM.Path) {
853853
return errSet(&modload.QueryMatchesMainModulesError{
854+
LoaderState: loaderstate,
854855
MainModules: []module.Version{curM},
855856
Pattern: q.pattern,
856857
Query: q.version,
@@ -1482,7 +1483,7 @@ func (r *resolver) applyUpgrades(loaderstate *modload.State, ctx context.Context
14821483
// In the vast majority of cases, we expect only one module per pathSet,
14831484
// but we want to give some minimal additional tools so that users can add an
14841485
// extra argument or two on the command line to resolve simple ambiguities.
1485-
func (r *resolver) disambiguate(loaderstate *modload.State, cs pathSet) (filtered pathSet, isPackage bool, m module.Version, unique bool) {
1486+
func (r *resolver) disambiguate(s *modload.State, cs pathSet) (filtered pathSet, isPackage bool, m module.Version, unique bool) {
14861487
if len(cs.pkgMods) == 0 && cs.mod.Path == "" {
14871488
panic("internal error: resolveIfUnambiguous called with empty pathSet")
14881489
}
@@ -1494,7 +1495,7 @@ func (r *resolver) disambiguate(loaderstate *modload.State, cs pathSet) (filtere
14941495
continue
14951496
}
14961497

1497-
if loaderstate.MainModules.Contains(m.Path) {
1498+
if s.MainModules.Contains(m.Path) {
14981499
if m.Version == "" {
14991500
return pathSet{}, true, m, true
15001501
}
@@ -1944,13 +1945,14 @@ func (r *resolver) reportChanges(oldReqs, newReqs []module.Version) {
19441945
// resolve records that module m must be at its indicated version (which may be
19451946
// "none") due to query q. If some other query forces module m to be at a
19461947
// different version, resolve reports a conflict error.
1947-
func (r *resolver) resolve(loaderstate *modload.State, q *query, m module.Version) {
1948+
func (r *resolver) resolve(s *modload.State, q *query, m module.Version) {
19481949
if m.Path == "" {
19491950
panic("internal error: resolving a module.Version with an empty path")
19501951
}
19511952

1952-
if loaderstate.MainModules.Contains(m.Path) && m.Version != "" {
1953+
if s.MainModules.Contains(m.Path) && m.Version != "" {
19531954
reportError(q, &modload.QueryMatchesMainModulesError{
1955+
LoaderState: s,
19541956
MainModules: []module.Version{{Path: m.Path}},
19551957
Pattern: q.pattern,
19561958
Query: q.version,

src/cmd/go/internal/modload/query.go

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -763,6 +763,7 @@ func QueryPattern(loaderstate *State, ctx context.Context, pattern, query string
763763
return nil, modOnly, nil
764764
} else if len(mainModuleMatches) != 0 {
765765
return nil, nil, &QueryMatchesMainModulesError{
766+
LoaderState: loaderstate,
766767
MainModules: mainModuleMatches,
767768
Pattern: pattern,
768769
Query: query,
@@ -826,8 +827,9 @@ func QueryPattern(loaderstate *State, ctx context.Context, pattern, query string
826827

827828
if len(mainModuleMatches) > 0 && len(results) == 0 && modOnly == nil && errors.Is(err, fs.ErrNotExist) {
828829
return nil, nil, &QueryMatchesMainModulesError{
829-
Pattern: pattern,
830-
Query: query,
830+
LoaderState: loaderstate,
831+
Pattern: pattern,
832+
Query: query,
831833
}
832834
}
833835
return slices.Clip(results), modOnly, err
@@ -1112,7 +1114,7 @@ type versionRepo interface {
11121114
CheckReuse(context.Context, *codehost.Origin) error
11131115
Versions(ctx context.Context, prefix string) (*modfetch.Versions, error)
11141116
Stat(ctx context.Context, rev string) (*modfetch.RevInfo, error)
1115-
Latest(context.Context) (*modfetch.RevInfo, error)
1117+
Latest(ctx context.Context) (*modfetch.RevInfo, error)
11161118
}
11171119

11181120
var _ versionRepo = modfetch.Repo(nil)
@@ -1130,7 +1132,7 @@ func lookupRepo(loaderstate *State, ctx context.Context, proxy, path string) (re
11301132
if loaderstate.MainModules == nil {
11311133
return repo, err
11321134
} else if _, ok := loaderstate.MainModules.HighestReplaced()[path]; ok {
1133-
return &replacementRepo{repo: repo}, nil
1135+
return &replacementRepo{repo: repo, loaderstate: loaderstate}, nil
11341136
}
11351137

11361138
return repo, err
@@ -1163,7 +1165,8 @@ func (er emptyRepo) Latest(ctx context.Context) (*modfetch.RevInfo, error) { ret
11631165
// modules, so a replacementRepo should only be constructed for a module that
11641166
// actually has one or more valid replacements.
11651167
type replacementRepo struct {
1166-
repo versionRepo
1168+
repo versionRepo
1169+
loaderstate *State
11671170
}
11681171

11691172
var _ versionRepo = (*replacementRepo)(nil)
@@ -1186,8 +1189,8 @@ func (rr *replacementRepo) Versions(ctx context.Context, prefix string) (*modfet
11861189
}
11871190

11881191
versions := repoVersions.List
1189-
for _, mm := range LoaderState.MainModules.Versions() {
1190-
if index := LoaderState.MainModules.Index(mm); index != nil && len(index.replace) > 0 {
1192+
for _, mm := range rr.loaderstate.MainModules.Versions() {
1193+
if index := rr.loaderstate.MainModules.Index(mm); index != nil && len(index.replace) > 0 {
11911194
path := rr.ModulePath()
11921195
for m := range index.replace {
11931196
if m.Path == path && strings.HasPrefix(m.Version, prefix) && m.Version != "" && !module.IsPseudoVersion(m.Version) {
@@ -1215,8 +1218,8 @@ func (rr *replacementRepo) Stat(ctx context.Context, rev string) (*modfetch.RevI
12151218
return info, err
12161219
}
12171220
var hasReplacements bool
1218-
for _, v := range LoaderState.MainModules.Versions() {
1219-
if index := LoaderState.MainModules.Index(v); index != nil && len(index.replace) > 0 {
1221+
for _, v := range rr.loaderstate.MainModules.Versions() {
1222+
if index := rr.loaderstate.MainModules.Index(v); index != nil && len(index.replace) > 0 {
12201223
hasReplacements = true
12211224
}
12221225
}
@@ -1239,7 +1242,7 @@ func (rr *replacementRepo) Stat(ctx context.Context, rev string) (*modfetch.RevI
12391242
}
12401243
}
12411244

1242-
if r := Replacement(LoaderState, module.Version{Path: path, Version: v}); r.Path == "" {
1245+
if r := Replacement(rr.loaderstate, module.Version{Path: path, Version: v}); r.Path == "" {
12431246
return info, err
12441247
}
12451248
return rr.replacementStat(v)
@@ -1249,7 +1252,7 @@ func (rr *replacementRepo) Latest(ctx context.Context) (*modfetch.RevInfo, error
12491252
info, err := rr.repo.Latest(ctx)
12501253
path := rr.ModulePath()
12511254

1252-
if v, ok := LoaderState.MainModules.HighestReplaced()[path]; ok {
1255+
if v, ok := rr.loaderstate.MainModules.HighestReplaced()[path]; ok {
12531256
if v == "" {
12541257
// The only replacement is a wildcard that doesn't specify a version, so
12551258
// synthesize a pseudo-version with an appropriate major version and a
@@ -1284,13 +1287,15 @@ func (rr *replacementRepo) replacementStat(v string) (*modfetch.RevInfo, error)
12841287
// a version of the main module that cannot be satisfied.
12851288
// (The main module's version cannot be changed.)
12861289
type QueryMatchesMainModulesError struct {
1290+
LoaderState *State
12871291
MainModules []module.Version
12881292
Pattern string
12891293
Query string
12901294
}
12911295

12921296
func (e *QueryMatchesMainModulesError) Error() string {
1293-
if LoaderState.MainModules.Contains(e.Pattern) {
1297+
// TODO(jitsu): break dependency on loaderstate
1298+
if e.LoaderState.MainModules.Contains(e.Pattern) {
12941299
return fmt.Sprintf("can't request version %q of the main module (%s)", e.Query, e.Pattern)
12951300
}
12961301

0 commit comments

Comments
 (0)