cmd/go/internal/modfetch: rename State to Fetcher

This change renames the type State to Fetcher to better reflect its
purpose.  The global variable ModuleFetchState is also renamed to
Fetcher_, which will continue to be gradually eliminated as with all
global state in the modfetch package.

[git-generate]
cd src/cmd/go/internal/modfetch
rf '
  mv State Fetcher
  mv ModuleFetchState Fetcher_
  mv NewState NewFetcher

  mv Fetcher.GoSumFile GoSumFile
  mv GoSumFile.s GoSumFile.f
  mv GoSumFile Fetcher.GoSumFile

  mv Fetcher.SetGoSumFile SetGoSumFile
  mv SetGoSumFile.s SetGoSumFile.f
  mv SetGoSumFile Fetcher.SetGoSumFile

  mv Fetcher.AddWorkspaceGoSumFile AddWorkspaceGoSumFile
  mv AddWorkspaceGoSumFile.s AddWorkspaceGoSumFile.f
  mv AddWorkspaceGoSumFile Fetcher.AddWorkspaceGoSumFile
'
rf '
  add NewFetcher:+0 f := new(Fetcher) \
  f.lookupCache = new(par.Cache[lookupCacheKey, Repo]) \
  f.downloadCache = new(par.ErrCache[module.Version, string]) \
  return f
'
rf 'rm NewFetcher:+5,8'
cd ../modload
rf '
  mv State.modfetchState State.fetcher
'

Change-Id: I7cb6c945ea0f1d2119e1615064f041e88c81c689
Reviewed-on: https://go-review.googlesource.com/c/go/+/721740
Reviewed-by: Michael Matloob <matloob@golang.org>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Mark Freeman <markfreeman@google.com>
This commit is contained in:
Ian Alexander 2025-11-17 16:38:22 -05:00
parent d3e11b3f90
commit 1dc1505d4a
3 changed files with 45 additions and 45 deletions

View file

