]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/go/internal/modcmd: eliminate a call to modload.LoadedModules
authorBryan C. Mills <bcmills@google.com>
Wed, 18 Nov 2020 03:13:35 +0000 (22:13 -0500)
committerBryan C. Mills <bcmills@google.com>
Thu, 19 Nov 2020 04:02:42 +0000 (04:02 +0000)
modload.LoadedModules reveals more information than necessary about
whether modules have been loaded lazily. The 'vendor' subcommand
doesn't actually need that much information: it has all of the
information that it needs from prior calls to LoadPackages and
ModFile.

For #36460

Change-Id: If08733cca930b2b80616b037b63985ecfd6a320b
Reviewed-on: https://go-review.googlesource.com/c/go/+/270979
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>
src/cmd/go/internal/modcmd/vendor.go

index 1b9ce6052943448c2d9444abdfa3961b2147ae77..4e73960e80d7b5c52e4913f8cf199aabbc8f5f43 100644 (file)
@@ -73,7 +73,7 @@ func runVendor(ctx context.Context, cmd *base.Command, args []string) {
        modpkgs := make(map[module.Version][]string)
        for _, pkg := range pkgs {
                m := modload.PackageModule(pkg)
-               if m == modload.Target {
+               if m.Path == "" || m == modload.Target {
                        continue
                }
                modpkgs[m] = append(modpkgs[m], pkg)
@@ -91,28 +91,38 @@ func runVendor(ctx context.Context, cmd *base.Command, args []string) {
                includeAllReplacements = true
        }
 
+       var vendorMods []module.Version
+       for m := range isExplicit {
+               vendorMods = append(vendorMods, m)
+       }
+       for m := range modpkgs {
+               if !isExplicit[m] {
+                       vendorMods = append(vendorMods, m)
+               }
+       }
+       module.Sort(vendorMods)
+
        var buf bytes.Buffer
-       for _, m := range modload.LoadedModules()[1:] {
-               if pkgs := modpkgs[m]; len(pkgs) > 0 || isExplicit[m] {
-                       line := moduleLine(m, modload.Replacement(m))
-                       buf.WriteString(line)
+       for _, m := range vendorMods {
+               line := moduleLine(m, modload.Replacement(m))
+               buf.WriteString(line)
+               if cfg.BuildV {
+                       os.Stderr.WriteString(line)
+               }
+               if isExplicit[m] {
+                       buf.WriteString("## explicit\n")
                        if cfg.BuildV {
-                               os.Stderr.WriteString(line)
-                       }
-                       if isExplicit[m] {
-                               buf.WriteString("## explicit\n")
-                               if cfg.BuildV {
-                                       os.Stderr.WriteString("## explicit\n")
-                               }
+                               os.Stderr.WriteString("## explicit\n")
                        }
-                       sort.Strings(pkgs)
-                       for _, pkg := range pkgs {
-                               fmt.Fprintf(&buf, "%s\n", pkg)
-                               if cfg.BuildV {
-                                       fmt.Fprintf(os.Stderr, "%s\n", pkg)
-                               }
-                               vendorPkg(vdir, pkg)
+               }
+               pkgs := modpkgs[m]
+               sort.Strings(pkgs)
+               for _, pkg := range pkgs {
+                       fmt.Fprintf(&buf, "%s\n", pkg)
+                       if cfg.BuildV {
+                               fmt.Fprintf(os.Stderr, "%s\n", pkg)
                        }
+                       vendorPkg(vdir, pkg)
                }
        }