From ee4f9656ac419bb92408f65f082c735a6b82d1fa Mon Sep 17 00:00:00 2001 From: "Bryan C. Mills" Date: Fri, 9 Apr 2021 22:00:14 -0400 Subject: [PATCH] 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 Run-TryBot: Bryan C. Mills TryBot-Result: Go Bot Reviewed-by: Jay Conrod Reviewed-by: Michael Matloob --- src/cmd/go/internal/modload/init.go | 58 ++++++++++++++++++----------- 1 file changed, 36 insertions(+), 22 deletions(-) diff --git a/src/cmd/go/internal/modload/init.go b/src/cmd/go/internal/modload/init.go index f46c58f474..ef21908064 100644 --- a/src/cmd/go/internal/modload/init.go +++ b/src/cmd/go/internal/modload/init.go @@ -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 } -- 2.50.0