Skip to content

Commit c3365ad

Browse files
author
Bryan C. Mills
committed
cmd/go: annotate versions in vendor/modules.txt
In order to prevent edit wars with previous cmd/go releases, the new version annotations are only included if the main module specifies 'go 1.17' or higher. Fixes #36876 Change-Id: Iba15e47dd1ac2c16d754679a9b501db4069fa250 Reviewed-on: https://go-review.googlesource.com/c/go/+/315409 Trust: Bryan C. Mills <[email protected]> Run-TryBot: Bryan C. Mills <[email protected]> TryBot-Result: Go Bot <[email protected]> Reviewed-by: Michael Matloob <[email protected]>
1 parent 7dedc23 commit c3365ad

File tree

7 files changed

+154
-33
lines changed

7 files changed

+154
-33
lines changed

doc/go1.17.html

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,16 @@ <h4 id="missing-go-directive"><code>go.mod</code> files missing <code>go</code>
120120
files.)
121121
</p>
122122

123+
<h4 id="vendor-go-versions"><code>go</code> versions in <code>vendor/modules.txt</code></h4>
124+
125+
<p><!-- golang.org/issue/36876 -->
126+
If the main module specifies <code>go</code> <code>1.17</code> or higher,
127+
<code>go</code> <code>mod</code> <code>vendor</code> now annotates
128+
<code>vendor/modules.txt</code> with the <code>go</code> version indicated by
129+
each vendored module in its own <code>go.mod</code> file. The annotated
130+
version is used when building the module's packages from vendored source code.
131+
</p>
132+
123133
<h2 id="runtime">Runtime</h2>
124134

125135
<p>

src/cmd/go/internal/modcmd/vendor.go

Lines changed: 37 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -88,15 +88,23 @@ func runVendor(ctx context.Context, cmd *base.Command, args []string) {
8888
}
8989

