1
0
mirror of https://github.com/golang/go synced 2024-11-22 20:50:05 -07:00

cmd/go/internal/modload: avoid loading the full module graph to determine which checksums to add to go.sum

For #36460

Change-Id: I606314054bd9064f7c4053f56049fabbaec54143
Reviewed-on: https://go-review.googlesource.com/c/go/+/309189
Trust: Bryan C. Mills <bcmills@google.com>
Run-TryBot: Bryan C. Mills <bcmills@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Jay Conrod <jayconrod@google.com>
Reviewed-by: Michael Matloob <matloob@golang.org>
This commit is contained in:
Bryan C. Mills 2021-04-09 22:00:14 -04:00
parent 4063605e0d
commit ee4f9656ac

View File

@ -1094,34 +1094,16 @@ func keepSums(ctx context.Context, ld *loader, rs *Requirements, which whichSums
// that version is selected).
keep := make(map[module.Version]bool)
if go117LazyTODO {
// If the main module is lazy, avoid loading the module graph if it hasn't
// already been loaded.
}
mg, _ := rs.Graph(ctx)
mg.WalkBreadthFirst(func(m module.Version) {
if _, ok := mg.RequiredBy(m); ok {
// The requirements from m's go.mod file are present in the module graph,
// so they are relevant to the MVS result regardless of whether m was
// actually selected.
keep[modkey(resolveReplacement(m))] = true
}
})
if which == addBuildListZipSums {
for _, m := range mg.BuildList() {
keep[resolveReplacement(m)] = true
}
}
// Add entries for modules in the build list with paths that are prefixes of
// paths of loaded packages. We need to retain sums for all of these modules —
// not just the modules containing the actual packages — in order to rule out
// ambiguous import errors the next time we load the package.
if ld != nil {
for _, pkg := range ld.pkgs {
if pkg.testOf != nil || pkg.inStd || module.CheckImportPath(pkg.path) != nil {
// We check pkg.mod.Path here instead of pkg.inStd because the
// pseudo-package "C" is not in std, but not provided by any module (and
// shouldn't force loading the whole module graph).
if pkg.testOf != nil || (pkg.mod.Path == "" && pkg.err == nil) || module.CheckImportPath(pkg.path) != nil {
continue
}
@ -1141,6 +1123,7 @@ func keepSums(ctx context.Context, ld *loader, rs *Requirements, which whichSums
}
}
mg, _ := rs.Graph(ctx)
for prefix := pkg.path; prefix != "."; prefix = path.Dir(prefix) {
if v := mg.Selected(prefix); v != "none" {
m := module.Version{Path: prefix, Version: v}
@ -1150,6 +1133,37 @@ func keepSums(ctx context.Context, ld *loader, rs *Requirements, which whichSums
}
}
if rs.depth == lazy && rs.graph.Load() == nil {
// The main module is lazy and we haven't needed to load the module graph so
// far. Don't incur the cost of loading it now — since we haven't loaded the
// graph, we probably don't have any checksums to contribute to the distant
// parts of the graph anyway. Instead, just request sums for the roots that
// we know about.
for _, m := range rs.rootModules {
r := resolveReplacement(m)
keep[modkey(r)] = true
if which == addBuildListZipSums {
keep[r] = true
}
}
} else {
mg, _ := rs.Graph(ctx)
mg.WalkBreadthFirst(func(m module.Version) {
if _, ok := mg.RequiredBy(m); ok {
// The requirements from m's go.mod file are present in the module graph,
// so they are relevant to the MVS result regardless of whether m was
// actually selected.
keep[modkey(resolveReplacement(m))] = true
}
})
if which == addBuildListZipSums {
for _, m := range mg.BuildList() {
keep[resolveReplacement(m)] = true
}
}
}
return keep
}