Skip to content

Commit 099e002

Browse files
committed
cmd/go/internal/modfetch: consolidate global vars
This commit conslidates global variables represeting the current modfetch state into a single variable in preparation for injection via the rsc.io/rf tool. This commit is part of the larger effort to eliminate global module loader state. [git-generate] cd src/cmd/go/internal/modfetch rf ' add State func NewState() *State {\ s := new(State)\ s.lookupCache = new(par.Cache[lookupCacheKey, Repo])\ s.downloadCache = new(par.ErrCache[module.Version, string])\ return s\ } add State var ModuleFetchState *State = NewState() mv State.goSumFile State.GoSumFile add State:/^[[:space:]]*GoSumFile /-0 // path to go.sum; set by package modload ex { GoSumFile -> ModuleFetchState.GoSumFile } mv State.workspaceGoSumFiles State.WorkspaceGoSumFiles add State:/^[[:space:]]*WorkspaceGoSumFiles /-0 // path to module go.sums in workspace; set by package modload ex { WorkspaceGoSumFiles -> ModuleFetchState.WorkspaceGoSumFiles } add State:/^[[:space:]]*lookupCache /-0 // The Lookup cache is used cache the work done by Lookup.\ // It is important that the global functions of this package that access it do not\ // do so after they return. add State:/^[[:space:]]*downloadCache /-0 // The downloadCache is used to cache the operation of downloading a module to disk\ // (if it'\\\''s not already downloaded) and getting the directory it was downloaded to.\ // It is important that downloadCache must not be accessed by any of the exported\ // functions of this package after they return, because it can be modified by the\ // non-thread-safe SetState function. add State:/^[[:space:]]*downloadCache.*$/ // version → directory; ex { lookupCache -> ModuleFetchState.lookupCache downloadCache -> ModuleFetchState.downloadCache } rm 'lookupCache' rm 'downloadCache' ' for dir in modload ; do cd ../${dir} rf ' ex { import "cmd/go/internal/modfetch" modfetch.GoSumFile -> modfetch.ModuleFetchState.GoSumFile modfetch.WorkspaceGoSumFiles -> modfetch.ModuleFetchState.WorkspaceGoSumFiles } ' done cd ../modfetch rf ' rm GoSumFile rm WorkspaceGoSumFiles ' Change-Id: Iaa0f8a40192127457a539120eb94337940cb8d4a Reviewed-on: https://go-review.googlesource.com/c/go/+/719700 Reviewed-by: Michael Matloob <[email protected]> Reviewed-by: Michael Matloob <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]>
1 parent 0283753 commit 099e002

File tree

3 files changed

+47
-42
lines changed

3 files changed

+47
-42
lines changed

src/cmd/go/internal/modfetch/fetch.go

Lines changed: 43 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,6 @@ import (
3535
modzip "golang.org/x/mod/zip"
3636
)
3737

38-
// The downloadCache is used to cache the operation of downloading a module to disk
39-
// (if it's not already downloaded) and getting the directory it was downloaded to.
40-
// It is important that downloadCache must not be accessed by any of the exported
41-
// functions of this package after they return, because it can be modified by the
42-
// non-thread-safe SetState function
43-
var downloadCache = new(par.ErrCache[module.Version, string]) // version → directory;
44-
4538
var ErrToolchain = errors.New("internal error: invalid operation on toolchain module")
4639

4740
// Download downloads the specific module version to the
@@ -56,7 +49,7 @@ func Download(ctx context.Context, mod module.Version) (dir string, err error) {
5649
}
5750

