cmd/go: add setters for critical State fields

This commit unexports critical State fields and provides setter
methods to update their values.

[git-generate]
cd src/cmd/go/internal/modfetch
rf '
  add fetch.go:490 var jitsu int = 0 // rf marker
  mv State.GoSumFile State.GoSumFile_
  mv State.WorkspaceGoSumFiles State.WorkspaceGoSumFiles_
  add jitsu \
    func (s *State) GoSumFile() string { \
      return ""
    } \
    \
    func (s *State) SetGoSumFile(str string) { \
    } \
    \
    func (s *State) AddWorkspaceGoSumFile(file string) { \
        s.WorkspaceGoSumFiles_ = append(s.WorkspaceGoSumFiles_, file) \
    }
    \

  ex {
    var s *State
    var x string
    s.GoSumFile_ = x -> s.SetGoSumFile(x)
  }

  rm jitsu
'

cd ../modload
sed -i '
  s/modfetch.ModuleFetchState.WorkspaceGoSumFiles_ = append(modfetch.ModuleFetchState.WorkspaceGoSumFiles_, sumFile)/modfetch.ModuleFetchState.AddWorkspaceGoSumFile(sumFile)/
' init.go

for dir in modcmd modload ; do
 cd ../${dir}
 rf '
   ex {
     import "cmd/go/internal/modfetch"
     var s *modfetch.State
     var x string
     s.GoSumFile_ = x -> s.SetGoSumFile(x)
   }
 '
done

cd ../modfetch
rf '
 mv State.GoSumFile_ State.goSumFile
 mv State.WorkspaceGoSumFiles_ State.workspaceGoSumFiles
 add State.GoSumFile: return s.goSumFile
 rm State.GoSumFile://+1
 add State.SetGoSumFile: s.goSumFile = str
'

Change-Id: Iff694aad7ad1cc62d2096c210dbaa3cce2b4061d
Reviewed-on: https://go-review.googlesource.com/c/go/+/720840
Reviewed-by: Michael Matloob <matloob@golang.org>
Reviewed-by: Mark Freeman <markfreeman@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
This commit is contained in:
Ian Alexander 2025-11-15 19:19:10 -05:00
parent 6851795fb6
commit 2f7fd5714f
2 changed files with 29 additions and 17 deletions

View file

@ -462,9 +462,9 @@ type modSumStatus struct {
// 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.
@ -488,6 +488,18 @@ func NewState() *State {
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() {
@ -510,15 +522,15 @@ func SetState(newState State) (oldState State) {
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.
@ -535,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 ModuleFetchState.goSumFile == "" {
return false, nil
}
if goSum.m != nil {
@ -546,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 ModuleFetchState.workspaceGoSumFiles {
goSum.w[f] = make(map[module.Version][]string)
_, err := readGoSumFile(goSum.w[f], f)
if err != nil {
@ -554,7 +566,7 @@ func initGoSum() (bool, error) {
}
}
enabled, err := readGoSumFile(goSum.m, ModuleFetchState.GoSumFile)
enabled, err := readGoSumFile(goSum.m, ModuleFetchState.goSumFile)
goSum.enabled = enabled
return enabled, err
}
@ -800,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(ModuleFetchState.goSumFile, "go.work.sum") {
sumFileName = "go.work.sum"
}
for _, vh := range goSum.m[mod] {
@ -944,7 +956,7 @@ Outer:
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")
}
@ -954,7 +966,7 @@ Outer:
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
})
@ -973,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(ModuleFetchState.goSumFile)
if err != nil && !errors.Is(err, fs.ErrNotExist) {
base.Fatalf("reading go.sum: %v", err)
}
@ -990,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, ModuleFetchState.goSumFile, data)
for ms, st := range goSum.status {
if st.used && !sumInWorkspaceModulesLocked(ms.mod) {
addModSumLocked(ms.mod, ms.sum)

View file

@ -929,9 +929,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.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.
//
@ -951,7 +951,7 @@ func loadModFile(loaderstate *State, ctx context.Context, opts *PackageOpts) (*R
//
// 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,