From e7394e178d0ccf2053238b5cb28805f3062907c5 Mon Sep 17 00:00:00 2001 From: "Bryan C. Mills" Date: Tue, 15 Oct 2019 13:39:13 -0400 Subject: [PATCH] cmd/go: omit new 'vendor/modules.txt' annotations if the go version is 1.13 or lower Updates #33848 Change-Id: I887d78179d467030f4177d1834ccefee28a55c8a Reviewed-on: https://go-review.googlesource.com/c/go/+/201257 Run-TryBot: Bryan C. Mills TryBot-Result: Gobot Gobot Reviewed-by: Jay Conrod --- src/cmd/go/internal/modcmd/vendor.go | 40 ++++++++++++------- .../go/testdata/script/mod_vendor_auto.txt | 17 +++++--- 2 files changed, 36 insertions(+), 21 deletions(-) diff --git a/src/cmd/go/internal/modcmd/vendor.go b/src/cmd/go/internal/modcmd/vendor.go index 8509b8b190..bb1cecdbf5 100644 --- a/src/cmd/go/internal/modcmd/vendor.go +++ b/src/cmd/go/internal/modcmd/vendor.go @@ -19,6 +19,7 @@ import ( "cmd/go/internal/imports" "cmd/go/internal/modload" "cmd/go/internal/module" + "cmd/go/internal/semver" ) var cmdVendor = &base.Command{ @@ -59,9 +60,16 @@ func runVendor(cmd *base.Command, args []string) { modpkgs[m] = append(modpkgs[m], pkg) } + includeAllReplacements := false isExplicit := map[module.Version]bool{} - for _, r := range modload.ModFile().Require { - isExplicit[r.Mod] = true + if gv := modload.ModFile().Go; gv != nil && semver.Compare("v"+gv.Version, "v1.14") >= 0 { + // If the Go version is at least 1.14, annotate all explicit 'require' and + // 'replace' targets found in the go.mod file so that we can perform a + // stronger consistency check when -mod=vendor is set. + for _, r := range modload.ModFile().Require { + isExplicit[r.Mod] = true + } + includeAllReplacements = true } var buf bytes.Buffer @@ -89,20 +97,22 @@ 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 - } + if includeAllReplacements { + // 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) + line := moduleLine(r.Old, r.New) + buf.WriteString(line) + if cfg.BuildV { + os.Stderr.WriteString(line) + } } } diff --git a/src/cmd/go/testdata/script/mod_vendor_auto.txt b/src/cmd/go/testdata/script/mod_vendor_auto.txt index c80aa6ad63..7abe833f57 100644 --- a/src/cmd/go/testdata/script/mod_vendor_auto.txt +++ b/src/cmd/go/testdata/script/mod_vendor_auto.txt @@ -6,6 +6,7 @@ env GO111MODULE=on cd $WORK/auto cp go.mod go.mod.orig +cp $WORK/modules-1.13.txt $WORK/auto/modules.txt # An explicit -mod=vendor should force use of the vendor directory. env GOFLAGS=-mod=vendor @@ -145,19 +146,23 @@ stderr '^go: inconsistent vendoring in '$WORK/auto':$' stderr '^\texample.com/printversion@v1.0.0: is explicitly required in go.mod, but vendor/modules.txt indicates example.com/printversion@v1.1.0$' stderr '\n\nrun .go mod vendor. to sync, or use -mod=mod or -mod=readonly to ignore the vendor directory$' -# 'go mod vendor' should write a 1.14 vendor/modules.txt even if -# the go version is still 1.13. +# If the go version is still 1.13, 'go mod vendor' should write a +# matching vendor/modules.txt containing the corrected 1.13 data. go mod vendor -cmp $WORK/modules-1.14.txt vendor/modules.txt +cmp $WORK/modules-1.13.txt vendor/modules.txt go list -mod=vendor -f {{.Dir}} -tags tools all stdout '^'$WORK'[/\\]auto$' stdout '^'$WORK'[/\\]auto[/\\]vendor[/\\]example.com[/\\]printversion$' stdout '^'$WORK'[/\\]auto[/\\]vendor[/\\]example.com[/\\]version$' -# When the version is upgraded to 1.14, -mod=vendor should kick in -# automatically and succeed. +# When the version is upgraded to 1.14, 'go mod vendor' should write a +# vendor/modules.txt with the updated 1.14 annotations. go mod edit -go=1.14 +go mod vendor +cmp $WORK/modules-1.14.txt vendor/modules.txt + +# Then, -mod=vendor should kick in automatically and succeed. go list -f {{.Dir}} -tags tools all stdout '^'$WORK'[/\\]auto$' stdout '^'$WORK'[/\\]auto[/\\]vendor[/\\]example.com[/\\]printversion$' @@ -203,7 +208,7 @@ example.com/printversion example.com/version # example.com/unused => nonexistent.example.com/unused v1.0.0-whatever # example.com/version v1.2.0 => nonexistent.example.com/version v1.2.0 --- $WORK/auto/vendor/modules.txt -- +-- $WORK/modules-1.13.txt -- # example.com/printversion v1.0.0 example.com/printversion # example.com/version v1.0.0 => ./replacement-version