}
// 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
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()
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
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])
}
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
// 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 {
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 {
}
}
- enabled, err := readGoSumFile(goSum.m, ModuleFetchState.goSumFile)
+ enabled, err := readGoSumFile(goSum.m, Fetcher_.goSumFile)
goSum.enabled = enabled
return enabled, err
}
// 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] {
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")
}
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
})
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)
}
// 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)
MainModules: s.MainModules,
requirements: s.requirements,
workFilePath: s.workFilePath,
- modfetchState: s.modfetchState,
+ fetcher: s.fetcher,
}
s.initialized = new.initialized
s.ForceUseModules = new.ForceUseModules
// 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
}
// 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
}
}
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.
//
//
// 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,