1
0
mirror of https://github.com/golang/go synced 2024-11-05 15:06:09 -07:00

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 <bcmills@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Jay Conrod <jayconrod@google.com>
This commit is contained in:
Bryan C. Mills 2019-10-15 13:39:13 -04:00
parent 47759fbab7
commit e7394e178d
2 changed files with 36 additions and 21 deletions

View File

@ -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)
}
}
}

View File

@ -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