]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/go/internal/list: disallow 'list -m' with '-mod=vendor'
authorBryan C. Mills <bcmills@google.com>
Tue, 8 Oct 2019 18:51:30 +0000 (14:51 -0400)
committerBryan C. Mills <bcmills@google.com>
Wed, 9 Oct 2019 18:39:22 +0000 (18:39 +0000)
Updates #33848

Change-Id: I81663386297282397ce1b546a8b15597bfbcea78
Reviewed-on: https://go-review.googlesource.com/c/go/+/199821
Run-TryBot: Bryan C. Mills <bcmills@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Jay Conrod <jayconrod@google.com>
doc/go1.14.html
src/cmd/go/internal/list/list.go
src/cmd/go/internal/modload/build.go
src/cmd/go/testdata/script/mod_getmode_vendor.txt
src/cmd/go/testdata/script/mod_vendor.txt
src/cmd/go/testdata/script/mod_vendor_auto.txt
src/cmd/go/testdata/script/mod_vendor_replace.txt

index 361684358be5b0bd9e4ef0a89d2f949f39e385cf..eedc53226b5cfe67e26efc55563442f993c25cf7 100644 (file)
@@ -69,6 +69,13 @@ TODO
   <code>go.mod</code> file.
 </p>
 
+<p>
+  <code>go</code> <code>list</code> <code>-m</code> no longer silently omits
+  transitive dependencies that do not provide packages in
+  the <code>vendor</code> directory. It now fails explicitly if
+  <code>-mod=vendor</code> is set.
+</p>
+
 <p><!-- golang.org/issue/32502, golang.org/issue/30345 -->
   The <code>go</code> <code>get</code> command no longer accepts
   the <code>-mod</code> flag. Previously, the flag's setting either
