Skip to content

Commit 1623927

Browse files
committed
cmd/go: refactor usage of requirements
This commit refactors usage of the global variable `requirements` to the global LoaderState field 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 'ex { requirements -> LoaderState.requirements }' rf 'add State.MainModules \ // requirements is the requirement graph for the main module.\ //\ // It is always non-nil if the main module'\\\''s go.mod file has been\ // loaded.\ //\ // This variable should only be read from the loadModFile\ // function, and should only be written in the loadModFile and\ // commitRequirements functions. All other functions that need or\ // produce a *Requirements should accept and/or return an explicit\ // parameter.' rf 'rm requirements' Change-Id: I9d7d1d301a9e89f9214ce632fa5b656dd2940f39 Reviewed-on: https://go-review.googlesource.com/c/go/+/698061 LUCI-TryBot-Result: Go LUCI <[email protected]> Reviewed-by: Michael Matloob <[email protected]> Reviewed-by: Michael Matloob <[email protected]>
1 parent a1661e7 commit 1623927

File tree

4 files changed

+40
-39
lines changed

4 files changed

+40
-39
lines changed

src/cmd/go/internal/modload/buildlist.go

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -85,16 +85,6 @@ type cachedGraph struct {
8585
err error // If err is non-nil, mg may be incomplete (but must still be non-nil).
8686
}
8787

