diff options
author | Bryan C. Mills <bcmills@google.com> | 2019-10-01 15:48:17 -0400 |
---|---|---|
committer | Bryan C. Mills <bcmills@google.com> | 2019-10-09 18:12:31 +0000 |
commit | 1736f3a126ce0980e2584aebb35c94e8b33b1bfb (patch) | |
tree | 4f8cc7232a36b9d4195fd3862cd539244b05ae53 /src/cmd/go/internal/modcmd/vendor.go | |
parent | dae8e719614f6e3d2e4e66071e5c91964b404676 (diff) | |
download | go-1736f3a126ce0980e2584aebb35c94e8b33b1bfb.tar.gz go-1736f3a126ce0980e2584aebb35c94e8b33b1bfb.zip |
cmd/go: automatically check and use vendored packages
This implements the proposal described in
https://golang.org/issue/33848#issuecomment-537222782.
Fixes #33848
Change-Id: Ia34d6500ca396b6aa644b920233716c6b83ef729
Reviewed-on: https://go-review.googlesource.com/c/go/+/198319
Run-TryBot: Bryan C. Mills <bcmills@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Jay Conrod <jayconrod@google.com>
Diffstat (limited to 'src/cmd/go/internal/modcmd/vendor.go')
-rw-r--r-- | src/cmd/go/internal/modcmd/vendor.go | 63 |
1 files changed, 53 insertions, 10 deletions
diff --git a/src/cmd/go/internal/modcmd/vendor.go b/src/cmd/go/internal/modcmd/vendor.go index 75513f1d9c..8509b8b190 100644 --- a/src/cmd/go/internal/modcmd/vendor.go +++ b/src/cmd/go/internal/modcmd/vendor.go @@ -59,19 +59,24 @@ func runVendor(cmd *base.Command, args []string) { modpkgs[m] = append(modpkgs[m], pkg) } + isExplicit := map[module.Version]bool{} + for _, r := range modload.ModFile().Require { + isExplicit[r.Mod] = true + } + var buf bytes.Buffer for _, m := range modload.BuildList()[1:] { - if pkgs := modpkgs[m]; len(pkgs) > 0 { - repl := "" - if r := modload.Replacement(m); r.Path != "" { - repl = " => " + r.Path - if r.Version != "" { - repl += " " + r.Version - } - } - fmt.Fprintf(&buf, "# %s %s%s\n", m.Path, m.Version, repl) + if pkgs := modpkgs[m]; len(pkgs) > 0 || isExplicit[m] { + line := moduleLine(m, modload.Replacement(m)) + buf.WriteString(line) if cfg.BuildV { - fmt.Fprintf(os.Stderr, "# %s %s%s\n", m.Path, m.Version, repl) + os.Stderr.WriteString(line) + } + if isExplicit[m] { + buf.WriteString("## explicit\n") + if cfg.BuildV { + os.Stderr.WriteString("## explicit\n") + } } sort.Strings(pkgs) for _, pkg := range pkgs { @@ -83,6 +88,24 @@ func runVendor(cmd *base.Command, args []string) { } } } + + // Record unused and wildcard replacements at the end of the modules.txt file: + // without access to the complete build list, the consumer of the vendor + // directory can't otherwise determine that those replacements had no effect. + for _, r := range modload.ModFile().Replace { + if len(modpkgs[r.Old]) > 0 { + // We we already recorded this replacement in the entry for the replaced + // module with the packages it provides. + continue + } + + line := moduleLine(r.Old, r.New) + buf.WriteString(line) + if cfg.BuildV { + os.Stderr.WriteString(line) + } + } + if buf.Len() == 0 { fmt.Fprintf(os.Stderr, "go: no dependencies to vendor\n") return @@ -92,6 +115,26 @@ func runVendor(cmd *base.Command, args []string) { } } +func moduleLine(m, r module.Version) string { + b := new(strings.Builder) + b.WriteString("# ") + b.WriteString(m.Path) + if m.Version != "" { + b.WriteString(" ") + b.WriteString(m.Version) + } + if r.Path != "" { + b.WriteString(" => ") + b.WriteString(r.Path) + if r.Version != "" { + b.WriteString(" ") + b.WriteString(r.Version) + } + } + b.WriteString("\n") + return b.String() +} + func vendorPkg(vdir, pkg string) { realPath := modload.ImportMap(pkg) if realPath != pkg && modload.ImportMap(realPath) != "" { |