]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/go/internal/modload: avoid loading the full module graph to determine which check...
authorBryan C. Mills <bcmills@google.com>
Sat, 10 Apr 2021 02:00:14 +0000 (22:00 -0400)
committerBryan C. Mills <bcmills@google.com>
Fri, 30 Apr 2021 18:06:15 +0000 (18:06 +0000)
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>
src/cmd/go/internal/modload/init.go

index f46c58f4745f9fac7550956a731aadded9f10f07..ef21908064609b9e884ead7b2854b9b5f8745283 100644 (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
 }