88-
// requirements is the requirement graph for the main module.
89-
//
90-
// It is always non-nil if the main module's go.mod file has been loaded.
91-
//
92-
// This variable should only be read from the loadModFile function, and should
93-
// only be written in the loadModFile and commitRequirements functions.
94-
// All other functions that need or produce a *Requirements should
95-
// accept and/or return an explicit parameter.
96-
var requirements *Requirements
97-
9888
func mustHaveGoRoot(roots []module.Version) {
9989
for _, m := range roots {
10090
if m.Path == "go" {
@@ -589,7 +579,7 @@ func LoadModGraph(ctx context.Context, goVersion string) (*ModuleGraph, error) {
589579
if err != nil {
590580
return nil, err
591581
}
592-
requirements = rs
582+
LoaderState.requirements = rs
593583
return mg, nil
594584
}
595585

@@ -654,7 +644,7 @@ func EditBuildList(ctx context.Context, add, mustSelect []module.Version) (chang
654644
if err != nil {
655645
return false, err
656646
}
657-
requirements = rs
647+
LoaderState.requirements = rs
658648
return changed, nil
659649
}
660650

src/cmd/go/internal/modload/init.go

Lines changed: 33 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ var (
5959
// EnterModule resets MainModules and requirements to refer to just this one module.
6060
func EnterModule(ctx context.Context, enterModroot string) {
6161
LoaderState.MainModules = nil // reset MainModules
62-
requirements = nil
62+
LoaderState.requirements = nil
6363
workFilePath = "" // Force module mode
6464
modfetch.Reset()
6565

@@ -90,7 +90,7 @@ func EnterWorkspace(ctx context.Context) (exit func(), err error) {
9090

9191
// Update the content of the previous main module, and recompute the requirements.
9292
*LoaderState.MainModules.ModFile(mm) = *updatedmodfile
93-
requirements = requirementsFromModFiles(ctx, LoaderState.MainModules.workFile, slices.Collect(maps.Values(LoaderState.MainModules.modFiles)), nil)
93+
LoaderState.requirements = requirementsFromModFiles(ctx, LoaderState.MainModules.workFile, slices.Collect(maps.Values(LoaderState.MainModules.modFiles)), nil)
9494

9595
return func() {
9696
setState(oldstate)
@@ -395,15 +395,15 @@ func setState(s State) State {
395395
modRoots: LoaderState.modRoots,
396396
modulesEnabled: cfg.ModulesEnabled,
397397
MainModules: LoaderState.MainModules,
398-
requirements: requirements,
398+
requirements: LoaderState.requirements,
399399
}
400400
LoaderState.initialized = s.initialized
401401
LoaderState.ForceUseModules = s.ForceUseModules
402402
LoaderState.RootMode = s.RootMode
403403
LoaderState.modRoots = s.modRoots
404404
cfg.ModulesEnabled = s.modulesEnabled
405405
LoaderState.MainModules = s.MainModules
406-
requirements = s.requirements
406+
LoaderState.requirements = s.requirements
407407
workFilePath = s.workFilePath
408408
// The modfetch package's global state is used to compute
409409
// the go.sum file, so save and restore it along with the
@@ -430,9 +430,20 @@ type State struct {
430430
modRoots []string
431431
modulesEnabled bool
432432
MainModules *MainModuleSet
433-
requirements *Requirements
434-
workFilePath string
435-
modfetchState modfetch.State
433+
434+
// requirements is the requirement graph for the main module.
435+
//
436+
// It is always non-nil if the main module's go.mod file has been
437+
// loaded.
438+
//
439+
// This variable should only be read from the loadModFile
440+
// function, and should only be written in the loadModFile and
441+
// commitRequirements functions. All other functions that need or
442+
// produce a *Requirements should accept and/or return an explicit
443+
// parameter.
444+
requirements *Requirements
445+
workFilePath string
446+
modfetchState modfetch.State
436447
}
437448

438449
func NewState() *State { return &State{} }
@@ -869,8 +880,8 @@ func LoadModFile(ctx context.Context) *Requirements {
869880
}
870881

871882
func loadModFile(ctx context.Context, opts *PackageOpts) (*Requirements, error) {
872-
if requirements != nil {
873-
return requirements, nil
883+
if LoaderState.requirements != nil {
884+
return LoaderState.requirements, nil
874885
}
875886

876887
Init()
@@ -939,14 +950,14 @@ func loadModFile(ctx context.Context, opts *PackageOpts) (*Requirements, error)
939950
}
940951
}
941952
rawGoVersion.Store(mainModule, goVersion)
942-
requirements = newRequirements(pruning, roots, direct)
953+
LoaderState.requirements = newRequirements(pruning, roots, direct)
943954
if cfg.BuildMod == "vendor" {
944955
// For issue 56536: Some users may have GOFLAGS=-mod=vendor set.
945956
// Make sure it behaves as though the fake module is vendored
946957
// with no dependencies.
947-
requirements.initVendor(nil)
958+
LoaderState.requirements.initVendor(nil)
948959
}
949-
return requirements, nil
960+
return LoaderState.requirements, nil
950961
}
951962

952963
var modFiles []*modfile.File
@@ -1035,7 +1046,7 @@ func loadModFile(ctx context.Context, opts *PackageOpts) (*Requirements, error)
10351046

10361047
if inWorkspaceMode() {
10371048
// We don't need to update the mod file so return early.
1038-
requirements = rs
1049+
LoaderState.requirements = rs
10391050
return rs, nil
10401051
}
10411052

@@ -1081,8 +1092,8 @@ func loadModFile(ctx context.Context, opts *PackageOpts) (*Requirements, error)
10811092
}
10821093
}
10831094

1084-
requirements = rs
1085-
return requirements, nil
1095+
LoaderState.requirements = rs
1096+
return LoaderState.requirements, nil
10861097
}
10871098

10881099
func errWorkTooOld(gomod string, wf *modfile.WorkFile, goVers string) error {
@@ -1162,7 +1173,7 @@ func CreateModFile(ctx context.Context, modPath string) {
11621173
if err != nil {
11631174
base.Fatal(err)
11641175
}
1165-
requirements = rs
1176+
LoaderState.requirements = rs
11661177
if err := commitRequirements(ctx, WriteOpts{}); err != nil {
11671178
base.Fatal(err)
11681179
}
@@ -1801,7 +1812,7 @@ type WriteOpts struct {
18011812

18021813
// WriteGoMod writes the current build list back to go.mod.
18031814
func WriteGoMod(ctx context.Context, opts WriteOpts) error {
1804-
requirements = LoadModFile(ctx)
1815+
LoaderState.requirements = LoadModFile(ctx)
18051816
return commitRequirements(ctx, opts)
18061817
}
18071818

@@ -1828,7 +1839,7 @@ func UpdateGoModFromReqs(ctx context.Context, opts WriteOpts) (before, after []b
18281839
var list []*modfile.Require
18291840
toolchain := ""
18301841
goVersion := ""
1831-
for _, m := range requirements.rootModules {
1842+
for _, m := range LoaderState.requirements.rootModules {
18321843
if m.Path == "go" {
18331844
goVersion = m.Version
18341845
continue
@@ -1839,7 +1850,7 @@ func UpdateGoModFromReqs(ctx context.Context, opts WriteOpts) (before, after []b
18391850
}
18401851
list = append(list, &modfile.Require{
18411852
Mod: m,
1842-
Indirect: !requirements.direct[m.Path],
1853+
Indirect: !LoaderState.requirements.direct[m.Path],
18431854
})
18441855
}
18451856

@@ -1913,7 +1924,7 @@ func commitRequirements(ctx context.Context, opts WriteOpts) (err error) {
19131924
if inWorkspaceMode() {
19141925
// go.mod files aren't updated in workspace mode, but we still want to
19151926
// update the go.work.sum file.
1916-
return modfetch.WriteGoSum(ctx, keepSums(ctx, loaded, requirements, addBuildListZipSums), mustHaveCompleteRequirements())
1927+
return modfetch.WriteGoSum(ctx, keepSums(ctx, loaded, LoaderState.requirements, addBuildListZipSums), mustHaveCompleteRequirements())
19171928
}
19181929
_, updatedGoMod, modFile, err := UpdateGoModFromReqs(ctx, opts)
19191930
if err != nil {
@@ -1937,7 +1948,7 @@ func commitRequirements(ctx context.Context, opts WriteOpts) (err error) {
19371948
// Don't write go.mod, but write go.sum in case we added or trimmed sums.
19381949
// 'go mod init' shouldn't write go.sum, since it will be incomplete.
19391950
if cfg.CmdName != "mod init" {
1940-
if err := modfetch.WriteGoSum(ctx, keepSums(ctx, loaded, requirements, addBuildListZipSums), mustHaveCompleteRequirements()); err != nil {
1951+
if err := modfetch.WriteGoSum(ctx, keepSums(ctx, loaded, LoaderState.requirements, addBuildListZipSums), mustHaveCompleteRequirements()); err != nil {
19411952
return err
19421953
}
19431954
}
@@ -1960,7 +1971,7 @@ func commitRequirements(ctx context.Context, opts WriteOpts) (err error) {
19601971
// 'go mod init' shouldn't write go.sum, since it will be incomplete.
19611972
if cfg.CmdName != "mod init" {
19621973
if err == nil {
1963-
err = modfetch.WriteGoSum(ctx, keepSums(ctx, loaded, requirements, addBuildListZipSums), mustHaveCompleteRequirements())
1974+
err = modfetch.WriteGoSum(ctx, keepSums(ctx, loaded, LoaderState.requirements, addBuildListZipSums), mustHaveCompleteRequirements())
19641975
}
19651976
}
19661977
}()

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ func ListModules(ctx context.Context, args []string, mode ListMode, reuseFile st
109109
}
110110

111111
if err == nil {
112-
requirements = rs
112+
LoaderState.requirements = rs
113113
// TODO(#61605): The extra ListU clause fixes a problem with Go 1.21rc3
114114
// where "go mod tidy" and "go list -m -u all" fight over whether the go.sum
115115
// should be considered up-to-date. The fix for now is to always treat the

src/cmd/go/internal/modload/load.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -455,7 +455,7 @@ func LoadPackages(ctx context.Context, opts PackageOpts, patterns ...string) (ma
455455
if opts.TidyDiff {
456456
cfg.BuildMod = "readonly"
457457
loaded = ld
458-
requirements = loaded.requirements
458+
LoaderState.requirements = loaded.requirements
459459
currentGoMod, updatedGoMod, _, err := UpdateGoModFromReqs(ctx, WriteOpts{})
460460
if err != nil {
461461
base.Fatal(err)
@@ -466,7 +466,7 @@ func LoadPackages(ctx context.Context, opts PackageOpts, patterns ...string) (ma
466466
// Dropping compatibility for 1.16 may result in a strictly smaller go.sum.
467467
// Update the keep map with only the loaded.requirements.
468468
if gover.Compare(compatVersion, "1.16") > 0 {
469-
keep = keepSums(ctx, loaded, requirements, addBuildListZipSums)
469+
keep = keepSums(ctx, loaded, LoaderState.requirements, addBuildListZipSums)
470470
}
471471
currentGoSum, tidyGoSum := modfetch.TidyGoSum(keep)
472472
goSumDiff := diff.Diff("current/go.sum", currentGoSum, "tidy/go.sum", tidyGoSum)
@@ -505,7 +505,7 @@ func LoadPackages(ctx context.Context, opts PackageOpts, patterns ...string) (ma
505505
// to call WriteGoMod itself) or if ResolveMissingImports is false (the
506506
// command wants to examine the package graph as-is).
507507
loaded = ld
508-
requirements = loaded.requirements
508+
LoaderState.requirements = loaded.requirements
509509

510510
for _, pkg := range ld.pkgs {
511511
if !pkg.isTest() {
@@ -788,7 +788,7 @@ func ImportFromFiles(ctx context.Context, gofiles []string) {
788788
return roots
789789
},
790790
})
791-
requirements = loaded.requirements
791+
LoaderState.requirements = loaded.requirements
792792

793793
if !ExplicitWriteGoMod {
794794
if err := commitRequirements(ctx, WriteOpts{}); err != nil {

0 commit comments

Comments
 (0)