// State holds a snapshot of the global state of the modfetch package.
type State struct {
// path to go.sum; set by package modload
- GoSumFile string
+ goSumFile string
// path to module go.sums in workspace; set by package modload
- WorkspaceGoSumFiles []string
+ workspaceGoSumFiles []string
// 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.
return s
}
+func (s *State) GoSumFile() string {
+ return s.goSumFile
+}
+
+func (s *State) SetGoSumFile(str string) {
+ s.goSumFile = str
+}
+
+func (s *State) AddWorkspaceGoSumFile(file string) {
+ s.workspaceGoSumFiles = append(s.workspaceGoSumFiles, file)
+}
+
// Reset resets globals in the modfetch package, so previous loads don't affect
// contents of go.sum files.
func Reset() {
defer goSum.mu.Unlock()
oldState = State{
- GoSumFile: ModuleFetchState.GoSumFile,
- WorkspaceGoSumFiles: ModuleFetchState.WorkspaceGoSumFiles,
+ goSumFile: ModuleFetchState.goSumFile,
+ workspaceGoSumFiles: ModuleFetchState.workspaceGoSumFiles,
lookupCache: ModuleFetchState.lookupCache,
downloadCache: ModuleFetchState.downloadCache,
sumState: goSum.sumState,
}
- ModuleFetchState.GoSumFile = newState.GoSumFile
- ModuleFetchState.WorkspaceGoSumFiles = newState.WorkspaceGoSumFiles
+ ModuleFetchState.SetGoSumFile(newState.goSumFile)
+ ModuleFetchState.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.
// use of go.sum is now enabled.
// The goSum lock must be held.
func initGoSum() (bool, error) {
- if ModuleFetchState.GoSumFile == "" {
+ if ModuleFetchState.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 ModuleFetchState.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, ModuleFetchState.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(ModuleFetchState.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(ModuleFetchState.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(ModuleFetchState.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(ModuleFetchState.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, ModuleFetchState.goSumFile, data)
for ms, st := range goSum.status {
if st.used && !sumInWorkspaceModulesLocked(ms.mod) {
addModSumLocked(ms.mod, ms.sum)
}
for _, modRoot := range loaderstate.modRoots {
sumFile := strings.TrimSuffix(modFilePath(modRoot), ".mod") + ".sum"
- modfetch.ModuleFetchState.WorkspaceGoSumFiles = append(modfetch.ModuleFetchState.WorkspaceGoSumFiles, sumFile)
+ modfetch.ModuleFetchState.AddWorkspaceGoSumFile(sumFile)
}
- modfetch.ModuleFetchState.GoSumFile = loaderstate.workFilePath + ".sum"
+ modfetch.ModuleFetchState.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.GoSumFile = strings.TrimSuffix(modFilePath(loaderstate.modRoots[0]), ".mod") + ".sum"
+ modfetch.ModuleFetchState.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,