]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/go: fix go.mod corruption using -mod=vendor
authorRuss Cox <rsc@golang.org>
Thu, 9 Aug 2018 20:16:43 +0000 (16:16 -0400)
committerRuss Cox <rsc@golang.org>
Fri, 10 Aug 2018 00:47:20 +0000 (00:47 +0000)
If we're using -mod=vendor then we effectively load
a fake build list from vendor/modules.txt.
Do not write it back to go.mod.

Fixes #26704.

Change-Id: Ie79f2103dc16d0b7fe0c884e77ba726c7e04f2e4
Reviewed-on: https://go-review.googlesource.com/128899
Reviewed-by: Bryan C. Mills <bcmills@google.com>
src/cmd/go/internal/modload/init.go
src/cmd/go/testdata/script/mod_vendor_build.txt [new file with mode: 0644]

index 8ce0c32fd5f67922c84629729378e8d2a5246e2e..5e9db0f9ea3d1125765017bb874098b5a64821c1 100644 (file)
@@ -521,7 +521,10 @@ func MinReqs() mvs.Reqs {
 
 // WriteGoMod writes the current build list back to go.mod.
 func WriteGoMod() {
-       if !allowWriteGoMod {
+       // If we're using -mod=vendor we basically ignored
+       // go.mod, so definitely don't try to write back our
+       // incomplete view of the world.
+       if !allowWriteGoMod || cfg.BuildMod == "vendor" {
                return
        }
 
diff --git a/src/cmd/go/testdata/script/mod_vendor_build.txt b/src/cmd/go/testdata/script/mod_vendor_build.txt
new file mode 100644 (file)
index 0000000..7b304db
--- /dev/null
@@ -0,0 +1,27 @@
+env GO111MODULE=on
+
+# initial conditions: using sampler v1.3.0, not listed in go.mod.
+go list -deps
+stdout rsc.io/sampler
+! grep 'rsc.io/sampler v1.3.0' go.mod
+
+# update to v1.3.1, now indirect in go.mod.
+go get rsc.io/sampler@v1.3.1
+grep 'rsc.io/sampler v1.3.1 // indirect' go.mod
+cp go.mod go.mod.good
+
+# vendoring can but should not need to make changes.
+go mod vendor
+cmp go.mod go.mod.good
+
+# go list -mod=vendor (or go build -mod=vendor) must not modify go.mod.
+# golang.org/issue/26704
+go list -mod=vendor
+cmp go.mod go.mod.good
+
+-- go.mod --
+module m
+
+-- x.go --
+package x
+import _ "rsc.io/quote"