Skip to content

Commit f2d0d05

Browse files
committed
cmd/go: refactor usage of MainModules
This commit refactors usage of the global variable `MainModules` to the global LoaderState variable of the same name. This commit is part of the overall effort to eliminate global modloader state. [git-generate] cd src/cmd/go/internal/modload rf 'mv State.mainModules State.MainModules' rf 'ex { MainModules -> LoaderState.MainModules }' for dir in load modcmd modget test tool workcmd ; do cd ../${dir} rf 'ex { import "cmd/go/internal/modload" modload.MainModules -> modload.LoaderState.MainModules }' done cd ../modload rf 'rm MainModules' Change-Id: I15644c84190717d62ae953747a288ec6495ef168 Reviewed-on: https://go-review.googlesource.com/c/go/+/698060 Reviewed-by: Michael Matloob <[email protected]> Reviewed-by: Michael Matloob <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]>
1 parent f7a68d3 commit f2d0d05

File tree

23 files changed

+182
-184
lines changed

23 files changed

+182
-184
lines changed

src/cmd/go/internal/load/godebug.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ func defaultGODEBUG(p *Package, directives, testDirectives, xtestDirectives []bu
4949
if p.Name != "main" {
5050
return ""
5151
}
52-
goVersion := modload.MainModules.GoVersion()
52+
goVersion := modload.LoaderState.MainModules.GoVersion()
5353
if modload.LoaderState.RootMode == modload.NoRoot && p.Module != nil {
5454
// This is go install pkg@version or go run pkg@version.
5555
// Use the Go version from the package.
@@ -73,7 +73,7 @@ func defaultGODEBUG(p *Package, directives, testDirectives, xtestDirectives []bu
7373
}
7474

7575
// Add directives from main module go.mod.
76-
for _, g := range modload.MainModules.Godebugs() {
76+
for _, g := range modload.LoaderState.MainModules.Godebugs() {
7777
if m == nil {
7878
m = make(map[string]string)
7979
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1552,7 +1552,7 @@ func disallowInternal(ctx context.Context, srcDir string, importer *Package, imp
15521552
// directory containing them.
15531553
// If the directory is outside the main modules, this will resolve to ".",
15541554
// which is not a prefix of any valid module.
1555-
importerPath, _ = modload.MainModules.DirImportPath(ctx, importer.Dir)
1555+
importerPath, _ = modload.LoaderState.MainModules.DirImportPath(ctx, importer.Dir)
15561556
}
15571557
parentOfInternal := p.ImportPath[:i]
15581558
if str.HasPathPrefix(importerPath, parentOfInternal) {

src/cmd/go/internal/load/search.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,11 @@ func MatchPackage(pattern, cwd string) func(*Package) bool {
5656
return func(p *Package) bool { return p.Standard && strings.HasPrefix(p.ImportPath, "cmd/") }
5757
case pattern == "tool" && modload.Enabled():
5858
return func(p *Package) bool {
59-
return modload.MainModules.Tools()[p.ImportPath]
59+
return modload.LoaderState.MainModules.Tools()[p.ImportPath]
6060
}
6161
case pattern == "work" && modload.Enabled():
6262
return func(p *Package) bool {
63-
return p.Module != nil && modload.MainModules.Contains(p.Module.Path)
63+
return p.Module != nil && modload.LoaderState.MainModules.Contains(p.Module.Path)
6464
}
6565

6666
default:

src/cmd/go/internal/modcmd/download.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ func runDownload(ctx context.Context, cmd *base.Command, args []string) {
120120
modload.LoadModFile(ctx) // to fill MainModules
121121

122122
if haveExplicitArgs {
123-
for _, mainModule := range modload.MainModules.Versions() {
123+
for _, mainModule := range modload.LoaderState.MainModules.Versions() {
124124
targetAtUpgrade := mainModule.Path + "@upgrade"
125125
targetAtPatch := mainModule.Path + "@patch"
126126
for _, arg := range args {
@@ -136,8 +136,8 @@ func runDownload(ctx context.Context, cmd *base.Command, args []string) {
136136
// https://go-review.googlesource.com/c/go/+/359794/comments/ce946a80_6cf53992.
137137
args = []string{"all"}
138138
} else {
139-
mainModule := modload.MainModules.Versions()[0]
140-
modFile := modload.MainModules.ModFile(mainModule)
139+
mainModule := modload.LoaderState.MainModules.Versions()[0]
140+
modFile := modload.LoaderState.MainModules.ModFile(mainModule)
141141
if modFile.Go == nil || gover.Compare(modFile.Go.Version, gover.ExplicitIndirectVersion) < 0 {
142142
if len(modFile.Require) > 0 {
143143
args = []string{"all"}

src/cmd/go/internal/modcmd/vendor.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ func RunVendor(ctx context.Context, vendorE bool, vendorO string, args []string)
106106
modpkgs := make(map[module.Version][]string)
107107
for _, pkg := range pkgs {
108108
m := modload.PackageModule(pkg)
109-
if m.Path == "" || modload.MainModules.Contains(m.Path) {
109+
if m.Path == "" || modload.LoaderState.MainModules.Contains(m.Path) {
110110
continue
111111
}
112112
modpkgs[m] = append(modpkgs[m], pkg)
@@ -116,13 +116,13 @@ func RunVendor(ctx context.Context, vendorE bool, vendorO string, args []string)
116116
includeAllReplacements := false
117117
includeGoVersions := false
118118
isExplicit := map[module.Version]bool{}
119-
gv := modload.MainModules.GoVersion()
119+
gv := modload.LoaderState.MainModules.GoVersion()
120120
if gover.Compare(gv, "1.14") >= 0 && (modload.FindGoWork(base.Cwd()) != "" || modload.ModFile().Go != nil) {
121121
// If the Go version is at least 1.14, annotate all explicit 'require' and
122122
// 'replace' targets found in the go.mod file so that we can perform a
123123
// stronger consistency check when -mod=vendor is set.
124-
for _, m := range modload.MainModules.Versions() {
125-
if modFile := modload.MainModules.ModFile(m); modFile != nil {
124+
for _, m := range modload.LoaderState.MainModules.Versions() {
125+
if modFile := modload.LoaderState.MainModules.ModFile(m); modFile != nil {
126126
for _, r := range modFile.Require {
127127
isExplicit[r.Mod] = true
128128
}
@@ -156,7 +156,7 @@ func RunVendor(ctx context.Context, vendorE bool, vendorO string, args []string)
156156
w = io.MultiWriter(&buf, os.Stderr)
157157
}
158158

159-
if modload.MainModules.WorkFile() != nil {
159+
if modload.LoaderState.MainModules.WorkFile() != nil {
160160
fmt.Fprintf(w, "## workspace\n")
161161
}
162162

@@ -192,8 +192,8 @@ func RunVendor(ctx context.Context, vendorE bool, vendorO string, args []string)
192192
// Record unused and wildcard replacements at the end of the modules.txt file:
193193
// without access to the complete build list, the consumer of the vendor
194194
// directory can't otherwise determine that those replacements had no effect.
195-
for _, m := range modload.MainModules.Versions() {
196-
if workFile := modload.MainModules.WorkFile(); workFile != nil {
195+
for _, m := range modload.LoaderState.MainModules.Versions() {
196+
if workFile := modload.LoaderState.MainModules.WorkFile(); workFile != nil {
197197
for _, r := range workFile.Replace {
198198
if replacementWritten[r.Old] {
199199
// We already recorded this replacement.
@@ -208,7 +208,7 @@ func RunVendor(ctx context.Context, vendorE bool, vendorO string, args []string)
208208
}
209209
}
210210
}
211-
if modFile := modload.MainModules.ModFile(m); modFile != nil {
211+
if modFile := modload.LoaderState.MainModules.ModFile(m); modFile != nil {
212212
for _, r := range modFile.Replace {
213213
if replacementWritten[r.Old] {
214214
// We already recorded this replacement.
@@ -315,7 +315,7 @@ func vendorPkg(vdir, pkg string) {
315315
}
316316
}
317317
var embedPatterns []string
318-
if gover.Compare(modload.MainModules.GoVersion(), "1.22") >= 0 {
318+
if gover.Compare(modload.LoaderState.MainModules.GoVersion(), "1.22") >= 0 {
319319
embedPatterns = bp.EmbedPatterns
320320
} else {
321321
// Maintain the behavior of https://github.com/golang/go/issues/63473
@@ -431,7 +431,7 @@ func matchPotentialSourceFile(dir string, info fs.DirEntry) bool {
431431
return false
432432
}
433433
if info.Name() == "go.mod" || info.Name() == "go.sum" {
434-
if gv := modload.MainModules.GoVersion(); gover.Compare(gv, "1.17") >= 0 {
434+
if gv := modload.LoaderState.MainModules.GoVersion(); gover.Compare(gv, "1.17") >= 0 {
435435
// As of Go 1.17, we strip go.mod and go.sum files from dependency modules.
436436
// Otherwise, 'go' commands invoked within the vendor subtree may misidentify
437437
// an arbitrary directory within the vendor tree as a module root.

src/cmd/go/internal/modcmd/verify.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ func verifyMod(ctx context.Context, mod module.Version) []error {
9494
// "go" and "toolchain" have no disk footprint; nothing to verify.
9595
return nil
9696
}
97-
if modload.MainModules.Contains(mod.Path) {
97+
if modload.LoaderState.MainModules.Contains(mod.Path) {
9898
return nil
9999
}
100100
var errs []error

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

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -426,7 +426,7 @@ func runGet(ctx context.Context, cmd *base.Command, args []string) {
426426

427427
if gowork := modload.FindGoWork(base.Cwd()); gowork != "" {
428428
wf, err := modload.ReadWorkFile(gowork)
429-
if err == nil && modload.UpdateWorkGoVersion(wf, modload.MainModules.GoVersion()) {
429+
if err == nil && modload.UpdateWorkGoVersion(wf, modload.LoaderState.MainModules.GoVersion()) {
430430
modload.WriteWorkFile(gowork, wf)
431431
}
432432
}
@@ -722,7 +722,7 @@ func (r *resolver) queryNone(ctx context.Context, q *query) {
722722
if !q.isWildcard() {
723723
q.pathOnce(q.pattern, func() pathSet {
724724
hasModRoot := modload.HasModRoot()
725-
if hasModRoot && modload.MainModules.Contains(q.pattern) {
725+
if hasModRoot && modload.LoaderState.MainModules.Contains(q.pattern) {
726726
v := module.Version{Path: q.pattern}
727727
// The user has explicitly requested to downgrade their own module to
728728
// version "none". This is not an entirely unreasonable request: it
@@ -746,7 +746,7 @@ func (r *resolver) queryNone(ctx context.Context, q *query) {
746746
continue
747747
}
748748
q.pathOnce(curM.Path, func() pathSet {
749-
if modload.HasModRoot() && curM.Version == "" && modload.MainModules.Contains(curM.Path) {
749+
if modload.HasModRoot() && curM.Version == "" && modload.LoaderState.MainModules.Contains(curM.Path) {
750750
return errSet(&modload.QueryMatchesMainModulesError{MainModules: []module.Version{curM}, Pattern: q.pattern, Query: q.version})
751751
}
752752
return pathSet{mod: module.Version{Path: curM.Path, Version: "none"}}
@@ -766,13 +766,13 @@ func (r *resolver) performLocalQueries(ctx context.Context) {
766766

767767
// Absolute paths like C:\foo and relative paths like ../foo... are
768768
// restricted to matching packages in the main module.
769-
pkgPattern, mainModule := modload.MainModules.DirImportPath(ctx, q.pattern)
769+
pkgPattern, mainModule := modload.LoaderState.MainModules.DirImportPath(ctx, q.pattern)
770770
if pkgPattern == "." {
771771
modload.MustHaveModRoot()
772-
versions := modload.MainModules.Versions()
772+
versions := modload.LoaderState.MainModules.Versions()
773773
modRoots := make([]string, 0, len(versions))
774774
for _, m := range versions {
775-
modRoots = append(modRoots, modload.MainModules.ModRoot(m))
775+
modRoots = append(modRoots, modload.LoaderState.MainModules.ModRoot(m))
776776
}
777777
var plural string
778778
if len(modRoots) != 1 {
@@ -792,7 +792,7 @@ func (r *resolver) performLocalQueries(ctx context.Context) {
792792
}
793793
if !q.isWildcard() {
794794
modload.MustHaveModRoot()
795-
return errSet(fmt.Errorf("%s%s is not a package in module rooted at %s", q.pattern, absDetail, modload.MainModules.ModRoot(mainModule)))
795+
return errSet(fmt.Errorf("%s%s is not a package in module rooted at %s", q.pattern, absDetail, modload.LoaderState.MainModules.ModRoot(mainModule)))
796796
}
797797
search.WarnUnmatched([]*search.Match{match})
798798
return pathSet{}
@@ -848,7 +848,7 @@ func (r *resolver) queryWildcard(ctx context.Context, q *query) {
848848
return pathSet{}
849849
}
850850

851-
if modload.MainModules.Contains(curM.Path) && !versionOkForMainModule(q.version) {
851+
if modload.LoaderState.MainModules.Contains(curM.Path) && !versionOkForMainModule(q.version) {
852852
if q.matchesPath(curM.Path) {
853853
return errSet(&modload.QueryMatchesMainModulesError{
854854
MainModules: []module.Version{curM},
@@ -1065,7 +1065,7 @@ func (r *resolver) queryPath(ctx context.Context, q *query) {
10651065
// pattern is "tool".
10661066
func (r *resolver) performToolQueries(ctx context.Context) {
10671067
for _, q := range r.toolQueries {
1068-
for tool := range modload.MainModules.Tools() {
1068+
for tool := range modload.LoaderState.MainModules.Tools() {
10691069
q.pathOnce(tool, func() pathSet {
10701070
pkgMods, err := r.queryPackages(ctx, tool, q.version, r.initialSelected)
10711071
return pathSet{pkgMods: pkgMods, err: err}
@@ -1082,10 +1082,10 @@ func (r *resolver) performWorkQueries(ctx context.Context) {
10821082
// TODO(matloob): Maybe export MainModules.mustGetSingleMainModule and call that.
10831083
// There are a few other places outside the modload package where we expect
10841084
// a single main module.
1085-
if len(modload.MainModules.Versions()) != 1 {
1085+
if len(modload.LoaderState.MainModules.Versions()) != 1 {
10861086
panic("internal error: number of main modules is not exactly one in resolution phase of go get")
10871087
}
1088-
mainModule := modload.MainModules.Versions()[0]
1088+
mainModule := modload.LoaderState.MainModules.Versions()[0]
10891089

10901090
// We know what the result is going to be, assuming the main module is not
10911091
// empty, (it's the main module itself) but first check to see that there
@@ -1496,7 +1496,7 @@ func (r *resolver) disambiguate(cs pathSet) (filtered pathSet, isPackage bool, m
14961496
continue
14971497
}
14981498

1499-
if modload.MainModules.Contains(m.Path) {
1499+
if modload.LoaderState.MainModules.Contains(m.Path) {
15001500
if m.Version == "" {
15011501
return pathSet{}, true, m, true
15021502
}
@@ -1612,7 +1612,7 @@ func (r *resolver) checkPackageProblems(ctx context.Context, pkgPatterns []strin
16121612
// info, but switch back to single module mode when fetching sums so that we update
16131613
// the single module's go.sum file.
16141614
var exitWorkspace func()
1615-
if r.workspace != nil && r.workspace.hasModule(modload.MainModules.Versions()[0].Path) {
1615+
if r.workspace != nil && r.workspace.hasModule(modload.LoaderState.MainModules.Versions()[0].Path) {
16161616
var err error
16171617
exitWorkspace, err = modload.EnterWorkspace(ctx)
16181618
if err != nil {
@@ -1951,7 +1951,7 @@ func (r *resolver) resolve(q *query, m module.Version) {
19511951
panic("internal error: resolving a module.Version with an empty path")
19521952
}
19531953

1954-
if modload.MainModules.Contains(m.Path) && m.Version != "" {
1954+
if modload.LoaderState.MainModules.Contains(m.Path) && m.Version != "" {
19551955
reportError(q, &modload.QueryMatchesMainModulesError{
19561956
MainModules: []module.Version{{Path: m.Path}},
19571957
Pattern: q.pattern,
@@ -1983,7 +1983,7 @@ func (r *resolver) updateBuildList(ctx context.Context, additions []module.Versi
19831983

19841984
resolved := make([]module.Version, 0, len(r.resolvedVersion))
19851985
for mPath, rv := range r.resolvedVersion {
1986-
if !modload.MainModules.Contains(mPath) {
1986+
if !modload.LoaderState.MainModules.Contains(mPath) {
19871987
resolved = append(resolved, module.Version{Path: mPath, Version: rv.version})
19881988
}
19891989
}

src/cmd/go/internal/modget/query.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ func (q *query) validate() error {
192192
// request that we remove all module requirements, leaving only the main
193193
// module and standard library. Perhaps we should implement that someday.
194194
return &modload.QueryUpgradesAllError{
195-
MainModules: modload.MainModules.Versions(),
195+
MainModules: modload.LoaderState.MainModules.Versions(),
196196
Query: q.version,
197197
}
198198
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,7 @@ func addDeprecation(ctx context.Context, m *modinfo.ModulePublic) {
290290
// in rs (which may be nil to indicate that m was not loaded from a requirement
291291
// graph).
292292
func moduleInfo(ctx context.Context, rs *Requirements, m module.Version, mode ListMode, reuse map[module.Version]*modinfo.ModulePublic) *modinfo.ModulePublic {
293-
if m.Version == "" && MainModules.Contains(m.Path) {
293+
if m.Version == "" && LoaderState.MainModules.Contains(m.Path) {
294294
info := &modinfo.ModulePublic{
295295
Path: m.Path,
296296
Version: m.Version,
@@ -301,7 +301,7 @@ func moduleInfo(ctx context.Context, rs *Requirements, m module.Version, mode Li
301301
} else {
302302
panic("internal error: GoVersion not set for main module")
303303
}
304-
if modRoot := MainModules.ModRoot(m); modRoot != "" {
304+
if modRoot := LoaderState.MainModules.ModRoot(m); modRoot != "" {
305305
info.Dir = modRoot
306306
info.GoMod = modFilePath(modRoot)
307307
}

0 commit comments

Comments
 (0)