]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/go/internal/modload: factor out a method to update loader requirements
authorBryan C. Mills <bcmills@google.com>
Thu, 25 Mar 2021 03:31:32 +0000 (23:31 -0400)
committerBryan C. Mills <bcmills@google.com>
Tue, 6 Apr 2021 19:13:44 +0000 (19:13 +0000)
For #36460

Change-Id: Idb0b333a28d2470bc9482fe1829ccb6ddf8abd34
Reviewed-on: https://go-review.googlesource.com/c/go/+/304909
Trust: Bryan C. Mills <bcmills@google.com>
Run-TryBot: Bryan C. Mills <bcmills@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Michael Matloob <matloob@golang.org>
src/cmd/go/internal/modload/load.go

index 386b53938c987c540e005fa27f90f5207e664bfd..5bff17e5797624020fac696334cd9a54d0645b7b 100644 (file)
@@ -940,6 +940,34 @@ func loadFromRoots(ctx context.Context, params loaderParams) *loader {
        }
        base.ExitIfErrors() // TODO(bcmills): Is this actually needed?
 
+       if err := ld.updateRequirements(ctx); err != nil {
+               base.Fatalf("go: %v", err)
+       }
+
+       if go117LazyTODO {
+               // Promoting a root can pull in previously-irrelevant requirements,
+               // changing the build list. Iterate until the roots are stable.
+       }
+
+       return ld
+}
+
+// updateRequirements ensures that ld.requirements is consistent with
+// the information gained from ld.pkgs.
+//
+// In particular:
+//
+//     - Modules that provide packages directly imported from the main module are
+//       marked as direct, and are promoted to explicit roots. If a needed root
+//       cannot be promoted due to -mod=readonly or -mod=vendor, the importing
+//       package is marked with an error.
+//
+//     - If ld scanned the "all" pattern independent of build constraints, it is
+//       guaranteed to have seen every direct import. Module dependencies that did
+//       not provide any directly-imported package are then marked as indirect.
+//
+//     - Root dependencies are updated to their selected versions.
+func (ld *loader) updateRequirements(ctx context.Context) error {
        rs := ld.requirements
 
        // Compute directly referenced dependency modules.
@@ -956,11 +984,11 @@ func loadFromRoots(ctx context.Context, params loaderParams) *loader {
                        if pkg.err == nil && cfg.BuildMod != "mod" {
                                if v, ok := rs.rootSelected(dep.mod.Path); !ok || v != dep.mod.Version {
                                        // dep.mod is not an explicit dependency, but needs to be.
-                                       // Because we are not in "mod" mod, we will not be able to update it.
+                                       // Because we are not in "mod" mode, we will not be able to update it.
                                        // Instead, mark the importing package with an error.
                                        //
                                        // TODO(#41688): The resulting error message fails to include the file
-                                       // position of the erroneous import (because that information is not
+                                       // position of the import statement (because that information is not
                                        // tracked by the module loader). Figure out how to plumb the import
                                        // position through.
                                        pkg.err = &DirectImportFromImplicitDependencyError{
@@ -983,32 +1011,20 @@ func loadFromRoots(ctx context.Context, params loaderParams) *loader {
 
        // If we didn't scan all of the imports from the main module, or didn't use
        // imports.AnyTags, then we didn't necessarily load every package that
-       // contributes “direct” imports — so we can't safely mark existing
-       // direct dependencies in ld.requirements as indirect-only. Propagate them as direct.
-       if !ld.loadedDirect() {
+       // contributes “direct” imports — so we can't safely mark existing direct
+       // dependencies in ld.requirements as indirect-only. Propagate them as direct.
+       loadedDirect := ld.allPatternIsRoot && reflect.DeepEqual(ld.Tags, imports.AnyTags())
+       if !loadedDirect {
                for mPath := range rs.direct {
                        direct[mPath] = true
                }
        }
 
-       var err error
-       ld.requirements, err = updateRoots(ctx, direct, ld.pkgs, ld.requirements)
-       if err != nil {
-               base.Errorf("go: %v", err)
-       }
-
-       if go117LazyTODO {
-               // Promoting a root can pull in previously-irrelevant requirements,
-               // changing the build list. Iterate until the roots are stable.
+       rs, err := updateRoots(ctx, direct, ld.pkgs, rs)
+       if err == nil {
+               ld.requirements = rs
        }
-
-       return ld
-}
-
-// loadedDirect reports whether ld loaded all of the packages that are directly
-// imported by any package or test in the main module.
-func (ld *loader) loadedDirect() bool {
-       return ld.allPatternIsRoot && reflect.DeepEqual(ld.Tags, imports.AnyTags())
+       return err
 }
 
 // resolveMissingImports returns a set of modules that could be added as