Skip to content

Commit eb71887

Browse files
author
Bryan C. Mills
committed
cmd/go: prune go.mod and go.sum files from vendored dependencies
Fixes #42970 Change-Id: I79246ef7fc16ae05c8e7b40ffb239a61f6415447 Reviewed-on: https://go-review.googlesource.com/c/go/+/315410 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 c3365ad commit eb71887

File tree

6 files changed

+56
-11
lines changed

6 files changed

+56
-11
lines changed

doc/go1.17.html

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ <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>
123+
<h4 id="vendor"><code>vendor</code> contents</h4>
124124

125125
<p><!-- golang.org/issue/36876 -->
126126
If the main module specifies <code>go</code> <code>1.17</code> or higher,
@@ -130,6 +130,14 @@ <h4 id="vendor-go-versions"><code>go</code> versions in <code>vendor/modules.txt
130130
version is used when building the module's packages from vendored source code.
131131
</p>
132132

133+
<p><!-- golang.org/issue/42970 -->
134+
If the main module specifies <code>go</code> <code>1.17</code> or higher,
135+
<code>go</code> <code>mod</code> <code>vendor</code> now omits <code>go.mod</code>
136+
and <code>go.sum</code> files for vendored dependencies, which can otherwise
137+
interfere with the ability of the <code>go</code> command to identify the correct
138+
module root when invoked within the <code>vendor</code> tree.
139+
</p>
140+
133141
<h2 id="runtime">Runtime</h2>
134142

135143
<p>

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,15 @@ func matchPotentialSourceFile(dir string, info fs.DirEntry) bool {
340340
if strings.HasSuffix(info.Name(), "_test.go") {
341341
return false
342342
}
343+
if info.Name() == "go.mod" || info.Name() == "go.sum" {
344+
if gv := modload.ModFile().Go; gv != nil && semver.Compare("v"+gv.Version, "v1.17") >= 0 {
345+
// As of Go 1.17, we strip go.mod and go.sum files from dependency modules.
346+
// Otherwise, 'go' commands invoked within the vendor subtree may misidentify
347+
// an arbitrary directory within the vendor tree as a module root.
348+
// (See https://golang.org/issue/42970.)
349+
return false
350+
}
351+
}
343352
if strings.HasSuffix(info.Name(), ".go") {
344353
f, err := fsys.Open(filepath.Join(dir, info.Name()))
345354
if err != nil {
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# https://golang.org/issue/42970: As of Go 1.17, go.mod and go.sum files should
2+
# be stripped from vendored dependencies.
3+
4+
go mod vendor
5+
cd vendor/example.net/x
6+
go list all
7+
! stdout '^example.net/m'
8+
stdout '^example.net/x$'
9+
exists ./go.sum
10+
11+
cd ../../..
12+
go mod edit -go=1.17
13+
go mod vendor
14+
cd vendor/example.net/x
15+
go list all
16+
stdout '^example.net/m$'
17+
stdout '^example.net/x$'
18+
! exists ./go.sum
19+
20+
-- go.mod --
21+
module example.net/m
22+
23+
go 1.16
24+
25+
require example.net/x v0.1.0
26+
27+
replace example.net/x v0.1.0 => ./x
28+
-- m.go --
29+
package m
30+
31+
import _ "example.net/x"
32+
-- x/go.mod --
33+
module example.net/x
34+
35+
go 1.16
36+
-- x/go.sum --
37+
-- x/x.go --
38+
package x

src/cmd/vendor/golang.org/x/term/go.mod

Lines changed: 0 additions & 5 deletions
This file was deleted.

src/cmd/vendor/golang.org/x/term/go.sum

Lines changed: 0 additions & 2 deletions
This file was deleted.

src/cmd/vendor/golang.org/x/xerrors/go.mod

Lines changed: 0 additions & 3 deletions
This file was deleted.

0 commit comments

Comments
 (0)