Skip to content

Commit 6cba4db

Browse files
author
Bryan C. Mills
committed
cmd/go/internal/list: disallow 'list -m' with '-mod=vendor'
Updates #33848 Change-Id: I81663386297282397ce1b546a8b15597bfbcea78 Reviewed-on: https://go-review.googlesource.com/c/go/+/199821 Run-TryBot: Bryan C. Mills <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Jay Conrod <[email protected]>
1 parent 1736f3a commit 6cba4db

File tree

7 files changed

+40
-18
lines changed

7 files changed

+40
-18
lines changed

doc/go1.14.html

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,13 @@ <h3 id="go-command">Go command</h3>
6969
<code>go.mod</code> file.
7070
</p>
7171

72+
<p>
73+
<code>go</code> <code>list</code> <code>-m</code> no longer silently omits
74+
transitive dependencies that do not provide packages in
75+
the <code>vendor</code> directory. It now fails explicitly if
76+
<code>-mod=vendor</code> is set.
77+
</p>
78+
7279
<p><!-- golang.org/issue/32502, golang.org/issue/30345 -->
7380
The <code>go</code> <code>get</code> command no longer accepts
7481
the <code>-mod</code> flag. Previously, the flag's setting either

src/cmd/go/internal/list/list.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -384,6 +384,9 @@ func runList(cmd *base.Command, args []string) {
384384
if modload.Init(); !modload.Enabled() {
385385
base.Fatalf("go list -m: not using modules")
386386
}
387+
if cfg.BuildMod == "vendor" {
388+
base.Fatalf("go list -m: can't list modules with -mod=vendor\n\tuse -mod=mod or -mod=readonly to ignore the vendor directory")
389+
}
387390
modload.LoadBuildList()
388391

389392
mods := modload.ListModules(args, *listU, *listVersions)

src/cmd/go/internal/modload/build.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,8 @@ func moduleInfo(m module.Version, fromBuildList bool) *modinfo.ModulePublic {
120120
}
121121

122122
if cfg.BuildMod == "vendor" {
123-
info.Dir = filepath.Join(ModRoot(), "vendor", m.Path)
123+
// The vendor directory doesn't contain enough information to reconstruct
124+
// anything more about the module.
124125
return info
125126
}
126127

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@ env GOPATH=$WORK/empty
66
env GOPROXY=file:///nonexist
77

88
go list -mod=vendor
9-
go list -mod=vendor -m -f '{{.Path}} {{.Version}} {{.Dir}}' all
9+
go list -mod=vendor -f '{{with .Module}}{{.Path}} {{.Version}}{{end}} {{.Dir}}' all
1010
stdout '^rsc.io/quote v1.5.1 .*vendor[\\/]rsc.io[\\/]quote$'
11-
stdout '^golang.org/x/text v0.0.0.* .*vendor[\\/]golang.org[\\/]x[\\/]text$'
11+
stdout '^golang.org/x/text v0.0.0.* .*vendor[\\/]golang.org[\\/]x[\\/]text[\\/]language$'
1212

1313
! go list -mod=vendor -m rsc.io/quote@latest
14-
stderr 'module lookup disabled by -mod=vendor'
14+
stderr 'go list -m: can''t list modules with -mod=vendor\n\tuse -mod=mod or -mod=readonly to ignore the vendor directory'
1515
! go get -mod=vendor -u
1616
stderr 'flag provided but not defined: -mod'
1717

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

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
env GO111MODULE=on
22

3+
# Without vendoring, a build should succeed unless -mod=vendor is set.
34
[!short] go build
45
[!short] ! go build -mod=vendor
56

7+
# Without vendoring, 'go list' should report the replacement directory for
8+
# a package in a replaced module.
69
go list -f {{.Dir}} x
710
stdout 'src[\\/]x'
811

12+
# 'go mod vendor' should copy all replaced modules to the vendor directory.
913
go mod vendor -v
1014
stderr '^# x v1.0.0 => ./x'
1115
stderr '^x'
@@ -16,28 +20,38 @@ stderr '^z'
1620
! stderr '^w'
1721
grep 'a/foo/bar/b\na/foo/bar/c' vendor/modules.txt # must be sorted
1822

23+
# An explicit '-mod=mod' should ignore the vendor directory.
1924
go list -mod=mod -f {{.Dir}} x
2025
stdout 'src[\\/]x'
2126

2227
go list -mod=mod -f {{.Dir}} -m x
2328
stdout 'src[\\/]x'
2429

30+
# An explicit '-mod=vendor' should report package directories within
31+
# the vendor directory.
2532
go list -mod=vendor -f {{.Dir}} x
2633
stdout 'src[\\/]vendor[\\/]x'
2734

28-
go list -mod=vendor -f {{.Dir}} -m x
29-
stdout 'src[\\/]vendor[\\/]x'
35+
# 'go list -mod=vendor -m' does not have enough information to list modules
36+
# accurately, and should fail.
37+
! go list -mod=vendor -f {{.Dir}} -m x
38+
stderr 'can''t list modules with -mod=vendor\n\tuse -mod=mod or -mod=readonly to ignore the vendor directory'
3039

31-
go list -mod=mod -f {{.Dir}} -m w
40+
# 'go list -mod=mod' should report packages outside the import graph,
41+
# but 'go list -mod=vendor' should error out for them.
42+
go list -mod=mod -f {{.Dir}} w
3243
stdout 'src[\\/]w'
3344

3445
! go list -mod=vendor -f {{.Dir}} w
3546
stderr 'src[\\/]vendor[\\/]w'
3647

48+
# Test dependencies should not be copied.
3749
! exists vendor/x/testdata
3850
! exists vendor/a/foo/bar/b/ignored.go
3951
! exists vendor/a/foo/bar/b/main_test.go
4052

53+
# Licenses and other metadata for each module should be copied
54+
# if any package within their module is copied.
4155
exists vendor/a/foo/AUTHORS.txt
4256
exists vendor/a/foo/CONTRIBUTORS
4357
exists vendor/a/foo/LICENSE
@@ -55,7 +69,8 @@ exists vendor/mysite/myname/mypkg/LICENSE.txt
5569

5670
[short] stop
5771

58-
go build
72+
# 'go build' and 'go test' using vendored packages should succeed.
73+
go build -mod=mod
5974
go build -mod=vendor
6075
go test -mod=vendor . ./subdir
6176
go test -mod=vendor ./...

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

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,11 @@ stdout '^'$WORK'[/\\]auto$'
1515
stdout '^'$WORK'[/\\]auto[/\\]vendor[/\\]example.com[/\\]printversion$'
1616
stdout '^'$WORK'[/\\]auto[/\\]vendor[/\\]example.com[/\\]version$'
1717

18-
go list -m all
19-
stdout '^example.com/auto$'
20-
stdout 'example.com/printversion v1.0.0'
21-
stdout 'example.com/version v1.0.0'
18+
! go list -m all
19+
stderr 'can''t list modules with -mod=vendor'
2220

23-
go list -m -f '{{.Dir}}' all
24-
stdout '^'$WORK'[/\\]auto$'
25-
stdout '^'$WORK'[/\\]auto[/\\]vendor[/\\]example.com[/\\]printversion$'
26-
stdout '^'$WORK'[/\\]auto[/\\]vendor[/\\]example.com[/\\]version$'
21+
! go list -m -f '{{.Dir}}' all
22+
stderr 'can''t list modules with -mod=vendor'
2723

2824
# An explicit -mod=mod should force the vendor directory to be ignored.
2925
env GOFLAGS=-mod=mod

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
env GO111MODULE=on
22

33
# Before vendoring, we expect to see the original directory.
4-
go list -f '{{.Version}} {{.Dir}}' -m rsc.io/quote/v3
4+
go list -f '{{with .Module}}{{.Version}}{{end}} {{.Dir}}' rsc.io/quote/v3
55
stdout 'v3.0.0'
66
stdout '.*[/\\]not-rsc.io[/\\]quote[/\\]v3'
77

@@ -15,7 +15,7 @@ go mod vendor
1515
# without attempting to look up the non-replaced version.
1616
cmp vendor/rsc.io/quote/v3/quote.go local/not-rsc.io/quote/v3/quote.go
1717

18-
go list -mod=vendor -f '{{.Version}} {{.Dir}}' -m rsc.io/quote/v3
18+
go list -mod=vendor -f '{{with .Module}}{{.Version}}{{end}} {{.Dir}}' rsc.io/quote/v3
1919
stdout 'v3.0.0'
2020
stdout '.*[/\\]vendor[/\\]rsc.io[/\\]quote[/\\]v3'
2121
! stderr 'finding'

0 commit comments

Comments
 (0)