Skip to content

Commit 4976606

Browse files
committed
cmd/go: remove final references to modfetch.Fetcher_
This commit removes the final references to the global Fetcher_ variable from the modfetch and modload packages. This completes the removal of global state from the modfetch package. Change-Id: Ibb5309acdc7d05f1a7591ddcf890b44b6cc4cb2f Reviewed-on: https://go-review.googlesource.com/c/go/+/724249 Reviewed-by: Michael Matloob <matloob@golang.org> Reviewed-by: Michael Matloob <matloob@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
1 parent 08bf23c commit 4976606

File tree

9 files changed

+67
-69
lines changed

9 files changed

+67
-69
lines changed

src/cmd/go/internal/load/pkg.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2050,7 +2050,7 @@ func (p *Package) load(loaderstate *modload.State, ctx context.Context, opts Pac
20502050
// Consider starting this as a background goroutine and retrieving the result
20512051
// asynchronously when we're actually ready to build the package, or when we
20522052
// actually need to evaluate whether the package's metadata is stale.
2053-
p.setBuildInfo(ctx, opts.AutoVCS)
2053+
p.setBuildInfo(ctx, loaderstate.Fetcher(), opts.AutoVCS)
20542054
}
20552055

20562056
// If cgo is not enabled, ignore cgo supporting sources
@@ -2323,7 +2323,7 @@ func appendBuildSetting(info *debug.BuildInfo, key, value string) {
23232323
//
23242324
// Note that the GoVersion field is not set here to avoid encoding it twice.
23252325
// It is stored separately in the binary, mostly for historical reasons.
2326-
func (p *Package) setBuildInfo(ctx context.Context, autoVCS bool) {
2326+
func (p *Package) setBuildInfo(ctx context.Context, f *modfetch.Fetcher, autoVCS bool) {
23272327
setPkgErrorf := func(format string, args ...any) {
23282328
if p.Error == nil {
23292329
p.Error = &PackageError{Err: fmt.Errorf(format, args...)}
@@ -2595,7 +2595,7 @@ func (p *Package) setBuildInfo(ctx context.Context, autoVCS bool) {
25952595
if !ok {
25962596
goto omitVCS
25972597
}
2598-
repo := modfetch.LookupLocal(ctx, codeRoot, p.Module.Path, repoDir)
2598+
repo := f.LookupLocal(ctx, codeRoot, p.Module.Path, repoDir)
25992599
revInfo, err := repo.Stat(ctx, st.Revision)
26002600
if err != nil {
26012601
goto omitVCS

src/cmd/go/internal/load/test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,7 @@ func TestPackagesAndErrors(loaderstate *modload.State, ctx context.Context, done
301301
// pmain won't have buildinfo set (since we copy it from the package under test). If the default GODEBUG
302302
// used for the package under test is different from that of the test main, the BuildInfo assigned above from the package
303303
// under test incorrect for the test main package. Either set or correct pmain's build info.
304-
pmain.setBuildInfo(ctx, opts.AutoVCS)
304+
pmain.setBuildInfo(ctx, loaderstate.Fetcher(), opts.AutoVCS)
305305
}
306306

307307
// The generated main also imports testing, regexp, and os.

src/cmd/go/internal/modfetch/cache.go

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ func lockVersion(ctx context.Context, mod module.Version) (unlock func(), err er
156156
if err != nil {
157157
return nil, err
158158
}
159-
if err := os.MkdirAll(filepath.Dir(path), 0777); err != nil {
159+
if err := os.MkdirAll(filepath.Dir(path), 0o777); err != nil {
160160
return nil, err
161161
}
162162
return lockedfile.MutexAt(path).Lock()
@@ -172,7 +172,7 @@ func SideLock(ctx context.Context) (unlock func(), err error) {
172172
}
173173

174174
path := filepath.Join(cfg.GOMODCACHE, "cache", "lock")
175-
if err := os.MkdirAll(filepath.Dir(path), 0777); err != nil {
175+
if err := os.MkdirAll(filepath.Dir(path), 0o777); err != nil {
176176
return nil, fmt.Errorf("failed to create cache directory: %w", err)
177177
}
178178

@@ -194,12 +194,14 @@ type cachingRepo struct {
194194
once sync.Once
195195
initRepo func(context.Context) (Repo, error)
196196
r Repo
197+
fetcher *Fetcher
197198
}
198199

199-
func newCachingRepo(ctx context.Context, path string, initRepo func(context.Context) (Repo, error)) *cachingRepo {
200+
func newCachingRepo(ctx context.Context, fetcher *Fetcher, path string, initRepo func(context.Context) (Repo, error)) *cachingRepo {
200201
return &cachingRepo{
201202
path: path,
202203
initRepo: initRepo,
204+
fetcher: fetcher,
203205
}
204206
}
205207

@@ -226,7 +228,6 @@ func (r *cachingRepo) Versions(ctx context.Context, prefix string) (*Versions, e
226228
v, err := r.versionsCache.Do(prefix, func() (*Versions, error) {
227229
return r.repo(ctx).Versions(ctx, prefix)
228230
})
229-
230231
if err != nil {
231232
return nil, err
232233
}
@@ -309,15 +310,15 @@ func (r *cachingRepo) GoMod(ctx context.Context, version string) ([]byte, error)
309310
return r.repo(ctx).GoMod(ctx, version)
310311
}
311312
text, err := r.gomodCache.Do(version, func() ([]byte, error) {
312-
file, text, err := Fetcher_.readDiskGoMod(ctx, r.path, version)
313+
file, text, err := r.fetcher.readDiskGoMod(ctx, r.path, version)
313314
if err == nil {
314315
// Note: readDiskGoMod already called checkGoMod.
315316
return text, nil
316317
}
317318

318319
text, err = r.repo(ctx).GoMod(ctx, version)
319320
if err == nil {
320-
if err := checkGoMod(Fetcher_, r.path, version, text); err != nil {
321+
if err := checkGoMod(r.fetcher, r.path, version, text); err != nil {
321322
return text, err
322323
}
323324
if err := writeDiskGoMod(ctx, file, text); err != nil {
@@ -653,13 +654,13 @@ func writeDiskCache(ctx context.Context, file string, data []byte) error {
653654
return nil
654655
}
655656
// Make sure directory for file exists.
656-
if err := os.MkdirAll(filepath.Dir(file), 0777); err != nil {
657+
if err := os.MkdirAll(filepath.Dir(file), 0o777); err != nil {
657658
return err
658659
}
659660

660661
// Write the file to a temporary location, and then rename it to its final
661662
// path to reduce the likelihood of a corrupt file existing at that final path.
662-
f, err := tempFile(ctx, filepath.Dir(file), filepath.Base(file), 0666)
663+
f, err := tempFile(ctx, filepath.Dir(file), filepath.Base(file), 0o666)
663664
if err != nil {
664665
return err
665666
}
@@ -812,7 +813,7 @@ func checkCacheDir(ctx context.Context) error {
812813
statCacheErr = fmt.Errorf("could not create module cache: %w", err)
813814
return
814815
}
815-
if err := os.MkdirAll(cfg.GOMODCACHE, 0777); err != nil {
816+
if err := os.MkdirAll(cfg.GOMODCACHE, 0o777); err != nil {
816817
statCacheErr = fmt.Errorf("could not create module cache: %w", err)
817818
return
818819
}

src/cmd/go/internal/modfetch/coderepo_test.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ func TestMain(m *testing.M) {
3636
}
3737

3838
func testMain(m *testing.M) (err error) {
39-
4039
cfg.GOPROXY = "direct"
4140

4241
// The sum database is populated using a released version of the go command,
@@ -56,7 +55,7 @@ func testMain(m *testing.M) (err error) {
5655
}()
5756

5857
cfg.GOMODCACHE = filepath.Join(dir, "modcache")
59-
if err := os.Mkdir(cfg.GOMODCACHE, 0755); err != nil {
58+
if err := os.Mkdir(cfg.GOMODCACHE, 0o755); err != nil {
6059
return err
6160
}
6261

@@ -589,6 +588,7 @@ var codeRepoTests = []codeRepoTest{
589588
func TestCodeRepo(t *testing.T) {
590589
testenv.MustHaveExternalNetwork(t)
591590
tmpdir := t.TempDir()
591+
fetcher := NewFetcher()
592592

593593
for _, tt := range codeRepoTests {
594594
f := func(tt codeRepoTest) func(t *testing.T) {
@@ -603,7 +603,7 @@ func TestCodeRepo(t *testing.T) {
603603
}
604604
ctx := context.Background()
605605

606-
repo := Fetcher_.Lookup(ctx, "direct", tt.path)
606+
repo := fetcher.Lookup(ctx, "direct", tt.path)
607607

608608
if tt.mpath == "" {
609609
tt.mpath = tt.path
@@ -817,7 +817,7 @@ var codeRepoVersionsTests = []struct {
817817

818818
func TestCodeRepoVersions(t *testing.T) {
819819
testenv.MustHaveExternalNetwork(t)
820-
820+
fetcher := NewFetcher()
821821
for _, tt := range codeRepoVersionsTests {
822822
tt := tt
823823
t.Run(strings.ReplaceAll(tt.path, "/", "_"), func(t *testing.T) {
@@ -831,7 +831,7 @@ func TestCodeRepoVersions(t *testing.T) {
831831
}
832832
ctx := context.Background()
833833

834-
repo := Fetcher_.Lookup(ctx, "direct", tt.path)
834+
repo := fetcher.Lookup(ctx, "direct", tt.path)
835835
list, err := repo.Versions(ctx, tt.prefix)
836836
if err != nil {
837837
t.Fatalf("Versions(%q): %v", tt.prefix, err)
@@ -898,7 +898,7 @@ var latestTests = []struct {
898898

899899
func TestLatest(t *testing.T) {
900900
testenv.MustHaveExternalNetwork(t)
901-
901+
fetcher := NewFetcher()
902902
for _, tt := range latestTests {
903903
name := strings.ReplaceAll(tt.path, "/", "_")
904904
t.Run(name, func(t *testing.T) {
@@ -909,7 +909,7 @@ func TestLatest(t *testing.T) {
909909
}
910910
ctx := context.Background()
911911

912-
repo := Fetcher_.Lookup(ctx, "direct", tt.path)
912+
repo := fetcher.Lookup(ctx, "direct", tt.path)
913913
info, err := repo.Latest(ctx)
914914
if err != nil {
915915
if tt.err != "" {

src/cmd/go/internal/modfetch/fetch.go

Lines changed: 35 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -475,8 +475,6 @@ type Fetcher struct {
475475
sumState sumState
476476
}
477477

478-
var Fetcher_ *Fetcher = NewFetcher()
479-
480478
func NewFetcher() *Fetcher {
481479
f := new(Fetcher)
482480
f.lookupCache = new(par.Cache[lookupCacheKey, Repo])
@@ -498,42 +496,42 @@ func (f *Fetcher) AddWorkspaceGoSumFile(file string) {
498496

499497
// Reset resets globals in the modfetch package, so previous loads don't affect
500498
// contents of go.sum files.
501-
func Reset() {
502-
SetState(NewFetcher())
499+
func (f *Fetcher) Reset() {
500+
f.SetState(NewFetcher())
503501
}
504502

505503
// SetState sets the global state of the modfetch package to the newState, and returns the previous
506504
// global state. newState should have been returned by SetState, or be an empty State.
507505
// There should be no concurrent calls to any of the exported functions of this package with
508506
// a call to SetState because it will modify the global state in a non-thread-safe way.
509-
func SetState(newState *Fetcher) (oldState *Fetcher) {
507+
func (f *Fetcher) SetState(newState *Fetcher) (oldState *Fetcher) {
510508
if newState.lookupCache == nil {
511509
newState.lookupCache = new(par.Cache[lookupCacheKey, Repo])
512510
}
513511
if newState.downloadCache == nil {
514512
newState.downloadCache = new(par.ErrCache[module.Version, string])
515513
}
516514

517-
Fetcher_.mu.Lock()
518-
defer Fetcher_.mu.Unlock()
515+
f.mu.Lock()
516+
defer f.mu.Unlock()
519517

520518
oldState = &Fetcher{
521-
goSumFile: Fetcher_.goSumFile,
522-
workspaceGoSumFiles: Fetcher_.workspaceGoSumFiles,
523-
lookupCache: Fetcher_.lookupCache,
524-
downloadCache: Fetcher_.downloadCache,
525-
sumState: Fetcher_.sumState,
519+
goSumFile: f.goSumFile,
520+
workspaceGoSumFiles: f.workspaceGoSumFiles,
521+
lookupCache: f.lookupCache,
522+
downloadCache: f.downloadCache,
523+
sumState: f.sumState,
526524
}
527525

528-
Fetcher_.SetGoSumFile(newState.goSumFile)
529-
Fetcher_.workspaceGoSumFiles = newState.workspaceGoSumFiles
526+
f.SetGoSumFile(newState.goSumFile)
527+
f.workspaceGoSumFiles = newState.workspaceGoSumFiles
530528
// Uses of lookupCache and downloadCache both can call checkModSum,
531529
// which in turn sets the used bit on goSum.status for modules.
532530
// Set (or reset) them so used can be computed properly.
533-
Fetcher_.lookupCache = newState.lookupCache
534-
Fetcher_.downloadCache = newState.downloadCache
531+
f.lookupCache = newState.lookupCache
532+
f.downloadCache = newState.downloadCache
535533
// Set, or reset all fields on goSum. If being reset to empty, it will be initialized later.
536-
Fetcher_.sumState = newState.sumState
534+
f.sumState = newState.sumState
537535

538536
return oldState
539537
}
@@ -666,32 +664,32 @@ func HaveSum(f *Fetcher, mod module.Version) bool {
666664
// The entry's hash must be generated with a known hash algorithm.
667665
// mod.Version may have a "/go.mod" suffix to distinguish sums for
668666
// .mod and .zip files.
669-
func RecordedSum(mod module.Version) (sum string, ok bool) {
670-
Fetcher_.mu.Lock()
671-
defer Fetcher_.mu.Unlock()
672-
inited, err := Fetcher_.initGoSum()
667+
func (f *Fetcher) RecordedSum(mod module.Version) (sum string, ok bool) {
668+
f.mu.Lock()
669+
defer f.mu.Unlock()
670+
inited, err := f.initGoSum()
673671
foundSum := ""
674672
if err != nil || !inited {
675673
return "", false
676674
}
677-
for _, goSums := range Fetcher_.sumState.w {
675+
for _, goSums := range f.sumState.w {
678676
for _, h := range goSums[mod] {
679677
if !strings.HasPrefix(h, "h1:") {
680678
continue
681679
}
682-
if !Fetcher_.sumState.status[modSum{mod, h}].dirty {
680+
if !f.sumState.status[modSum{mod, h}].dirty {
683681
if foundSum != "" && foundSum != h { // conflicting sums exist
684682
return "", false
685683
}
686684
foundSum = h
687685
}
688686
}
689687
}
690-
for _, h := range Fetcher_.sumState.m[mod] {
688+
for _, h := range f.sumState.m[mod] {
691689
if !strings.HasPrefix(h, "h1:") {
692690
continue
693691
}
694-
if !Fetcher_.sumState.status[modSum{mod, h}].dirty {
692+
if !f.sumState.status[modSum{mod, h}].dirty {
695693
if foundSum != "" && foundSum != h { // conflicting sums exist
696694
return "", false
697695
}
@@ -977,14 +975,14 @@ Outer:
977975

978976
// TidyGoSum returns a tidy version of the go.sum file.
979977
// A missing go.sum file is treated as if empty.
980-
func TidyGoSum(keep map[module.Version]bool) (before, after []byte) {
981-
Fetcher_.mu.Lock()
982-
defer Fetcher_.mu.Unlock()
983-
before, err := lockedfile.Read(Fetcher_.goSumFile)
978+
func (f *Fetcher) TidyGoSum(keep map[module.Version]bool) (before, after []byte) {
979+
f.mu.Lock()
980+
defer f.mu.Unlock()
981+
before, err := lockedfile.Read(f.goSumFile)
984982
if err != nil && !errors.Is(err, fs.ErrNotExist) {
985983
base.Fatalf("reading go.sum: %v", err)
986984
}
987-
after = tidyGoSum(Fetcher_, before, keep)
985+
after = tidyGoSum(f, before, keep)
988986
return before, after
989987
}
990988

@@ -1041,23 +1039,23 @@ func sumInWorkspaceModulesLocked(f *Fetcher, m module.Version) bool {
10411039
// keep is used to check whether a sum should be retained in go.mod. It should
10421040
// have entries for both module content sums and go.mod sums (version ends
10431041
// with "/go.mod").
1044-
func TrimGoSum(keep map[module.Version]bool) {
1045-
Fetcher_.mu.Lock()
1046-
defer Fetcher_.mu.Unlock()
1047-
inited, err := Fetcher_.initGoSum()
1042+
func (f *Fetcher) TrimGoSum(keep map[module.Version]bool) {
1043+
f.mu.Lock()
1044+
defer f.mu.Unlock()
1045+
inited, err := f.initGoSum()
10481046
if err != nil {
10491047
base.Fatalf("%s", err)
10501048
}
10511049
if !inited {
10521050
return
10531051
}
10541052

1055-
for m, hs := range Fetcher_.sumState.m {
1053+
for m, hs := range f.sumState.m {
10561054
if !keep[m] {
10571055
for _, h := range hs {
1058-
Fetcher_.sumState.status[modSum{m, h}] = modSumStatus{used: false, dirty: true}
1056+
f.sumState.status[modSum{m, h}] = modSumStatus{used: false, dirty: true}
10591057
}
1060-
Fetcher_.sumState.overwrite = true
1058+
f.sumState.overwrite = true
10611059
}
10621060
}
10631061
}

src/cmd/go/internal/modfetch/repo.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ func (f *Fetcher) Lookup(ctx context.Context, proxy, path string) Repo {
206206
}
207207

208208
return f.lookupCache.Do(lookupCacheKey{proxy, path}, func() Repo {
209-
return newCachingRepo(ctx, path, func(ctx context.Context) (Repo, error) {
209+
return newCachingRepo(ctx, f, path, func(ctx context.Context) (Repo, error) {
210210
r, err := lookup(f, ctx, proxy, path)
211211
if err == nil && traceRepo {
212212
r = newLoggingRepo(r)
@@ -223,13 +223,13 @@ var lookupLocalCache = new(par.Cache[string, Repo]) // path, Repo
223223
// codeRoot is the module path of the root module in the repository.
224224
// path is the module path of the module being looked up.
225225
// dir is the file system path of the repository containing the module.
226-
func LookupLocal(ctx context.Context, codeRoot string, path string, dir string) Repo {
226+
func (f *Fetcher) LookupLocal(ctx context.Context, codeRoot string, path string, dir string) Repo {
227227
if traceRepo {
228228
defer logCall("LookupLocal(%q)", path)()
229229
}
230230

231231
return lookupLocalCache.Do(path, func() Repo {
232-
return newCachingRepo(ctx, path, func(ctx context.Context) (Repo, error) {
232+
return newCachingRepo(ctx, f, path, func(ctx context.Context) (Repo, error) {
233233
repoDir, vcsCmd, err := vcs.FromDir(dir, "")
234234
if err != nil {
235235
return nil, err

src/cmd/go/internal/modload/build.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -368,7 +368,7 @@ func moduleInfo(loaderstate *State, ctx context.Context, rs *Requirements, m mod
368368
m.GoMod = gomod
369369
}
370370
}
371-
if gomodsum, ok := modfetch.RecordedSum(modkey(mod)); ok {
371+
if gomodsum, ok := loaderstate.fetcher.RecordedSum(modkey(mod)); ok {
372372
m.GoModSum = gomodsum
373373
}
374374
}
@@ -377,7 +377,7 @@ func moduleInfo(loaderstate *State, ctx context.Context, rs *Requirements, m mod
377377
if err == nil {
378378
m.Dir = dir
379379
}
380-
if sum, ok := modfetch.RecordedSum(mod); ok {
380+
if sum, ok := loaderstate.fetcher.RecordedSum(mod); ok {
381381
m.Sum = sum
382382
}
383383
}

0 commit comments

Comments
 (0)