golang / go

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

go mod edit: changing go versions in modules to invalid versions updates go.mod and causes weird problems #66491

Closed kristoferfannar closed 6 months ago

kristoferfannar commented 6 months ago

Go version

go version go1.22.0 darwin/arm64

Output of go env in your module/workspace:

GO111MODULE=''
GOARCH='arm64'
GOBIN=''
GOCACHE='/Users/kristoferfannar/Library/Caches/go-build'
GOENV='/Users/kristoferfannar/Library/Application Support/go/env'
GOEXE=''
GOEXPERIMENT=''
GOFLAGS=''
GOHOSTARCH='arm64'
GOHOSTOS='darwin'
GOINSECURE=''
GOMODCACHE='/Users/kristoferfannar/go/pkg/mod'
GONOPROXY=''
GONOSUMDB=''
GOOS='darwin'
GOPATH='/Users/kristoferfannar/go'
GOPRIVATE=''
GOPROXY='https://proxy.golang.org,direct'
GOROOT='/opt/homebrew/Cellar/go/1.22.0/libexec'
GOSUMDB='sum.golang.org'
GOTMPDIR=''
GOTOOLCHAIN='auto'
GOTOOLDIR='/opt/homebrew/Cellar/go/1.22.0/libexec/pkg/tool/darwin_arm64'
GOVCS=''
GOVERSION='go1.22.0'
GCCGO='gccgo'
AR='ar'
CC='cc'
CXX='c++'
CGO_ENABLED='1'
GOMOD='/Users/kristoferfannar/Desktop/projects/leetcode/problems/72. Edit Distance/golang/go.mod'
GOWORK=''
CGO_CFLAGS='-O2 -g'
CGO_CPPFLAGS=''
CGO_CXXFLAGS='-O2 -g'
CGO_FFLAGS='-O2 -g'
CGO_LDFLAGS='-O2 -g'
PKG_CONFIG='pkg-config'
GOGCCFLAGS='-fPIC -arch arm64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -ffile-prefix-map=/var/folders/nm/58kw_yd559x9p63n3v_88wk80000gn/T/go-build1557319019=/tmp/go-build -gno-record-gcc-switches -fno-common'

What did you do?

I have a super simple go project (Edit Distance algorithm, ca 100 lines in one main.go) using go.mod with version 1.21. I don't think my code is in any way special or needed for a reproduction.

// go.mod
go 1.21

Anyways, I'm updating my project version 1.22 using go mod edit.

What did you see happen?

I type go mod edit -go=1.22 and it looks like everything works.

// go.mod
go 1.22

To verify that it actually works, I try go mod edit -go=1.30 (hoping for an error), and see that go.mod has updated accordingly:

// go.mod
go 1.30

Now, I want to go back to version 1.22. I execute go mod edit -go=1.22, but get this error message.

> go mod edit -go=1.22
go: downloading go1.30 (darwin/arm64)
go: download go1.30 for darwin/arm64: toolchain not available

It now seems to download go version 1.30, exactly when I want to change back to 1.22. What's worse, is that my go.mod hasn't updated, still at go 1.30.

No matter what I try, I'm stuck at this version.

❯ go mod edit -go=1.21
go: downloading go1.30 (darwin/arm64)
go: download go1.30 for darwin/arm64: toolchain not available
❯ go mod edit -go=1.23
go: downloading go1.30 (darwin/arm64)
go: download go1.30 for darwin/arm64: toolchain not available

Finally, when I now run go version, I don't get any output. Rather, it retries downloading the first invalid version, 1.30.

❯ go version
go: downloading go1.30 (darwin/arm64)
go: download go1.30 for darwin/arm64: toolchain not available

Only when I manually change the version in go.mod, does everything go back to normal.

What did you expect to see?

When updating to go mod edit -go=1.30. I'd expect the error I got later on when switching back to v 1.22.

go: downloading go1.30 (darwin/arm64)
go: download go1.30 for darwin/arm64: toolchain not available

As a result, I would expect the go version in my go.mod file not to change to the invalid go version, rather remaining at its prior version.

seankhliao commented 6 months ago

Unfortunate, but 1.30 is a valid version number, just not one you can use now, and the forward compatibility go aims for prevents it from modifying newer config it potentially doesn't understand.

kristoferfannar commented 6 months ago

That's valid. Especially when something like go mod edit -go=0.1 gives the appropriate error.

However, there's something not nice about being able to set a go version to any float above 1.0, such as go mod edit -go=999.999.999, and have go.mod change to that version.