Skip to content

Commit ca379b1

Browse files
committed
cmd/go: remove loaderstate dependency
This change removes the dependency on the module loader state from the `QueryMatchesMainModulesError.Error()` method. This commit is part of the overall effort to eliminate global modloader state. Change-Id: I47241587a0bf9b578931628f35ed3b936a0cb04a Reviewed-on: https://go-review.googlesource.com/c/go/+/714700 LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Michael Matloob <matloob@google.com> Reviewed-by: Michael Matloob <matloob@golang.org>
1 parent 83a44bd commit ca379b1

File tree

2 files changed

+32
-23
lines changed

2 files changed

+32
-23
lines changed

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

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -735,7 +735,12 @@ func (r *resolver) queryNone(loaderstate *modload.State, ctx context.Context, q
735735
// However, neither of those behaviors would be consistent with the
736736
// plain meaning of the query. To try to reduce confusion, reject the
737737
// query explicitly.
738-
return errSet(&modload.QueryMatchesMainModulesError{LoaderState: loaderstate, MainModules: []module.Version{v}, Pattern: q.pattern, Query: q.version})
738+
return errSet(&modload.QueryMatchesMainModulesError{
739+
MainModules: []module.Version{v},
740+
Pattern: q.pattern,
741+
Query: q.version,
742+
PatternIsModule: loaderstate.MainModules.Contains(q.pattern),
743+
})
739744
}
740745

741746
return pathSet{mod: module.Version{Path: q.pattern, Version: "none"}}
@@ -748,7 +753,12 @@ func (r *resolver) queryNone(loaderstate *modload.State, ctx context.Context, q
748753
}
749754
q.pathOnce(curM.Path, func() pathSet {
750755
if modload.HasModRoot(loaderstate) && curM.Version == "" && loaderstate.MainModules.Contains(curM.Path) {
751-
return errSet(&modload.QueryMatchesMainModulesError{LoaderState: loaderstate, MainModules: []module.Version{curM}, Pattern: q.pattern, Query: q.version})
756+
return errSet(&modload.QueryMatchesMainModulesError{
757+
MainModules: []module.Version{curM},
758+
Pattern: q.pattern,
759+
Query: q.version,
760+
PatternIsModule: loaderstate.MainModules.Contains(q.pattern),
761+
})
752762
}
753763
return pathSet{mod: module.Version{Path: curM.Path, Version: "none"}}
754764
})
@@ -852,10 +862,10 @@ func (r *resolver) queryWildcard(loaderstate *modload.State, ctx context.Context
852862
if loaderstate.MainModules.Contains(curM.Path) && !versionOkForMainModule(q.version) {
853863
if q.matchesPath(curM.Path) {
854864
return errSet(&modload.QueryMatchesMainModulesError{
855-
LoaderState: loaderstate,
856-
MainModules: []module.Version{curM},
857-
Pattern: q.pattern,
858-
Query: q.version,
865+
MainModules: []module.Version{curM},
866+
Pattern: q.pattern,
867+
Query: q.version,
868+
PatternIsModule: loaderstate.MainModules.Contains(q.pattern),
859869
})
860870
}
861871

@@ -1953,10 +1963,10 @@ func (r *resolver) resolve(s *modload.State, q *query, m module.Version) {
19531963

19541964
if s.MainModules.Contains(m.Path) && m.Version != "" {
19551965
reportError(q, &modload.QueryMatchesMainModulesError{
1956-
LoaderState: s,
1957-
MainModules: []module.Version{{Path: m.Path}},
1958-
Pattern: q.pattern,
1959-
Query: q.version,
1966+
MainModules: []module.Version{{Path: m.Path}},
1967+
Pattern: q.pattern,
1968+
Query: q.version,
1969+
PatternIsModule: s.MainModules.Contains(q.pattern),
19601970
})
19611971
return
19621972
}

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

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -763,10 +763,10 @@ 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,
767-
MainModules: mainModuleMatches,
768-
Pattern: pattern,
769-
Query: query,
766+
MainModules: mainModuleMatches,
767+
Pattern: pattern,
768+
Query: query,
769+
PatternIsModule: loaderstate.MainModules.Contains(pattern),
770770
}
771771
} else {
772772
return nil, nil, &PackageNotInModuleError{
@@ -827,9 +827,9 @@ func QueryPattern(loaderstate *State, ctx context.Context, pattern, query string
827827

828828
if len(mainModuleMatches) > 0 && len(results) == 0 && modOnly == nil && errors.Is(err, fs.ErrNotExist) {
829829
return nil, nil, &QueryMatchesMainModulesError{
830-
LoaderState: loaderstate,
831-
Pattern: pattern,
832-
Query: query,
830+
Pattern: pattern,
831+
Query: query,
832+
PatternIsModule: loaderstate.MainModules.Contains(pattern),
833833
}
834834
}
835835
return slices.Clip(results), modOnly, err
@@ -1287,15 +1287,14 @@ func (rr *replacementRepo) replacementStat(v string) (*modfetch.RevInfo, error)
12871287
// a version of the main module that cannot be satisfied.
12881288
// (The main module's version cannot be changed.)
12891289
type QueryMatchesMainModulesError struct {
1290-
LoaderState *State
1291-
MainModules []module.Version
1292-
Pattern string
1293-
Query string
1290+
MainModules []module.Version
1291+
Pattern string
1292+
Query string
1293+
PatternIsModule bool // true if pattern is one of the main modules
12941294
}
12951295

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

0 commit comments

Comments
 (0)