9090
includeAllReplacements := false
91+
includeGoVersions := false
9192
isExplicit := map[module.Version]bool{}
92-
if gv := modload.ModFile().Go; gv != nil && semver.Compare("v"+gv.Version, "v1.14") >= 0 {
93-
// If the Go version is at least 1.14, annotate all explicit 'require' and
94-
// 'replace' targets found in the go.mod file so that we can perform a
95-
// stronger consistency check when -mod=vendor is set.
96-
for _, r := range modload.ModFile().Require {
97-
isExplicit[r.Mod] = true
93+
if gv := modload.ModFile().Go; gv != nil {
94+
if semver.Compare("v"+gv.Version, "v1.14") >= 0 {
95+
// If the Go version is at least 1.14, annotate all explicit 'require' and
96+
// 'replace' targets found in the go.mod file so that we can perform a
97+
// stronger consistency check when -mod=vendor is set.
98+
for _, r := range modload.ModFile().Require {
99+
isExplicit[r.Mod] = true
100+
}
101+
includeAllReplacements = true
102+
}
103+
if semver.Compare("v"+gv.Version, "v1.17") >= 0 {
104+
// If the Go version is at least 1.17, annotate all modules with their
105+
// 'go' version directives.
106+
includeGoVersions = true
98107
}
99-
includeAllReplacements = true
100108
}
101109

102110
var vendorMods []module.Version
@@ -110,26 +118,35 @@ func runVendor(ctx context.Context, cmd *base.Command, args []string) {
110118
}
111119
module.Sort(vendorMods)
112120

113-
var buf bytes.Buffer
121+
var (
122+
buf bytes.Buffer
123+
w io.Writer = &buf
124+
)
125+
if cfg.BuildV {
126+
w = io.MultiWriter(&buf, os.Stderr)
127+
}
128+
114129
for _, m := range vendorMods {
115130
line := moduleLine(m, modload.Replacement(m))
116-
buf.WriteString(line)
117-
if cfg.BuildV {
118-
os.Stderr.WriteString(line)
131+
io.WriteString(w, line)
132+
133+
goVersion := ""
134+
if includeGoVersions {
135+
goVersion = modload.ModuleInfo(ctx, m.Path).GoVersion
119136
}
120-
if isExplicit[m] {
121-
buf.WriteString("## explicit\n")
122-
if cfg.BuildV {
123-
os.Stderr.WriteString("## explicit\n")
124-
}
137+
switch {
138+
case isExplicit[m] && goVersion != "":
139+
fmt.Fprintf(w, "## explicit; go %s\n", goVersion)
140+
case isExplicit[m]:
141+
io.WriteString(w, "## explicit\n")
142+
case goVersion != "":
143+
fmt.Fprintf(w, "## go %s\n", goVersion)
125144
}
145+
126146
pkgs := modpkgs[m]
127147
sort.Strings(pkgs)
128148
for _, pkg := range pkgs {
129-
fmt.Fprintf(&buf, "%s\n", pkg)
130-
if cfg.BuildV {
131-
fmt.Fprintf(os.Stderr, "%s\n", pkg)
132-
}
149+
fmt.Fprintf(w, "%s\n", pkg)
133150
vendorPkg(vdir, pkg)
134151
}
135152
}

src/cmd/go/internal/modload/vendor.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ var (
3131
type vendorMetadata struct {
3232
Explicit bool
3333
Replacement module.Version
34+
GoVersion string
3435
}
3536

3637
// readVendorList reads the list of vendored modules from vendor/modules.txt.
@@ -104,6 +105,10 @@ func readVendorList() {
104105
if entry == "explicit" {
105106
meta.Explicit = true
106107
}
108+
if strings.HasPrefix(entry, "go ") {
109+
meta.GoVersion = strings.TrimPrefix(entry, "go ")
110+
rawGoVersion.Store(mod, meta.GoVersion)
111+
}
107112
// All other tokens are reserved for future use.
108113
}
109114
vendorMeta[mod] = meta

src/cmd/go/testdata/script/mod_go_version_vendor.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
[short] skip
66

7-
go mod init example.com/foo
87
go mod edit -replace=example.com/[email protected]=./use113
98

109
go mod vendor
@@ -20,7 +19,10 @@ go mod vendor
2019
! grep 1.13 vendor/modules.txt
2120
go build .
2221

22+
-- go.mod --
23+
module example.com/foo
2324

25+
go 1.16
2426
-- foo.go --
2527
package foo
2628

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
# https://golang.org/issue/36876: As of Go 1.17, vendor/modules.txt should
2+
# indicate the language version used by each dependency.
3+
4+
[short] skip
5+
6+
7+
# Control case: without a vendor directory, need117 builds and bad114 doesn't.
8+
9+
go build example.net/need117
10+
! go build example.net/bad114
11+
stderr '^bad114[/\\]bad114.go:15:2: duplicate method Y$'
12+
13+
14+
# With a vendor/modules.txt lacking language versions, the world is topsy-turvy.
15+
# Things that ought to build shouldn't, and things that shouldn't build do.
16+
17+
go mod vendor
18+
go build example.net/bad114
19+
! go build example.net/need117
20+
stderr '^vendor[/\\]example\.net[/\\]need117[/\\]need117.go:5:18: .*\n\tconversion of slices to array pointers only supported as of -lang=go1\.17'
21+
22+
23+
# Upgrading the main module to 1.17 adds version annotations.
24+
# Then everything is once again consistent with the non-vendored world.
25+
26+
go mod edit -go=1.17
27+
go mod vendor
28+
go build example.net/need117
29+
! go build example.net/bad114
30+
stderr '^vendor[/\\]example\.net[/\\]bad114[/\\]bad114.go:15:2: duplicate method Y$'
31+
32+
33+
-- go.mod --
34+
module example.net/m
35+
36+
go 1.16
37+
38+
require (
39+
example.net/bad114 v0.1.0
40+
example.net/need117 v0.1.0
41+
)
42+
43+
replace (
44+
example.net/bad114 v0.1.0 => ./bad114
45+
example.net/need117 v0.1.0 => ./need117
46+
)
47+
-- m.go --
48+
package m
49+
50+
import _ "example.net/bad114"
51+
import _ "example.net/need117"
52+
53+
-- bad114/go.mod --
54+
// Module bad114 requires Go 1.14 or higher, but declares Go 1.13.
55+
module example.net/bad114
56+
57+
go 1.13
58+
-- bad114/bad114.go --
59+
package bad114
60+
61+
type XY interface {
62+
X()
63+
Y()
64+
}
65+
66+
type YZ interface {
67+
Y()
68+
Z()
69+
}
70+
71+
type XYZ interface {
72+
XY
73+
YZ
74+
}
75+
76+
-- need117/go.mod --
77+
// Module need117 requires Go 1.17 or higher.
78+
module example.net/need117
79+
80+
go 1.17
81+
-- need117/need117.go --
82+
package need117
83+
84+
func init() {
85+
s := make([]byte, 4)
86+
_ = (*[4]byte)(s)
87+
}

src/cmd/vendor/modules.txt

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# github.com/google/pprof v0.0.0-20210226084205-cbba55b83ad5
2-
## explicit
2+
## explicit; go 1.14
33
github.com/google/pprof/driver
44
github.com/google/pprof/internal/binutils
55
github.com/google/pprof/internal/driver
@@ -19,17 +19,17 @@ github.com/google/pprof/third_party/svgpan
1919
## explicit
2020
github.com/ianlancetaylor/demangle
2121
# golang.org/x/arch v0.0.0-20210308155006-05f8f0431f72
22-
## explicit
22+
## explicit; go 1.11
2323
golang.org/x/arch/arm/armasm
2424
golang.org/x/arch/arm64/arm64asm
2525
golang.org/x/arch/ppc64/ppc64asm
2626
golang.org/x/arch/x86/x86asm
2727
# golang.org/x/crypto v0.0.0-20210220033148-5ea612d1eb83
28-
## explicit
28+
## explicit; go 1.11
2929
golang.org/x/crypto/ed25519
3030
golang.org/x/crypto/ed25519/internal/edwards25519
3131
# golang.org/x/mod v0.4.3-0.20210409134425-858fdbee9c24
32-
## explicit
32+
## explicit; go 1.12
3333
golang.org/x/mod/internal/lazyregexp
3434
golang.org/x/mod/modfile
3535
golang.org/x/mod/module
@@ -40,16 +40,16 @@ golang.org/x/mod/sumdb/note
4040
golang.org/x/mod/sumdb/tlog
4141
golang.org/x/mod/zip
4242
# golang.org/x/sys v0.0.0-20210403161142-5e06dd20ab57
43-
## explicit
43+
## explicit; go 1.12
4444
golang.org/x/sys/internal/unsafeheader
4545
golang.org/x/sys/plan9
4646
golang.org/x/sys/unix
4747
golang.org/x/sys/windows
4848
# golang.org/x/term v0.0.0-20210220032956-6a3ed077a48d
49-
## explicit
49+
## explicit; go 1.11
5050
golang.org/x/term
5151
# golang.org/x/tools v0.1.1-0.20210422170518-f946a157eefe
52-
## explicit
52+
## explicit; go 1.12
5353
golang.org/x/tools/cover
5454
golang.org/x/tools/go/analysis
5555
golang.org/x/tools/go/analysis/internal/analysisflags
@@ -93,6 +93,6 @@ golang.org/x/tools/go/types/typeutil
9393
golang.org/x/tools/internal/analysisinternal
9494
golang.org/x/tools/internal/lsp/fuzzy
9595
# golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1
96-
## explicit
96+
## explicit; go 1.11
9797
golang.org/x/xerrors
9898
golang.org/x/xerrors/internal

src/vendor/modules.txt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# golang.org/x/crypto v0.0.0-20210220033148-5ea612d1eb83
2-
## explicit
2+
## explicit; go 1.11
33
golang.org/x/crypto/chacha20
44
golang.org/x/crypto/chacha20poly1305
55
golang.org/x/crypto/cryptobyte
@@ -9,7 +9,7 @@ golang.org/x/crypto/hkdf
99
golang.org/x/crypto/internal/subtle
1010
golang.org/x/crypto/poly1305
1111
# golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4
12-
## explicit
12+
## explicit; go 1.11
1313
golang.org/x/net/dns/dnsmessage
1414
golang.org/x/net/http/httpguts
1515
golang.org/x/net/http/httpproxy
@@ -19,10 +19,10 @@ golang.org/x/net/lif
1919
golang.org/x/net/nettest
2020
golang.org/x/net/route
2121
# golang.org/x/sys v0.0.0-20210403161142-5e06dd20ab57
22-
## explicit
22+
## explicit; go 1.12
2323
golang.org/x/sys/cpu
2424
# golang.org/x/text v0.3.6-0.20210227105805-e3aa4adf54f6
25-
## explicit
25+
## explicit; go 1.11
2626
golang.org/x/text/secure/bidirule
2727
golang.org/x/text/transform
2828
golang.org/x/text/unicode/bidi

0 commit comments

Comments
 (0)