cmd/go: fix go.mod corruption using -mod=vendor

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>
This commit is contained in:
Russ Cox 2018-08-09 16:16:43 -04:00
parent 7aa9855704
commit cb7d0efc23
2 changed files with 31 additions and 1 deletions

View 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
}

View file

@ -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"