@ -49,7 +49,7 @@ func Download(ctx context.Context, mod module.Version) (dir string, err error) {
}
// The par.Cache here avoids duplicate work.
return ModuleFetchState.downloadCache.Do(mod, func() (string, error) {
return Fetcher_.downloadCache.Do(mod, func() (string, error) {
dir, err := download(ctx, mod)
if err != nil {
return "", err
@ -78,7 +78,7 @@ func Unzip(ctx context.Context, mod module.Version, zipfile string) (dir string,
base.Fatal(err)
}
return ModuleFetchState.downloadCache.Do(mod, func() (string, error) {
return Fetcher_.downloadCache.Do(mod, func() (string, error) {
ctx, span := trace.StartSpan(ctx, "modfetch.Unzip "+mod.String())
defer span.Done()
@ -459,8 +459,8 @@ type modSumStatus struct {
used, dirty bool
}
// State holds a snapshot of the global state of the modfetch package.
type State struct {
// Fetcher holds a snapshot of the global state of the modfetch package.
type Fetcher struct {
// path to go.sum; set by package modload
goSumFile string
// path to module go.sums in workspace; set by package modload
@ -479,38 +479,38 @@ type State struct {
sumState sumState
}
var ModuleFetchState *State = NewState()
var Fetcher_ *Fetcher = NewFetcher()
func NewState() *State {
s := new(State)
s.lookupCache = new(par.Cache[lookupCacheKey, Repo])
s.downloadCache = new(par.ErrCache[module.Version, string])
return s
func NewFetcher() *Fetcher {
f := new(Fetcher)
f.lookupCache = new(par.Cache[lookupCacheKey, Repo])
f.downloadCache = new(par.ErrCache[module.Version, string])
return f
}
func (s *State) GoSumFile() string {
return s.goSumFile
func (f *Fetcher) GoSumFile() string {
return f.goSumFile
}
func (s *State) SetGoSumFile(str string) {
s.goSumFile = str
func (f *Fetcher) SetGoSumFile(str string) {
f.goSumFile = str
}
func (s *State) AddWorkspaceGoSumFile(file string) {
s.workspaceGoSumFiles = append(s.workspaceGoSumFiles, file)
func (f *Fetcher) AddWorkspaceGoSumFile(file string) {
f.workspaceGoSumFiles = append(f.workspaceGoSumFiles, file)
}
// Reset resets globals in the modfetch package, so previous loads don't affect
// contents of go.sum files.
func Reset() {
SetState(NewState())
SetState(NewFetcher())
}
// SetState sets the global state of the modfetch package to the newState, and returns the previous
// global state. newState should have been returned by SetState, or be an empty State.
// There should be no concurrent calls to any of the exported functions of this package with
// a call to SetState because it will modify the global state in a non-thread-safe way.
func SetState(newState *State) (oldState *State) {
func SetState(newState *Fetcher) (oldState *Fetcher) {
if newState.lookupCache == nil {
newState.lookupCache = new(par.Cache[lookupCacheKey, Repo])
}
@ -521,21 +521,21 @@ func SetState(newState *State) (oldState *State) {
goSum.mu.Lock()
defer goSum.mu.Unlock()
oldState = &State{
goSumFile: ModuleFetchState.goSumFile,
workspaceGoSumFiles: ModuleFetchState.workspaceGoSumFiles,
lookupCache: ModuleFetchState.lookupCache,
downloadCache: ModuleFetchState.downloadCache,
oldState = &Fetcher{
goSumFile: Fetcher_.goSumFile,
workspaceGoSumFiles: Fetcher_.workspaceGoSumFiles,
lookupCache: Fetcher_.lookupCache,
downloadCache: Fetcher_.downloadCache,
sumState: goSum.sumState,
}
ModuleFetchState.SetGoSumFile(newState.goSumFile)
ModuleFetchState.workspaceGoSumFiles = newState.workspaceGoSumFiles
Fetcher_.SetGoSumFile(newState.goSumFile)
Fetcher_.workspaceGoSumFiles = newState.workspaceGoSumFiles
// Uses of lookupCache and downloadCache both can call checkModSum,
// which in turn sets the used bit on goSum.status for modules.
// Set (or reset) them so used can be computed properly.
ModuleFetchState.lookupCache = newState.lookupCache
ModuleFetchState.downloadCache = newState.downloadCache
Fetcher_.lookupCache = newState.lookupCache
Fetcher_.downloadCache = newState.downloadCache
// Set, or reset all fields on goSum. If being reset to empty, it will be initialized later.
goSum.sumState = newState.sumState
@ -547,7 +547,7 @@ func SetState(newState *State) (oldState *State) {
// use of go.sum is now enabled.
// The goSum lock must be held.
func initGoSum() (bool, error) {
if ModuleFetchState.goSumFile == "" {
if Fetcher_.goSumFile == "" {
return false, nil
}
if goSum.m != nil {
@ -558,7 +558,7 @@ func initGoSum() (bool, error) {
goSum.status = make(map[modSum]modSumStatus)
goSum.w = make(map[string]map[module.Version][]string)
for _, f := range ModuleFetchState.workspaceGoSumFiles {
for _, f := range Fetcher_.workspaceGoSumFiles {
goSum.w[f] = make(map[module.Version][]string)
_, err := readGoSumFile(goSum.w[f], f)
if err != nil {
@ -566,7 +566,7 @@ func initGoSum() (bool, error) {
}
}
enabled, err := readGoSumFile(goSum.m, ModuleFetchState.goSumFile)
enabled, err := readGoSumFile(goSum.m, Fetcher_.goSumFile)
goSum.enabled = enabled
return enabled, err
}
@ -812,7 +812,7 @@ func checkModSum(mod module.Version, h string) error {
// goSum.mu must be locked.
func haveModSumLocked(mod module.Version, h string) bool {
sumFileName := "go.sum"
if strings.HasSuffix(ModuleFetchState.goSumFile, "go.work.sum") {
if strings.HasSuffix(Fetcher_.goSumFile, "go.work.sum") {
sumFileName = "go.work.sum"
}
for _, vh := range goSum.m[mod] {
@ -956,7 +956,7 @@ Outer:
if readonly {
return ErrGoSumDirty
}
if fsys.Replaced(ModuleFetchState.goSumFile) {
if fsys.Replaced(Fetcher_.goSumFile) {
base.Fatalf("go: updates to go.sum needed, but go.sum is part of the overlay specified with -overlay")
}
@ -966,7 +966,7 @@ Outer:
defer unlock()
}
err := lockedfile.Transform(ModuleFetchState.goSumFile, func(data []byte) ([]byte, error) {
err := lockedfile.Transform(Fetcher_.goSumFile, func(data []byte) ([]byte, error) {
tidyGoSum := tidyGoSum(data, keep)
return tidyGoSum, nil
})
@ -985,7 +985,7 @@ Outer:
func TidyGoSum(keep map[module.Version]bool) (before, after []byte) {
goSum.mu.Lock()
defer goSum.mu.Unlock()
before, err := lockedfile.Read(ModuleFetchState.goSumFile)
before, err := lockedfile.Read(Fetcher_.goSumFile)
if err != nil && !errors.Is(err, fs.ErrNotExist) {
base.Fatalf("reading go.sum: %v", err)
}
@ -1002,7 +1002,7 @@ func tidyGoSum(data []byte, keep map[module.Version]bool) []byte {
// truncated the file to remove erroneous hashes, and we shouldn't restore
// them without good reason.
goSum.m = make(map[module.Version][]string, len(goSum.m))
readGoSum(goSum.m, ModuleFetchState.goSumFile, data)
readGoSum(goSum.m, Fetcher_.goSumFile, data)
for ms, st := range goSum.status {
if st.used && !sumInWorkspaceModulesLocked(ms.mod) {
addModSumLocked(ms.mod, ms.sum)

View file

@ -205,7 +205,7 @@ func Lookup(ctx context.Context, proxy, path string) Repo {
defer logCall("Lookup(%q, %q)", proxy, path)()
}
return ModuleFetchState.lookupCache.Do(lookupCacheKey{proxy, path}, func() Repo {
return Fetcher_.lookupCache.Do(lookupCacheKey{proxy, path}, func() Repo {
return newCachingRepo(ctx, path, func(ctx context.Context) (Repo, error) {
r, err := lookup(ctx, proxy, path)
if err == nil && traceRepo {

View file

@ -398,7 +398,7 @@ func (s *State) setState(new *State) (old *State) {
MainModules: s.MainModules,
requirements: s.requirements,
workFilePath: s.workFilePath,
modfetchState: s.modfetchState,
fetcher: s.fetcher,
}
s.initialized = new.initialized
s.ForceUseModules = new.ForceUseModules
@ -411,8 +411,8 @@ func (s *State) setState(new *State) (old *State) {
// The modfetch package's global state is used to compute
// the go.sum file, so save and restore it along with the
// modload state.
s.modfetchState = new.modfetchState
old.modfetchState = modfetch.SetState(s.modfetchState) // TODO(jitsu): remove after completing global state elimination
s.fetcher = new.fetcher
old.fetcher = modfetch.SetState(s.fetcher) // TODO(jitsu): remove after completing global state elimination
return old
}
@ -451,13 +451,13 @@ type State struct {
// Set to the path to the go.work file, or "" if workspace mode is
// disabled
workFilePath string
modfetchState *modfetch.State
workFilePath string
fetcher *modfetch.Fetcher
}
func NewState() *State {
s := new(State)
s.modfetchState = modfetch.NewState()
s.fetcher = modfetch.NewFetcher()
return s
}
@ -937,9 +937,9 @@ func loadModFile(loaderstate *State, ctx context.Context, opts *PackageOpts) (*R
}
for _, modRoot := range loaderstate.modRoots {
sumFile := strings.TrimSuffix(modFilePath(modRoot), ".mod") + ".sum"
modfetch.ModuleFetchState.AddWorkspaceGoSumFile(sumFile)
modfetch.Fetcher_.AddWorkspaceGoSumFile(sumFile)
}
modfetch.ModuleFetchState.SetGoSumFile(loaderstate.workFilePath + ".sum")
modfetch.Fetcher_.SetGoSumFile(loaderstate.workFilePath + ".sum")
} else if len(loaderstate.modRoots) == 0 {
// We're in module mode, but not inside a module.
//
@ -959,7 +959,7 @@ func loadModFile(loaderstate *State, ctx context.Context, opts *PackageOpts) (*R
//
// See golang.org/issue/32027.
} else {
modfetch.ModuleFetchState.SetGoSumFile(strings.TrimSuffix(modFilePath(loaderstate.modRoots[0]), ".mod") + ".sum")
modfetch.Fetcher_.SetGoSumFile(strings.TrimSuffix(modFilePath(loaderstate.modRoots[0]), ".mod") + ".sum")
}
if len(loaderstate.modRoots) == 0 {
// TODO(#49228): Instead of creating a fake module with an empty modroot,