golang / go

The Go programming language
https://go.dev
BSD 3-Clause "New" or "Revised" License
122.68k stars 17.49k forks source link

cmd/go: go mod -replace without old version creates invalid go.mod #26373

Closed myitcv closed 6 years ago

myitcv commented 6 years ago

Please answer these questions before submitting your issue. Thanks!

What version of Go are you using (go version)?

go version devel +8a330454dc Fri Jul 13 03:53:00 2018 +0000 linux/amd64

Does this issue reproduce with the latest release?

No, working with tip.

What operating system and processor architecture are you using (go env)?

GOARCH="amd64"
GOBIN=""
GOCACHE="/home/myitcv/.cache/go-build"
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH="/tmp/tmp.KCiNC02W22"
GOPROXY=""
GORACE=""
GOROOT="/home/myitcv/gos"
GOTMPDIR=""
GOTOOLDIR="/home/myitcv/gos/pkg/tool/linux_amd64"
GCCGO="gccgo"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD="/tmp/tmp.KCiNC02W22/hello/go.mod"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build520682053=/tmp/go-build -gno-record-gcc-switches"

What did you do?

cd `mktemp -d`
export GOPATH=$PWD
mkdir hello
cd hello
go mod -init -module example.com/hello
go mod -require github.com/gopherjs/gopherjs@master
go mod -replace github.com/gopherjs/gopherjs=github.com/myitcv/gopherjs@vgo
cat go.mod
go mod -json

What did you expect to see?

The JSON representation of the go.mod file

What did you see instead?

go: errors parsing go.mod:
/tmp/tmp.KCiNC02W22/hello/go.mod:5: invalid module version ithub.com/myitcv/gopherjs: unrecognized import path "ithub.com/myitcv/gopherjs" (https fetch: Get https://ithub.com/myitcv/gopherjs?go-get=1: x509: certificate has expired or is not yet valid)

because the replace directive has incorrectly written the go.mod file:

$ cat go.mod
module example.com/hello

require github.com/gopherjs/gopherjs v0.0.0-20180628210949-0892b62f0d9f

replace github.com/gopherjs/gopherjs => ithub.com/myitcv/gopherjs vgo

Notice the replace target ithub.com/myitcv/gopherjs

/cc @rsc @bcmills

bcmills commented 6 years ago

Off-by-one error in https://golang.org/cl/122404. Fix goes here: https://github.com/golang/go/blob/28502b5023b6ddbecf24c68f225762054882ebf5/src/cmd/go/internal/modcmd/mod.go#L403

We should add a test along with the fix. 🙂

gopherbot commented 6 years ago

Change https://golang.org/cl/123778 mentions this issue: cmd/go: fix module replace flag parsed bug