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

View file

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