cmd/go/internal/modload: replace references to modfetch.Fetcher_

This commit replaces references of modfetch.Fetcher_ with
modload.State.Fetcher() in the modload package.  Note that the
constructor `NewState` still intentionally references the global
variable.  This will be refactored in a later commit.

Change-Id: Ia8cfb41a81b0e29043694bc0f0f33f5a2f4920c6
Reviewed-on: https://go-review.googlesource.com/c/go/+/724243
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>
This commit is contained in:
Ian Alexander 2025-11-24 12:19:55 -05:00
parent 07b10e97d6
commit da31fd4177
10 changed files with 75 additions and 71 deletions

View file

@ -103,7 +103,7 @@ func ModuleInfo(loaderstate *State, ctx context.Context, path string) *modinfo.M
v, ok = rs.rootSelected(loaderstate, path) v, ok = rs.rootSelected(loaderstate, path)
} }
if !ok { if !ok {
mg, err := rs.Graph(modfetch.Fetcher_, loaderstate, ctx) mg, err := rs.Graph(loaderstate, ctx)
if err != nil { if err != nil {
base.Fatal(err) base.Fatal(err)
} }
@ -329,7 +329,7 @@ func moduleInfo(loaderstate *State, ctx context.Context, rs *Requirements, m mod
checksumOk := func(suffix string) bool { checksumOk := func(suffix string) bool {
return rs == nil || m.Version == "" || !mustHaveSums(loaderstate) || return rs == nil || m.Version == "" || !mustHaveSums(loaderstate) ||
modfetch.HaveSum(modfetch.Fetcher_, module.Version{Path: m.Path, Version: m.Version + suffix}) modfetch.HaveSum(loaderstate.Fetcher(), module.Version{Path: m.Path, Version: m.Version + suffix})
} }
mod := module.Version{Path: m.Path, Version: m.Version} mod := module.Version{Path: m.Path, Version: m.Version}
@ -355,7 +355,7 @@ func moduleInfo(loaderstate *State, ctx context.Context, rs *Requirements, m mod
if m.GoVersion == "" && checksumOk("/go.mod") { if m.GoVersion == "" && checksumOk("/go.mod") {
// Load the go.mod file to determine the Go version, since it hasn't // Load the go.mod file to determine the Go version, since it hasn't
// already been populated from rawGoVersion. // already been populated from rawGoVersion.
if summary, err := rawGoModSummary(modfetch.Fetcher_, loaderstate, mod); err == nil && summary.goVersion != "" { if summary, err := rawGoModSummary(loaderstate, mod); err == nil && summary.goVersion != "" {
m.GoVersion = summary.goVersion m.GoVersion = summary.goVersion
} }
} }

View file

@ -20,7 +20,6 @@ import (
"cmd/go/internal/base" "cmd/go/internal/base"
"cmd/go/internal/cfg" "cmd/go/internal/cfg"
"cmd/go/internal/gover" "cmd/go/internal/gover"
"cmd/go/internal/modfetch"
"cmd/go/internal/mvs" "cmd/go/internal/mvs"
"cmd/internal/par" "cmd/internal/par"
@ -270,9 +269,9 @@ func (rs *Requirements) hasRedundantRoot(loaderstate *State) bool {
// //
// If the requirements of any relevant module fail to load, Graph also // If the requirements of any relevant module fail to load, Graph also
// returns a non-nil error of type *mvs.BuildListError. // returns a non-nil error of type *mvs.BuildListError.
func (rs *Requirements) Graph(fetcher_ *modfetch.Fetcher, loaderstate *State, ctx context.Context) (*ModuleGraph, error) { func (rs *Requirements) Graph(loaderstate *State, ctx context.Context) (*ModuleGraph, error) {
rs.graphOnce.Do(func() { rs.graphOnce.Do(func() {
mg, mgErr := readModGraph(fetcher_, loaderstate, ctx, rs.pruning, rs.rootModules, nil) mg, mgErr := readModGraph(loaderstate, ctx, rs.pruning, rs.rootModules, nil)
rs.graph.Store(&cachedGraph{mg, mgErr}) rs.graph.Store(&cachedGraph{mg, mgErr})
}) })
cached := rs.graph.Load() cached := rs.graph.Load()
@ -308,7 +307,7 @@ var readModGraphDebugOnce sync.Once
// //
// Unlike LoadModGraph, readModGraph does not attempt to diagnose or update // Unlike LoadModGraph, readModGraph does not attempt to diagnose or update
// inconsistent roots. // inconsistent roots.
func readModGraph(f *modfetch.Fetcher, loaderstate *State, ctx context.Context, pruning modPruning, roots []module.Version, unprune map[module.Version]bool) (*ModuleGraph, error) { func readModGraph(loaderstate *State, ctx context.Context, pruning modPruning, roots []module.Version, unprune map[module.Version]bool) (*ModuleGraph, error) {
mustHaveGoRoot(roots) mustHaveGoRoot(roots)
if pruning == pruned { if pruning == pruned {
// Enable diagnostics for lazy module loading // Enable diagnostics for lazy module loading
@ -368,7 +367,7 @@ func readModGraph(f *modfetch.Fetcher, loaderstate *State, ctx context.Context,
// m's go.mod file indicates that it supports graph pruning. // m's go.mod file indicates that it supports graph pruning.
loadOne := func(m module.Version) (*modFileSummary, error) { loadOne := func(m module.Version) (*modFileSummary, error) {
return mg.loadCache.Do(m, func() (*modFileSummary, error) { return mg.loadCache.Do(m, func() (*modFileSummary, error) {
summary, err := goModSummary(f, loaderstate, m) summary, err := goModSummary(loaderstate, m)
mu.Lock() mu.Lock()
if err == nil { if err == nil {
@ -573,7 +572,7 @@ func LoadModGraph(loaderstate *State, ctx context.Context, goVersion string) (*M
rs = newRequirements(loaderstate, unpruned, rs.rootModules, rs.direct) rs = newRequirements(loaderstate, unpruned, rs.rootModules, rs.direct)
} }
return rs.Graph(modfetch.Fetcher_, loaderstate, ctx) return rs.Graph(loaderstate, ctx)
} }
rs, mg, err := expandGraph(loaderstate, ctx, rs) rs, mg, err := expandGraph(loaderstate, ctx, rs)
@ -596,7 +595,7 @@ func LoadModGraph(loaderstate *State, ctx context.Context, goVersion string) (*M
// expandGraph returns non-nil requirements and a non-nil graph regardless of // expandGraph returns non-nil requirements and a non-nil graph regardless of
// errors. On error, the roots might not be updated to be consistent. // errors. On error, the roots might not be updated to be consistent.
func expandGraph(loaderstate *State, ctx context.Context, rs *Requirements) (*Requirements, *ModuleGraph, error) { func expandGraph(loaderstate *State, ctx context.Context, rs *Requirements) (*Requirements, *ModuleGraph, error) {
mg, mgErr := rs.Graph(modfetch.Fetcher_, loaderstate, ctx) mg, mgErr := rs.Graph(loaderstate, ctx)
if mgErr != nil { if mgErr != nil {
// Without the graph, we can't update the roots: we don't know which // Without the graph, we can't update the roots: we don't know which
// versions of transitive dependencies would be selected. // versions of transitive dependencies would be selected.
@ -618,7 +617,7 @@ func expandGraph(loaderstate *State, ctx context.Context, rs *Requirements) (*Re
return rs, mg, rsErr return rs, mg, rsErr
} }
rs = newRS rs = newRS
mg, mgErr = rs.Graph(modfetch.Fetcher_, loaderstate, ctx) mg, mgErr = rs.Graph(loaderstate, ctx)
} }
return rs, mg, mgErr return rs, mg, mgErr
@ -860,7 +859,7 @@ func tidyPrunedRoots(loaderstate *State, ctx context.Context, mainModule module.
for len(queue) > 0 { for len(queue) > 0 {
roots = tidy.rootModules roots = tidy.rootModules
mg, err := tidy.Graph(modfetch.Fetcher_, loaderstate, ctx) mg, err := tidy.Graph(loaderstate, ctx)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -898,7 +897,7 @@ func tidyPrunedRoots(loaderstate *State, ctx context.Context, mainModule module.
} }
roots = tidy.rootModules roots = tidy.rootModules
_, err := tidy.Graph(modfetch.Fetcher_, loaderstate, ctx) _, err := tidy.Graph(loaderstate, ctx)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -940,7 +939,7 @@ func tidyPrunedRoots(loaderstate *State, ctx context.Context, mainModule module.
if len(roots) > len(tidy.rootModules) { if len(roots) > len(tidy.rootModules) {
module.Sort(roots) module.Sort(roots)
tidy = newRequirements(loaderstate, pruned, roots, tidy.direct) tidy = newRequirements(loaderstate, pruned, roots, tidy.direct)
_, err = tidy.Graph(modfetch.Fetcher_, loaderstate, ctx) _, err = tidy.Graph(loaderstate, ctx)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -1122,7 +1121,7 @@ func updatePrunedRoots(loaderstate *State, ctx context.Context, direct map[strin
rs = newRequirements(loaderstate, pruned, roots, direct) rs = newRequirements(loaderstate, pruned, roots, direct)
var err error var err error
mg, err = rs.Graph(modfetch.Fetcher_, loaderstate, ctx) mg, err = rs.Graph(loaderstate, ctx)
if err != nil { if err != nil {
return rs, err return rs, err
} }
@ -1136,7 +1135,7 @@ func updatePrunedRoots(loaderstate *State, ctx context.Context, direct map[strin
// We've already loaded the full module graph, which includes the // We've already loaded the full module graph, which includes the
// requirements of all of the root modules — even the transitive // requirements of all of the root modules — even the transitive
// requirements, if they are unpruned! // requirements, if they are unpruned!
mg, _ = rs.Graph(modfetch.Fetcher_, loaderstate, ctx) mg, _ = rs.Graph(loaderstate, ctx)
} else if cfg.BuildMod == "vendor" { } else if cfg.BuildMod == "vendor" {
// We can't spot-check the requirements of other modules because we // We can't spot-check the requirements of other modules because we
// don't in general have their go.mod files available in the vendor // don't in general have their go.mod files available in the vendor
@ -1149,7 +1148,7 @@ func updatePrunedRoots(loaderstate *State, ctx context.Context, direct map[strin
// inconsistent in some way; we need to load the full module graph // inconsistent in some way; we need to load the full module graph
// so that we can fix the roots properly. // so that we can fix the roots properly.
var err error var err error
mg, err = rs.Graph(modfetch.Fetcher_, loaderstate, ctx) mg, err = rs.Graph(loaderstate, ctx)
if err != nil { if err != nil {
return rs, err return rs, err
} }
@ -1236,7 +1235,7 @@ func spotCheckRoots(loaderstate *State, ctx context.Context, rs *Requirements, m
return return
} }
summary, err := goModSummary(modfetch.Fetcher_, loaderstate, m) summary, err := goModSummary(loaderstate, m)
if err != nil { if err != nil {
cancel() cancel()
return return
@ -1369,7 +1368,7 @@ func tidyUnprunedRoots(loaderstate *State, ctx context.Context, mainModule modul
// 4. Every version in add is selected at its given version unless upgraded by // 4. Every version in add is selected at its given version unless upgraded by
// (the dependencies of) an existing root or another module in add. // (the dependencies of) an existing root or another module in add.
func updateUnprunedRoots(loaderstate *State, ctx context.Context, direct map[string]bool, rs *Requirements, add []module.Version) (*Requirements, error) { func updateUnprunedRoots(loaderstate *State, ctx context.Context, direct map[string]bool, rs *Requirements, add []module.Version) (*Requirements, error) {
mg, err := rs.Graph(modfetch.Fetcher_, loaderstate, ctx) mg, err := rs.Graph(loaderstate, ctx)
if err != nil { if err != nil {
// We can't ignore errors in the module graph even if the user passed the -e // We can't ignore errors in the module graph even if the user passed the -e
// flag to try to push past them. If we can't load the complete module // flag to try to push past them. If we can't load the complete module
@ -1488,7 +1487,7 @@ func convertPruning(loaderstate *State, ctx context.Context, rs *Requirements, p
// root set! “Include the transitive dependencies of every module in the build // root set! “Include the transitive dependencies of every module in the build
// list” is exactly what happens in a pruned module if we promote every module // list” is exactly what happens in a pruned module if we promote every module
// in the build list to a root. // in the build list to a root.
mg, err := rs.Graph(modfetch.Fetcher_, loaderstate, ctx) mg, err := rs.Graph(loaderstate, ctx)
if err != nil { if err != nil {
return rs, err return rs, err
} }

View file

@ -5,11 +5,6 @@
package modload package modload
import ( import (
"cmd/go/internal/cfg"
"cmd/go/internal/gover"
"cmd/go/internal/modfetch"
"cmd/go/internal/mvs"
"cmd/internal/par"
"context" "context"
"errors" "errors"
"fmt" "fmt"
@ -17,6 +12,11 @@ import (
"os" "os"
"slices" "slices"
"cmd/go/internal/cfg"
"cmd/go/internal/gover"
"cmd/go/internal/mvs"
"cmd/internal/par"
"golang.org/x/mod/module" "golang.org/x/mod/module"
) )
@ -101,7 +101,7 @@ func editRequirements(loaderstate *State, ctx context.Context, rs *Requirements,
// dependencies, so we need to treat everything in the build list as // dependencies, so we need to treat everything in the build list as
// potentially relevant — that is, as what would be a “root” in a module // potentially relevant — that is, as what would be a “root” in a module
// with graph pruning enabled. // with graph pruning enabled.
mg, err := rs.Graph(modfetch.Fetcher_, loaderstate, ctx) mg, err := rs.Graph(loaderstate, ctx)
if err != nil { if err != nil {
// If we couldn't load the graph, we don't know what its requirements were // If we couldn't load the graph, we don't know what its requirements were
// to begin with, so we can't edit those requirements in a coherent way. // to begin with, so we can't edit those requirements in a coherent way.
@ -392,7 +392,7 @@ func editRequirements(loaderstate *State, ctx context.Context, rs *Requirements,
// the edit. We want to make sure we consider keeping it as-is, // the edit. We want to make sure we consider keeping it as-is,
// even if it wouldn't normally be included. (For example, it might // even if it wouldn't normally be included. (For example, it might
// be a pseudo-version or pre-release.) // be a pseudo-version or pre-release.)
origMG, _ := orig.Graph(modfetch.Fetcher_, loaderstate, ctx) origMG, _ := orig.Graph(loaderstate, ctx)
origV := origMG.Selected(m.Path) origV := origMG.Selected(m.Path)
if conflict.Err != nil && origV == m.Version { if conflict.Err != nil && origV == m.Version {
@ -610,7 +610,7 @@ func editRequirements(loaderstate *State, ctx context.Context, rs *Requirements,
// some root to that version. // some root to that version.
func extendGraph(loaderstate *State, ctx context.Context, rootPruning modPruning, roots []module.Version, selectedRoot map[string]string) (mg *ModuleGraph, upgradedRoot map[module.Version]bool, err error) { func extendGraph(loaderstate *State, ctx context.Context, rootPruning modPruning, roots []module.Version, selectedRoot map[string]string) (mg *ModuleGraph, upgradedRoot map[module.Version]bool, err error) {
for { for {
mg, err = readModGraph(modfetch.Fetcher_, loaderstate, ctx, rootPruning, roots, upgradedRoot) mg, err = readModGraph(loaderstate, ctx, rootPruning, roots, upgradedRoot)
// We keep on going even if err is non-nil until we reach a steady state. // We keep on going even if err is non-nil until we reach a steady state.
// (Note that readModGraph returns a non-nil *ModuleGraph even in case of // (Note that readModGraph returns a non-nil *ModuleGraph even in case of
// errors.) The caller may be able to fix the errors by adjusting versions, // errors.) The caller may be able to fix the errors by adjusting versions,

View file

@ -481,7 +481,7 @@ func importFromModules(loaderstate *State, ctx context.Context, path string, rs
// of a package in "all", we didn't necessarily load that file // of a package in "all", we didn't necessarily load that file
// when we read the module graph, so do it now to be sure. // when we read the module graph, so do it now to be sure.
if !skipModFile && cfg.BuildMod != "vendor" && mods[0].Path != "" && !loaderstate.MainModules.Contains(mods[0].Path) { if !skipModFile && cfg.BuildMod != "vendor" && mods[0].Path != "" && !loaderstate.MainModules.Contains(mods[0].Path) {
if _, err := goModSummary(modfetch.Fetcher_, loaderstate, mods[0]); err != nil { if _, err := goModSummary(loaderstate, mods[0]); err != nil {
return module.Version{}, "", "", nil, err return module.Version{}, "", "", nil, err
} }
} }
@ -506,7 +506,7 @@ func importFromModules(loaderstate *State, ctx context.Context, path string, rs
// So far we've checked the root dependencies. // So far we've checked the root dependencies.
// Load the full module graph and try again. // Load the full module graph and try again.
mg, err = rs.Graph(modfetch.Fetcher_, loaderstate, ctx) mg, err = rs.Graph(loaderstate, ctx)
if err != nil { if err != nil {
// We might be missing one or more transitive (implicit) dependencies from // We might be missing one or more transitive (implicit) dependencies from
// the module graph, so we can't return an ImportMissingError here — one // the module graph, so we can't return an ImportMissingError here — one
@ -543,7 +543,7 @@ func queryImport(loaderstate *State, ctx context.Context, path string, rs *Requi
mv = module.ZeroPseudoVersion("v0") mv = module.ZeroPseudoVersion("v0")
} }
} }
mg, err := rs.Graph(modfetch.Fetcher_, loaderstate, ctx) mg, err := rs.Graph(loaderstate, ctx)
if err != nil { if err != nil {
return module.Version{}, err return module.Version{}, err
} }
@ -637,7 +637,7 @@ func queryImport(loaderstate *State, ctx context.Context, path string, rs *Requi
// and return m, dir, ImportMissingError. // and return m, dir, ImportMissingError.
fmt.Fprintf(os.Stderr, "go: finding module for package %s\n", path) fmt.Fprintf(os.Stderr, "go: finding module for package %s\n", path)
mg, err := rs.Graph(modfetch.Fetcher_, loaderstate, ctx) mg, err := rs.Graph(loaderstate, ctx)
if err != nil { if err != nil {
return module.Version{}, err return module.Version{}, err
} }
@ -817,11 +817,11 @@ func fetch(loaderstate *State, ctx context.Context, mod module.Version) (dir str
mod = r mod = r
} }
if mustHaveSums(loaderstate) && !modfetch.HaveSum(modfetch.Fetcher_, mod) { if mustHaveSums(loaderstate) && !modfetch.HaveSum(loaderstate.Fetcher(), mod) {
return "", false, module.VersionError(mod, &sumMissingError{}) return "", false, module.VersionError(mod, &sumMissingError{})
} }
dir, err = modfetch.Fetcher_.Download(ctx, mod) dir, err = loaderstate.Fetcher().Download(ctx, mod)
return dir, false, err return dir, false, err
} }

View file

@ -457,10 +457,14 @@ type State struct {
func NewState() *State { func NewState() *State {
s := new(State) s := new(State)
s.fetcher = modfetch.NewFetcher() s.fetcher = modfetch.Fetcher_
return s return s
} }
func (s *State) Fetcher() *modfetch.Fetcher {
return s.fetcher
}
// Init determines whether module mode is enabled, locates the root of the // Init determines whether module mode is enabled, locates the root of the
// current module (if any), sets environment variables for Git subprocesses, and // current module (if any), sets environment variables for Git subprocesses, and
// configures the cfg, codehost, load, modfetch, and search packages for use // configures the cfg, codehost, load, modfetch, and search packages for use
@ -937,9 +941,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.Fetcher_.AddWorkspaceGoSumFile(sumFile) loaderstate.Fetcher().AddWorkspaceGoSumFile(sumFile)
} }
modfetch.Fetcher_.SetGoSumFile(loaderstate.workFilePath + ".sum") loaderstate.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 +963,7 @@ func loadModFile(loaderstate *State, ctx context.Context, opts *PackageOpts) (*R
// //
// See golang.org/issue/32027. // See golang.org/issue/32027.
} else { } else {
modfetch.Fetcher_.SetGoSumFile(strings.TrimSuffix(modFilePath(loaderstate.modRoots[0]), ".mod") + ".sum") loaderstate.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,
@ -1965,7 +1969,7 @@ func commitRequirements(loaderstate *State, ctx context.Context, opts WriteOpts)
if loaderstate.inWorkspaceMode() { if loaderstate.inWorkspaceMode() {
// go.mod files aren't updated in workspace mode, but we still want to // go.mod files aren't updated in workspace mode, but we still want to
// update the go.work.sum file. // update the go.work.sum file.
return modfetch.Fetcher_.WriteGoSum(ctx, keepSums(modfetch.Fetcher_, loaderstate, ctx, loaded, loaderstate.requirements, addBuildListZipSums), mustHaveCompleteRequirements(loaderstate)) return loaderstate.Fetcher().WriteGoSum(ctx, keepSums(loaderstate, ctx, loaded, loaderstate.requirements, addBuildListZipSums), mustHaveCompleteRequirements(loaderstate))
} }
_, updatedGoMod, modFile, err := UpdateGoModFromReqs(loaderstate, ctx, opts) _, updatedGoMod, modFile, err := UpdateGoModFromReqs(loaderstate, ctx, opts)
if err != nil { if err != nil {
@ -1989,7 +1993,7 @@ func commitRequirements(loaderstate *State, ctx context.Context, opts WriteOpts)
// Don't write go.mod, but write go.sum in case we added or trimmed sums. // Don't write go.mod, but write go.sum in case we added or trimmed sums.
// 'go mod init' shouldn't write go.sum, since it will be incomplete. // 'go mod init' shouldn't write go.sum, since it will be incomplete.
if cfg.CmdName != "mod init" { if cfg.CmdName != "mod init" {
if err := modfetch.Fetcher_.WriteGoSum(ctx, keepSums(modfetch.Fetcher_, loaderstate, ctx, loaded, loaderstate.requirements, addBuildListZipSums), mustHaveCompleteRequirements(loaderstate)); err != nil { if err := loaderstate.Fetcher().WriteGoSum(ctx, keepSums(loaderstate, ctx, loaded, loaderstate.requirements, addBuildListZipSums), mustHaveCompleteRequirements(loaderstate)); err != nil {
return err return err
} }
} }
@ -2012,7 +2016,7 @@ func commitRequirements(loaderstate *State, ctx context.Context, opts WriteOpts)
// 'go mod init' shouldn't write go.sum, since it will be incomplete. // 'go mod init' shouldn't write go.sum, since it will be incomplete.
if cfg.CmdName != "mod init" { if cfg.CmdName != "mod init" {
if err == nil { if err == nil {
err = modfetch.Fetcher_.WriteGoSum(ctx, keepSums(modfetch.Fetcher_, loaderstate, ctx, loaded, loaderstate.requirements, addBuildListZipSums), mustHaveCompleteRequirements(loaderstate)) err = loaderstate.Fetcher().WriteGoSum(ctx, keepSums(loaderstate, ctx, loaded, loaderstate.requirements, addBuildListZipSums), mustHaveCompleteRequirements(loaderstate))
} }
} }
}() }()
@ -2055,7 +2059,7 @@ func commitRequirements(loaderstate *State, ctx context.Context, opts WriteOpts)
// including any go.mod files needed to reconstruct the MVS result // including any go.mod files needed to reconstruct the MVS result
// or identify go versions, // or identify go versions,
// in addition to the checksums for every module in keepMods. // in addition to the checksums for every module in keepMods.
func keepSums(fetcher_ *modfetch.Fetcher, loaderstate *State, ctx context.Context, ld *loader, rs *Requirements, which whichSums) map[module.Version]bool { func keepSums(loaderstate *State, ctx context.Context, ld *loader, rs *Requirements, which whichSums) map[module.Version]bool {
// Every module in the full module graph contributes its requirements, // Every module in the full module graph contributes its requirements,
// so in order to ensure that the build list itself is reproducible, // so in order to ensure that the build list itself is reproducible,
// we need sums for every go.mod in the graph (regardless of whether // we need sums for every go.mod in the graph (regardless of whether
@ -2113,7 +2117,7 @@ func keepSums(fetcher_ *modfetch.Fetcher, loaderstate *State, ctx context.Contex
} }
} }
mg, _ := rs.Graph(fetcher_, loaderstate, ctx) mg, _ := rs.Graph(loaderstate, ctx)
for prefix := pkg.path; prefix != "."; prefix = path.Dir(prefix) { for prefix := pkg.path; prefix != "."; prefix = path.Dir(prefix) {
if v := mg.Selected(prefix); v != "none" { if v := mg.Selected(prefix); v != "none" {
m := module.Version{Path: prefix, Version: v} m := module.Version{Path: prefix, Version: v}
@ -2136,7 +2140,7 @@ func keepSums(fetcher_ *modfetch.Fetcher, loaderstate *State, ctx context.Contex
} }
} }
} else { } else {
mg, _ := rs.Graph(fetcher_, loaderstate, ctx) mg, _ := rs.Graph(loaderstate, ctx)
mg.WalkBreadthFirst(func(m module.Version) { mg.WalkBreadthFirst(func(m module.Version) {
if _, ok := mg.RequiredBy(m); ok { if _, ok := mg.RequiredBy(m); ok {
// The requirements from m's go.mod file are present in the module graph, // The requirements from m's go.mod file are present in the module graph,

View file

@ -311,7 +311,7 @@ func LoadPackages(loaderstate *State, ctx context.Context, opts PackageOpts, pat
case strings.Contains(m.Pattern(), "..."): case strings.Contains(m.Pattern(), "..."):
m.Errs = m.Errs[:0] m.Errs = m.Errs[:0]
mg, err := rs.Graph(modfetch.Fetcher_, loaderstate, ctx) mg, err := rs.Graph(loaderstate, ctx)
if err != nil { if err != nil {
// The module graph is (or may be) incomplete — perhaps we failed to // The module graph is (or may be) incomplete — perhaps we failed to
// load the requirements of some module. This is an error in matching // load the requirements of some module. This is an error in matching
@ -404,7 +404,7 @@ func LoadPackages(loaderstate *State, ctx context.Context, opts PackageOpts, pat
if opts.Tidy { if opts.Tidy {
if cfg.BuildV { if cfg.BuildV {
mg, _ := ld.requirements.Graph(modfetch.Fetcher_, loaderstate, ctx) mg, _ := ld.requirements.Graph(loaderstate, ctx)
for _, m := range initialRS.rootModules { for _, m := range initialRS.rootModules {
var unused bool var unused bool
if ld.requirements.pruning == unpruned { if ld.requirements.pruning == unpruned {
@ -425,7 +425,7 @@ func LoadPackages(loaderstate *State, ctx context.Context, opts PackageOpts, pat
} }
} }
keep := keepSums(modfetch.Fetcher_, loaderstate, ctx, ld, ld.requirements, loadedZipSumsOnly) keep := keepSums(loaderstate, ctx, ld, ld.requirements, loadedZipSumsOnly)
compatVersion := ld.TidyCompatibleVersion compatVersion := ld.TidyCompatibleVersion
goVersion := ld.requirements.GoVersion(loaderstate) goVersion := ld.requirements.GoVersion(loaderstate)
if compatVersion == "" { if compatVersion == "" {
@ -447,7 +447,7 @@ func LoadPackages(loaderstate *State, ctx context.Context, opts PackageOpts, pat
compatRS := newRequirements(loaderstate, compatPruning, ld.requirements.rootModules, ld.requirements.direct) compatRS := newRequirements(loaderstate, compatPruning, ld.requirements.rootModules, ld.requirements.direct)
ld.checkTidyCompatibility(loaderstate, ctx, compatRS, compatVersion) ld.checkTidyCompatibility(loaderstate, ctx, compatRS, compatVersion)
for m := range keepSums(modfetch.Fetcher_, loaderstate, ctx, ld, compatRS, loadedZipSumsOnly) { for m := range keepSums(loaderstate, ctx, ld, compatRS, loadedZipSumsOnly) {
keep[m] = true keep[m] = true
} }
} }
@ -466,7 +466,7 @@ func LoadPackages(loaderstate *State, ctx context.Context, opts PackageOpts, pat
// Dropping compatibility for 1.16 may result in a strictly smaller go.sum. // Dropping compatibility for 1.16 may result in a strictly smaller go.sum.
// Update the keep map with only the loaded.requirements. // Update the keep map with only the loaded.requirements.
if gover.Compare(compatVersion, "1.16") > 0 { if gover.Compare(compatVersion, "1.16") > 0 {
keep = keepSums(modfetch.Fetcher_, loaderstate, ctx, loaded, loaderstate.requirements, addBuildListZipSums) keep = keepSums(loaderstate, ctx, loaded, loaderstate.requirements, addBuildListZipSums)
} }
currentGoSum, tidyGoSum := modfetch.TidyGoSum(keep) currentGoSum, tidyGoSum := modfetch.TidyGoSum(keep)
goSumDiff := diff.Diff("current/go.sum", currentGoSum, "tidy/go.sum", tidyGoSum) goSumDiff := diff.Diff("current/go.sum", currentGoSum, "tidy/go.sum", tidyGoSum)
@ -490,7 +490,7 @@ func LoadPackages(loaderstate *State, ctx context.Context, opts PackageOpts, pat
// loaded.requirements, but here we may have also loaded (and want to // loaded.requirements, but here we may have also loaded (and want to
// preserve checksums for) additional entities from compatRS, which are // preserve checksums for) additional entities from compatRS, which are
// only needed for compatibility with ld.TidyCompatibleVersion. // only needed for compatibility with ld.TidyCompatibleVersion.
if err := modfetch.Fetcher_.WriteGoSum(ctx, keep, mustHaveCompleteRequirements(loaderstate)); err != nil { if err := loaderstate.Fetcher().WriteGoSum(ctx, keep, mustHaveCompleteRequirements(loaderstate)); err != nil {
base.Fatal(err) base.Fatal(err)
} }
} }
@ -747,7 +747,7 @@ func pathInModuleCache(loaderstate *State, ctx context.Context, dir string, rs *
// versions of root modules may differ from what we already checked above. // versions of root modules may differ from what we already checked above.
// Re-check those paths too. // Re-check those paths too.
mg, _ := rs.Graph(modfetch.Fetcher_, loaderstate, ctx) mg, _ := rs.Graph(loaderstate, ctx)
var importPath string var importPath string
for _, m := range mg.BuildList() { for _, m := range mg.BuildList() {
var found bool var found bool
@ -1253,7 +1253,7 @@ func loadFromRoots(loaderstate *State, ctx context.Context, params loaderParams)
// pruning and semantics all along, but there may have been — and may // pruning and semantics all along, but there may have been — and may
// still be — requirements on higher versions in the graph. // still be — requirements on higher versions in the graph.
tidy := overrideRoots(loaderstate, ctx, rs, []module.Version{{Path: "go", Version: ld.TidyGoVersion}}) tidy := overrideRoots(loaderstate, ctx, rs, []module.Version{{Path: "go", Version: ld.TidyGoVersion}})
mg, err := tidy.Graph(modfetch.Fetcher_, loaderstate, ctx) mg, err := tidy.Graph(loaderstate, ctx)
if err != nil { if err != nil {
ld.error(err) ld.error(err)
} }
@ -1412,7 +1412,7 @@ func (ld *loader) updateRequirements(loaderstate *State, ctx context.Context) (c
// of the vendor directory anyway. // of the vendor directory anyway.
continue continue
} }
if mg, err := rs.Graph(modfetch.Fetcher_, loaderstate, ctx); err != nil { if mg, err := rs.Graph(loaderstate, ctx); err != nil {
return false, err return false, err
} else if _, ok := mg.RequiredBy(dep.mod); !ok { } else if _, ok := mg.RequiredBy(dep.mod); !ok {
// dep.mod is not an explicit dependency, but needs to be. // dep.mod is not an explicit dependency, but needs to be.
@ -1515,7 +1515,7 @@ func (ld *loader) updateRequirements(loaderstate *State, ctx context.Context) (c
// The roots of the module graph have changed in some way (not just the // The roots of the module graph have changed in some way (not just the
// "direct" markings). Check whether the changes affected any of the loaded // "direct" markings). Check whether the changes affected any of the loaded
// packages. // packages.
mg, err := rs.Graph(modfetch.Fetcher_, loaderstate, ctx) mg, err := rs.Graph(loaderstate, ctx)
if err != nil { if err != nil {
return false, err return false, err
} }
@ -1841,7 +1841,7 @@ func (ld *loader) load(loaderstate *State, ctx context.Context, pkg *loadPkg) {
var mg *ModuleGraph var mg *ModuleGraph
if ld.requirements.pruning == unpruned { if ld.requirements.pruning == unpruned {
var err error var err error
mg, err = ld.requirements.Graph(modfetch.Fetcher_, loaderstate, ctx) mg, err = ld.requirements.Graph(loaderstate, ctx)
if err != nil { if err != nil {
// We already checked the error from Graph in loadFromRoots and/or // We already checked the error from Graph in loadFromRoots and/or
// updateRequirements, so we ignored the error on purpose and we should // updateRequirements, so we ignored the error on purpose and we should
@ -2095,7 +2095,7 @@ func (ld *loader) checkTidyCompatibility(loaderstate *State, ctx context.Context
fmt.Fprintf(os.Stderr, "For information about 'go mod tidy' compatibility, see:\n\thttps://go.dev/ref/mod#graph-pruning\n") fmt.Fprintf(os.Stderr, "For information about 'go mod tidy' compatibility, see:\n\thttps://go.dev/ref/mod#graph-pruning\n")
} }
mg, err := rs.Graph(modfetch.Fetcher_, loaderstate, ctx) mg, err := rs.Graph(loaderstate, ctx)
if err != nil { if err != nil {
ld.error(fmt.Errorf("error loading go %s module graph: %w", compatVersion, err)) ld.error(fmt.Errorf("error loading go %s module graph: %w", compatVersion, err))
ld.switchIfErrors(ctx) ld.switchIfErrors(ctx)

View file

@ -214,7 +214,7 @@ func (s *State) CheckRetractions(ctx context.Context, m module.Version) (err err
if err != nil { if err != nil {
return err return err
} }
summary, err := rawGoModSummary(modfetch.Fetcher_, s, rm) summary, err := rawGoModSummary(s, rm)
if err != nil && !errors.Is(err, gover.ErrTooNew) { if err != nil && !errors.Is(err, gover.ErrTooNew) {
return err return err
} }
@ -322,7 +322,7 @@ func CheckDeprecation(loaderstate *State, ctx context.Context, m module.Version)
if err != nil { if err != nil {
return "", err return "", err
} }
summary, err := rawGoModSummary(modfetch.Fetcher_, loaderstate, latest) summary, err := rawGoModSummary(loaderstate, latest)
if err != nil && !errors.Is(err, gover.ErrTooNew) { if err != nil && !errors.Is(err, gover.ErrTooNew) {
return "", err return "", err
} }
@ -573,12 +573,12 @@ type retraction struct {
// module versions. // module versions.
// //
// The caller must not modify the returned summary. // The caller must not modify the returned summary.
func goModSummary(fetcher_ *modfetch.Fetcher, loaderstate *State, m module.Version) (*modFileSummary, error) { func goModSummary(loaderstate *State, m module.Version) (*modFileSummary, error) {
if m.Version == "" && !loaderstate.inWorkspaceMode() && loaderstate.MainModules.Contains(m.Path) { if m.Version == "" && !loaderstate.inWorkspaceMode() && loaderstate.MainModules.Contains(m.Path) {
panic("internal error: goModSummary called on a main module") panic("internal error: goModSummary called on a main module")
} }
if gover.IsToolchain(m.Path) { if gover.IsToolchain(m.Path) {
return rawGoModSummary(fetcher_, loaderstate, m) return rawGoModSummary(loaderstate, m)
} }
if cfg.BuildMod == "vendor" { if cfg.BuildMod == "vendor" {
@ -604,12 +604,12 @@ func goModSummary(fetcher_ *modfetch.Fetcher, loaderstate *State, m module.Versi
actual := resolveReplacement(loaderstate, m) actual := resolveReplacement(loaderstate, m)
if mustHaveSums(loaderstate) && actual.Version != "" { if mustHaveSums(loaderstate) && actual.Version != "" {
key := module.Version{Path: actual.Path, Version: actual.Version + "/go.mod"} key := module.Version{Path: actual.Path, Version: actual.Version + "/go.mod"}
if !modfetch.HaveSum(fetcher_, key) { if !modfetch.HaveSum(loaderstate.Fetcher(), key) {
suggestion := fmt.Sprintf(" for go.mod file; to add it:\n\tgo mod download %s", m.Path) suggestion := fmt.Sprintf(" for go.mod file; to add it:\n\tgo mod download %s", m.Path)
return nil, module.VersionError(actual, &sumMissingError{suggestion: suggestion}) return nil, module.VersionError(actual, &sumMissingError{suggestion: suggestion})
} }
} }
summary, err := rawGoModSummary(fetcher_, loaderstate, actual) summary, err := rawGoModSummary(loaderstate, actual)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -676,7 +676,7 @@ func goModSummary(fetcher_ *modfetch.Fetcher, loaderstate *State, m module.Versi
// rawGoModSummary cannot be used on the main module outside of workspace mode. // rawGoModSummary cannot be used on the main module outside of workspace mode.
// The modFileSummary can still be used for retractions and deprecations // The modFileSummary can still be used for retractions and deprecations
// even if a TooNewError is returned. // even if a TooNewError is returned.
func rawGoModSummary(fetcher_ *modfetch.Fetcher, loaderstate *State, m module.Version) (*modFileSummary, error) { func rawGoModSummary(loaderstate *State, m module.Version) (*modFileSummary, error) {
if gover.IsToolchain(m.Path) { if gover.IsToolchain(m.Path) {
if m.Path == "go" && gover.Compare(m.Version, gover.GoStrictVersion) >= 0 { if m.Path == "go" && gover.Compare(m.Version, gover.GoStrictVersion) >= 0 {
// Declare that go 1.21.3 requires toolchain 1.21.3, // Declare that go 1.21.3 requires toolchain 1.21.3,
@ -709,7 +709,7 @@ func rawGoModSummary(fetcher_ *modfetch.Fetcher, loaderstate *State, m module.Ve
} }
} }
return rawGoModSummaryCache.Do(m, func() (*modFileSummary, error) { return rawGoModSummaryCache.Do(m, func() (*modFileSummary, error) {
name, data, err := rawGoModData(fetcher_, loaderstate, m) name, data, err := rawGoModData(loaderstate, m)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -781,7 +781,7 @@ var rawGoModSummaryCache par.ErrCache[module.Version, *modFileSummary]
// //
// Unlike rawGoModSummary, rawGoModData does not cache its results in memory. // Unlike rawGoModSummary, rawGoModData does not cache its results in memory.
// Use rawGoModSummary instead unless you specifically need these bytes. // Use rawGoModSummary instead unless you specifically need these bytes.
func rawGoModData(fetcher_ *modfetch.Fetcher, loaderstate *State, m module.Version) (name string, data []byte, err error) { func rawGoModData(loaderstate *State, m module.Version) (name string, data []byte, err error) {
if m.Version == "" { if m.Version == "" {
dir := m.Path dir := m.Path
if !filepath.IsAbs(dir) { if !filepath.IsAbs(dir) {
@ -810,7 +810,7 @@ func rawGoModData(fetcher_ *modfetch.Fetcher, loaderstate *State, m module.Versi
base.Fatalf("go: internal error: %s@%s: unexpected invalid semantic version", m.Path, m.Version) base.Fatalf("go: internal error: %s@%s: unexpected invalid semantic version", m.Path, m.Version)
} }
name = "go.mod" name = "go.mod"
data, err = fetcher_.GoMod(context.TODO(), m.Path, m.Version) data, err = loaderstate.Fetcher().GoMod(context.TODO(), m.Path, m.Version)
} }
return name, data, err return name, data, err
} }

View file

@ -54,7 +54,7 @@ func (r *mvsReqs) Required(mod module.Version) ([]module.Version, error) {
return nil, nil return nil, nil
} }
summary, err := goModSummary(modfetch.Fetcher_, r.loaderstate, mod) summary, err := goModSummary(r.loaderstate, mod)
if err != nil { if err != nil {
return nil, err return nil, err
} }

View file

@ -1099,7 +1099,7 @@ func (e *PackageNotInModuleError) ImportPath() string {
// we don't need to verify it in go.sum. This makes 'go list -m -u' faster // we don't need to verify it in go.sum. This makes 'go list -m -u' faster
// and simpler. // and simpler.
func versionHasGoMod(loaderstate *State, _ context.Context, m module.Version) (bool, error) { func versionHasGoMod(loaderstate *State, _ context.Context, m module.Version) (bool, error) {
_, data, err := rawGoModData(modfetch.Fetcher_, loaderstate, m) _, data, err := rawGoModData(loaderstate, m)
if err != nil { if err != nil {
return false, err return false, err
} }
@ -1124,7 +1124,7 @@ func lookupRepo(loaderstate *State, ctx context.Context, proxy, path string) (re
err = module.CheckPath(path) err = module.CheckPath(path)
} }
if err == nil { if err == nil {
repo = modfetch.Fetcher_.Lookup(ctx, proxy, path) repo = loaderstate.Fetcher().Lookup(ctx, proxy, path)
} else { } else {
repo = emptyRepo{path: path, err: err} repo = emptyRepo{path: path, err: err}
} }
@ -1150,9 +1150,11 @@ func (er emptyRepo) ModulePath() string { return er.path }
func (er emptyRepo) CheckReuse(ctx context.Context, old *codehost.Origin) error { func (er emptyRepo) CheckReuse(ctx context.Context, old *codehost.Origin) error {
return fmt.Errorf("empty repo") return fmt.Errorf("empty repo")
} }
func (er emptyRepo) Versions(ctx context.Context, prefix string) (*modfetch.Versions, error) { func (er emptyRepo) Versions(ctx context.Context, prefix string) (*modfetch.Versions, error) {
return &modfetch.Versions{}, nil return &modfetch.Versions{}, nil
} }
func (er emptyRepo) Stat(ctx context.Context, rev string) (*modfetch.RevInfo, error) { func (er emptyRepo) Stat(ctx context.Context, rev string) (*modfetch.RevInfo, error) {
return nil, er.err return nil, er.err
} }

View file

@ -21,7 +21,6 @@ import (
"cmd/go/internal/fsys" "cmd/go/internal/fsys"
"cmd/go/internal/gover" "cmd/go/internal/gover"
"cmd/go/internal/imports" "cmd/go/internal/imports"
"cmd/go/internal/modfetch"
"cmd/go/internal/modindex" "cmd/go/internal/modindex"
"cmd/go/internal/search" "cmd/go/internal/search"
"cmd/go/internal/str" "cmd/go/internal/str"
@ -349,7 +348,7 @@ func parseIgnorePatterns(loaderstate *State, ctx context.Context, treeCanMatch f
if err != nil { if err != nil {
continue continue
} }
summary, err := goModSummary(modfetch.Fetcher_, loaderstate, mod) summary, err := goModSummary(loaderstate, mod)
if err != nil { if err != nil {
continue continue
} }