5851
// The par.Cache here avoids duplicate work.
59-
return downloadCache.Do(mod, func() (string, error) {
52+
return ModuleFetchState.downloadCache.Do(mod, func() (string, error) {
6053
dir, err := download(ctx, mod)
6154
if err != nil {
6255
return "", err
@@ -85,7 +78,7 @@ func Unzip(ctx context.Context, mod module.Version, zipfile string) (dir string,
8578
base.Fatal(err)
8679
}
8780

88-
return downloadCache.Do(mod, func() (string, error) {
81+
return ModuleFetchState.downloadCache.Do(mod, func() (string, error) {
8982
ctx, span := trace.StartSpan(ctx, "modfetch.Unzip "+mod.String())
9083
defer span.Done()
9184

@@ -444,9 +437,6 @@ func RemoveAll(dir string) error {
444437
// accessed by any of the exported functions of this package after they return, because
445438
// they can be modified by the non-thread-safe SetState function.
446439

447-
var GoSumFile string // path to go.sum; set by package modload
448-
var WorkspaceGoSumFiles []string // path to module go.sums in workspace; set by package modload
449-
450440
type modSum struct {
451441
mod module.Version
452442
sum string
@@ -471,11 +461,31 @@ type modSumStatus struct {
471461

472462
// State holds a snapshot of the global state of the modfetch package.
473463
type State struct {
474-
goSumFile string
475-
workspaceGoSumFiles []string
476-
lookupCache *par.Cache[lookupCacheKey, Repo]
477-
downloadCache *par.ErrCache[module.Version, string]
478-
sumState sumState
464+
// path to go.sum; set by package modload
465+
GoSumFile string
466+
// path to module go.sums in workspace; set by package modload
467+
WorkspaceGoSumFiles []string
468+
// The Lookup cache is used cache the work done by Lookup.
469+
// It is important that the global functions of this package that access it do not
470+
// do so after they return.
471+
lookupCache *par.Cache[lookupCacheKey, Repo]
472+
// The downloadCache is used to cache the operation of downloading a module to disk
473+
// (if it's not already downloaded) and getting the directory it was downloaded to.
474+
// It is important that downloadCache must not be accessed by any of the exported
475+
// functions of this package after they return, because it can be modified by the
476+
// non-thread-safe SetState function.
477+
downloadCache *par.ErrCache[module.Version, string] // version → directory;
478+
479+
sumState sumState
480+
}
481+
482+
var ModuleFetchState *State = NewState()
483+
484+
func NewState() *State {
485+
s := new(State)
486+
s.lookupCache = new(par.Cache[lookupCacheKey, Repo])
487+
s.downloadCache = new(par.ErrCache[module.Version, string])
488+
return s
479489
}
480490

481491
// Reset resets globals in the modfetch package, so previous loads don't affect
@@ -500,20 +510,20 @@ func SetState(newState State) (oldState State) {
500510
defer goSum.mu.Unlock()
501511

502512
oldState = State{
503-
goSumFile: GoSumFile,
504-
workspaceGoSumFiles: WorkspaceGoSumFiles,
505-
lookupCache: lookupCache,
506-
downloadCache: downloadCache,
513+
GoSumFile: ModuleFetchState.GoSumFile,
514+
WorkspaceGoSumFiles: ModuleFetchState.WorkspaceGoSumFiles,
515+
lookupCache: ModuleFetchState.lookupCache,
516+
downloadCache: ModuleFetchState.downloadCache,
507517
sumState: goSum.sumState,
508518
}
509519

510-
GoSumFile = newState.goSumFile
511-
WorkspaceGoSumFiles = newState.workspaceGoSumFiles
520+
ModuleFetchState.GoSumFile = newState.GoSumFile
521+
ModuleFetchState.WorkspaceGoSumFiles = newState.WorkspaceGoSumFiles
512522
// Uses of lookupCache and downloadCache both can call checkModSum,
513523
// which in turn sets the used bit on goSum.status for modules.
514524
// Set (or reset) them so used can be computed properly.
515-
lookupCache = newState.lookupCache
516-
downloadCache = newState.downloadCache
525+
ModuleFetchState.lookupCache = newState.lookupCache
526+
ModuleFetchState.downloadCache = newState.downloadCache
517527
// Set, or reset all fields on goSum. If being reset to empty, it will be initialized later.
518528
goSum.sumState = newState.sumState
519529

@@ -525,7 +535,7 @@ func SetState(newState State) (oldState State) {
525535
// use of go.sum is now enabled.
526536
// The goSum lock must be held.
527537
func initGoSum() (bool, error) {
528-
if GoSumFile == "" {
538+
if ModuleFetchState.GoSumFile == "" {
529539
return false, nil
530540
}
531541
if goSum.m != nil {
@@ -536,15 +546,15 @@ func initGoSum() (bool, error) {
536546
goSum.status = make(map[modSum]modSumStatus)
537547
goSum.w = make(map[string]map[module.Version][]string)
538548

539-
for _, f := range WorkspaceGoSumFiles {
549+
for _, f := range ModuleFetchState.WorkspaceGoSumFiles {
540550
goSum.w[f] = make(map[module.Version][]string)
541551
_, err := readGoSumFile(goSum.w[f], f)
542552
if err != nil {
543553
return false, err
544554
}
545555
}
546556

547-
enabled, err := readGoSumFile(goSum.m, GoSumFile)
557+
enabled, err := readGoSumFile(goSum.m, ModuleFetchState.GoSumFile)
548558
goSum.enabled = enabled
549559
return enabled, err
550560
}
@@ -790,7 +800,7 @@ func checkModSum(mod module.Version, h string) error {
790800
// goSum.mu must be locked.
791801
func haveModSumLocked(mod module.Version, h string) bool {
792802
sumFileName := "go.sum"
793-
if strings.HasSuffix(GoSumFile, "go.work.sum") {
803+
if strings.HasSuffix(ModuleFetchState.GoSumFile, "go.work.sum") {
794804
sumFileName = "go.work.sum"
795805
}
796806
for _, vh := range goSum.m[mod] {
@@ -934,7 +944,7 @@ Outer:
934944
if readonly {
935945
return ErrGoSumDirty
936946
}
937-
if fsys.Replaced(GoSumFile) {
947+
if fsys.Replaced(ModuleFetchState.GoSumFile) {
938948
base.Fatalf("go: updates to go.sum needed, but go.sum is part of the overlay specified with -overlay")
939949
}
940950

@@ -944,7 +954,7 @@ Outer:
944954
defer unlock()
945955
}
946956

947-
err := lockedfile.Transform(GoSumFile, func(data []byte) ([]byte, error) {
957+
err := lockedfile.Transform(ModuleFetchState.GoSumFile, func(data []byte) ([]byte, error) {
948958
tidyGoSum := tidyGoSum(data, keep)
949959
return tidyGoSum, nil
950960
})
@@ -963,7 +973,7 @@ Outer:
963973
func TidyGoSum(keep map[module.Version]bool) (before, after []byte) {
964974
goSum.mu.Lock()
965975
defer goSum.mu.Unlock()
966-
before, err := lockedfile.Read(GoSumFile)
976+
before, err := lockedfile.Read(ModuleFetchState.GoSumFile)
967977
if err != nil && !errors.Is(err, fs.ErrNotExist) {
968978
base.Fatalf("reading go.sum: %v", err)
969979
}
@@ -980,7 +990,7 @@ func tidyGoSum(data []byte, keep map[module.Version]bool) []byte {
980990
// truncated the file to remove erroneous hashes, and we shouldn't restore
981991
// them without good reason.
982992
goSum.m = make(map[module.Version][]string, len(goSum.m))
983-
readGoSum(goSum.m, GoSumFile, data)
993+
readGoSum(goSum.m, ModuleFetchState.GoSumFile, data)
984994
for ms, st := range goSum.status {
985995
if st.used && !sumInWorkspaceModulesLocked(ms.mod) {
986996
addModSumLocked(ms.mod, ms.sum)

src/cmd/go/internal/modfetch/repo.go

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -184,11 +184,6 @@ type RevInfo struct {
184184
// To avoid version control access except when absolutely necessary,
185185
// Lookup does not attempt to connect to the repository itself.
186186

187-
// The Lookup cache is used cache the work done by Lookup.
188-
// It is important that the global functions of this package that access it do not
189-
// do so after they return.
190-
var lookupCache = new(par.Cache[lookupCacheKey, Repo])
191-
192187
type lookupCacheKey struct {
193188
proxy, path string
194189
}
@@ -210,7 +205,7 @@ func Lookup(ctx context.Context, proxy, path string) Repo {
210205
defer logCall("Lookup(%q, %q)", proxy, path)()
211206
}
212207

213-
return lookupCache.Do(lookupCacheKey{proxy, path}, func() Repo {
208+
return ModuleFetchState.lookupCache.Do(lookupCacheKey{proxy, path}, func() Repo {
214209
return newCachingRepo(ctx, path, func(ctx context.Context) (Repo, error) {
215210
r, err := lookup(ctx, proxy, path)
216211
if err == nil && traceRepo {

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -928,9 +928,9 @@ func loadModFile(loaderstate *State, ctx context.Context, opts *PackageOpts) (*R
928928
}
929929
for _, modRoot := range loaderstate.modRoots {
930930
sumFile := strings.TrimSuffix(modFilePath(modRoot), ".mod") + ".sum"
931-
modfetch.WorkspaceGoSumFiles = append(modfetch.WorkspaceGoSumFiles, sumFile)
931+
modfetch.ModuleFetchState.WorkspaceGoSumFiles = append(modfetch.ModuleFetchState.WorkspaceGoSumFiles, sumFile)
932932
}
933-
modfetch.GoSumFile = loaderstate.workFilePath + ".sum"
933+
modfetch.ModuleFetchState.GoSumFile = loaderstate.workFilePath + ".sum"
934934
} else if len(loaderstate.modRoots) == 0 {
935935
// We're in module mode, but not inside a module.
936936
//
@@ -950,7 +950,7 @@ func loadModFile(loaderstate *State, ctx context.Context, opts *PackageOpts) (*R
950950
//
951951
// See golang.org/issue/32027.
952952
} else {
953-
modfetch.GoSumFile = strings.TrimSuffix(modFilePath(loaderstate.modRoots[0]), ".mod") + ".sum"
953+
modfetch.ModuleFetchState.GoSumFile = strings.TrimSuffix(modFilePath(loaderstate.modRoots[0]), ".mod") + ".sum"
954954
}
955955
if len(loaderstate.modRoots) == 0 {
956956
// TODO(#49228): Instead of creating a fake module with an empty modroot,

0 commit comments

Comments
 (0)