index 67819939e60020bde48f4a59e8b49c070d105afa..d8c75776bbac741703bc8c4c1f61ce62b4a1eab1 100644 (file)
@@ -384,6 +384,9 @@ func runList(cmd *base.Command, args []string) {
                if modload.Init(); !modload.Enabled() {
                        base.Fatalf("go list -m: not using modules")
                }
+               if cfg.BuildMod == "vendor" {
+                       base.Fatalf("go list -m: can't list modules with -mod=vendor\n\tuse -mod=mod or -mod=readonly to ignore the vendor directory")
+               }
                modload.LoadBuildList()
 
                mods := modload.ListModules(args, *listU, *listVersions)
index 1309d1ad6317be73ce7935546d7730cc1e95c4ec..4105c47ba7611ed364706f05b7961b93d6db2f05 100644 (file)
@@ -120,7 +120,8 @@ func moduleInfo(m module.Version, fromBuildList bool) *modinfo.ModulePublic {
        }
 
        if cfg.BuildMod == "vendor" {
-               info.Dir = filepath.Join(ModRoot(), "vendor", m.Path)
+               // The vendor directory doesn't contain enough information to reconstruct
+               // anything more about the module.
                return info
        }
 
index c532cee9cbd523b1c3ed93fcdfa2819fd91796c7..21fec5b85f97a90052381ecf9ee7434b20767801 100644 (file)
@@ -6,12 +6,12 @@ env GOPATH=$WORK/empty
 env GOPROXY=file:///nonexist
 
 go list -mod=vendor
-go list -mod=vendor -m -f '{{.Path}} {{.Version}} {{.Dir}}' all
+go list -mod=vendor -f '{{with .Module}}{{.Path}} {{.Version}}{{end}} {{.Dir}}' all
 stdout '^rsc.io/quote v1.5.1 .*vendor[\\/]rsc.io[\\/]quote$'
-stdout '^golang.org/x/text v0.0.0.* .*vendor[\\/]golang.org[\\/]x[\\/]text$'
+stdout '^golang.org/x/text v0.0.0.* .*vendor[\\/]golang.org[\\/]x[\\/]text[\\/]language$'
 
 ! go list -mod=vendor -m rsc.io/quote@latest
-stderr 'module lookup disabled by -mod=vendor'
+stderr 'go list -m: can''t list modules with -mod=vendor\n\tuse -mod=mod or -mod=readonly to ignore the vendor directory'
 ! go get -mod=vendor -u
 stderr 'flag provided but not defined: -mod'
 
index 991a6d1926ff4bd47e31100ea0cd678a2247c796..9b716906e5915e94e4704531fde6af681cef268d 100644 (file)
@@ -1,11 +1,15 @@
 env GO111MODULE=on
 
+# Without vendoring, a build should succeed unless -mod=vendor is set.
 [!short] go build
 [!short] ! go build -mod=vendor
 
+# Without vendoring, 'go list' should report the replacement directory for
+# a package in a replaced module.
 go list -f {{.Dir}} x
 stdout 'src[\\/]x'
 
+# 'go mod vendor' should copy all replaced modules to the vendor directory.
 go mod vendor -v
 stderr '^# x v1.0.0 => ./x'
 stderr '^x'
@@ -16,28 +20,38 @@ stderr '^z'
 ! stderr '^w'
 grep 'a/foo/bar/b\na/foo/bar/c' vendor/modules.txt # must be sorted
 
+# An explicit '-mod=mod' should ignore the vendor directory.
 go list -mod=mod -f {{.Dir}} x
 stdout 'src[\\/]x'
 
 go list -mod=mod -f {{.Dir}} -m x
 stdout 'src[\\/]x'
 
+# An explicit '-mod=vendor' should report package directories within
+# the vendor directory.
 go list -mod=vendor -f {{.Dir}} x
 stdout 'src[\\/]vendor[\\/]x'
 
-go list -mod=vendor -f {{.Dir}} -m x
-stdout 'src[\\/]vendor[\\/]x'
+# 'go list -mod=vendor -m' does not have enough information to list modules
+# accurately, and should fail.
+! go list -mod=vendor -f {{.Dir}} -m x
+stderr 'can''t list modules with -mod=vendor\n\tuse -mod=mod or -mod=readonly to ignore the vendor directory'
 
-go list -mod=mod -f {{.Dir}} -m w
+# 'go list -mod=mod' should report packages outside the import graph,
+# but 'go list -mod=vendor' should error out for them.
+go list -mod=mod -f {{.Dir}} w
 stdout 'src[\\/]w'
 
 ! go list -mod=vendor -f {{.Dir}} w
 stderr 'src[\\/]vendor[\\/]w'
 
+# Test dependencies should not be copied.
 ! exists vendor/x/testdata
 ! exists vendor/a/foo/bar/b/ignored.go
 ! exists vendor/a/foo/bar/b/main_test.go
 
+# Licenses and other metadata for each module should be copied
+# if any package within their module is copied.
 exists vendor/a/foo/AUTHORS.txt
 exists vendor/a/foo/CONTRIBUTORS
 exists vendor/a/foo/LICENSE
@@ -55,7 +69,8 @@ exists vendor/mysite/myname/mypkg/LICENSE.txt
 
 [short] stop
 
-go build
+# 'go build' and 'go test' using vendored packages should succeed.
+go build -mod=mod
 go build -mod=vendor
 go test -mod=vendor . ./subdir
 go test -mod=vendor ./...
index 44f9fd4d38ee493a9acf1c6aaba20e8faae6c9ec..c80aa6ad63030a969efb8811b47411b6f1e3b594 100644 (file)
@@ -15,15 +15,11 @@ stdout '^'$WORK'[/\\]auto$'
 stdout '^'$WORK'[/\\]auto[/\\]vendor[/\\]example.com[/\\]printversion$'
 stdout '^'$WORK'[/\\]auto[/\\]vendor[/\\]example.com[/\\]version$'
 
-go list -m all
-stdout '^example.com/auto$'
-stdout 'example.com/printversion v1.0.0'
-stdout 'example.com/version v1.0.0'
+! go list -m all
+stderr 'can''t list modules with -mod=vendor'
 
-go list -m -f '{{.Dir}}' all
-stdout '^'$WORK'[/\\]auto$'
-stdout '^'$WORK'[/\\]auto[/\\]vendor[/\\]example.com[/\\]printversion$'
-stdout '^'$WORK'[/\\]auto[/\\]vendor[/\\]example.com[/\\]version$'
+! go list -m -f '{{.Dir}}' all
+stderr 'can''t list modules with -mod=vendor'
 
 # An explicit -mod=mod should force the vendor directory to be ignored.
 env GOFLAGS=-mod=mod
index 6bc1c77ed3d0d76e8d52242dd253370af0092034..a251daa6c1450660853680c12bdc4e278e437f12 100644 (file)
@@ -1,7 +1,7 @@
 env GO111MODULE=on
 
 # Before vendoring, we expect to see the original directory.
-go list -f '{{.Version}} {{.Dir}}' -m rsc.io/quote/v3
+go list -f '{{with .Module}}{{.Version}}{{end}} {{.Dir}}' rsc.io/quote/v3
 stdout 'v3.0.0'
 stdout '.*[/\\]not-rsc.io[/\\]quote[/\\]v3'
 
@@ -15,7 +15,7 @@ go mod vendor
 # without attempting to look up the non-replaced version.
 cmp vendor/rsc.io/quote/v3/quote.go local/not-rsc.io/quote/v3/quote.go
 
-go list -mod=vendor -f '{{.Version}} {{.Dir}}' -m rsc.io/quote/v3
+go list -mod=vendor -f '{{with .Module}}{{.Version}}{{end}} {{.Dir}}' rsc.io/quote/v3
 stdout 'v3.0.0'
 stdout '.*[/\\]vendor[/\\]rsc.io[/\\]quote[/\\]v3'
 ! stderr 'finding'