Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

x/tools/gopls: issues errors for range-over-func (InvalidRangeExpr) in go1.23rc1 projects #68248

Closed
matta opened this issue Jun 29, 2024 · 8 comments
Labels
gopls Issues related to the Go language server, gopls. Tools This label describes issues relating to any tools in the x/tools repository.
Milestone

Comments

@matta
Copy link

matta commented Jun 29, 2024

Go version

go version go1.23rc1 linux/amd64

Output of go env in your module/workspace:

GO111MODULE='on'
GOARCH='amd64'
GOBIN=''
GOCACHE='/home/matt/.cache/go-build'
GOENV='/home/matt/.config/go/env'
GOEXE=''
GOEXPERIMENT=''
GOFLAGS=''
GOHOSTARCH='amd64'
GOHOSTOS='linux'
GOINSECURE=''
GOMODCACHE='/home/matt/go/pkg/mod'
GONOPROXY=''
GONOSUMDB=''
GOOS='linux'
GOPATH='/home/matt/go'
GOPRIVATE=''
GOPROXY='https://proxy.golang.org,direct'
GOROOT='/home/matt/sdk/go1.23rc1'
GOSUMDB='sum.golang.org'
GOTMPDIR=''
GOTOOLCHAIN='auto'
GOTOOLDIR='/home/matt/sdk/go1.23rc1/pkg/tool/linux_amd64'
GOVCS=''
GOVERSION='go1.23rc1'
GODEBUG=''
GOTELEMETRY='on'
GOTELEMETRYDIR='/home/matt/.config/go/telemetry'
GCCGO='gccgo'
GOAMD64='v1'
AR='ar'
CC='gcc'
CXX='g++'
CGO_ENABLED='1'
GOMOD='/home/matt/scratch/maps-all/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 -m64 -pthread -Wl,--no-gc-sections -fmessage-length=0 -ffile-prefix-map=/tmp/go-build1304235650=/tmp/go-build -gno-record-gcc-switches'

What did you do?

$ gopls version
golang.org/x/tools/gopls v0.16.0

$ mkdir -p ~/scratch/maps-all
$ go1.23rc1 mod init example.com/m

Verify go.mod contains this:

module example.com/m

go 1.23rc1

$ vi mapsall.go

Write this code:

package main

import (
	"fmt"
	"maps"
)
func main() {
	m := make(map[string]int)
	m["a"] = 1
	m["b"] = 2
	for k, v := range maps.All(m) {
		fmt.Println(k, v)
	}
}

$ go run mapsall.go
<verify it prints the expected thing, which it does, without reporting warnings>

Now edit mapsall.go in any editor using gopls. Witness errors like:

cannot range over maps.All(m) (value of type iter.Seq2[string, int])
(InvalidRangeExpr)

What did you see happen?

gopls seems to issue errors for code using the new range-over-func stuff, even when go build works in the same module.

What did you expect to see?

No errors.

https://github.com/golang/tools/releases/tag/gopls%2Fv0.16.0 mentions

This version of gopls is the first to support the new language features of Go 1.23, including range-over-func iterators and support for the godebug directive in go.mod files.

It doesn't mention any special configuration steps, so I assume this should work out of the box and is keyed off the go version in go.mod?

@gopherbot gopherbot added Tools This label describes issues relating to any tools in the x/tools repository. gopls Issues related to the Go language server, gopls. labels Jun 29, 2024
@gopherbot gopherbot added this to the Unreleased milestone Jun 29, 2024
@matta
Copy link
Author

matta commented Jun 29, 2024

Setting GOEXPERIMENT=rangefunc in my environment cured it.

I'll leave this open because it isn't clear to me if this is expected behavior for 1.23rc1 projects.

@hyangah
Copy link
Contributor

hyangah commented Jun 29, 2024

Did you build gopls with go1.23rc1?

What is the output of go version -m <path/to/gopls>?

@teial
Copy link

teial commented Jun 29, 2024

If it helps, I have the same problem, and go version -m <path/to/gopls> returns /nix/store/pxyj1rv5jbf8rj5i0gdpwlf3lf4617iq-gopls-0.16.0/bin/gopls: go1.22.3 which is probably the real issue here. In my case gopls is taken from nixpkgs unstable channel, and the derivation there points to the correct tag gopls/v0.16.0 but it probably needs override somewhere to be built with go 1.23rc1. This is nix issue of course, not gopls. Adding GOEXPERIMENT did help in my case.

@matta
Copy link
Author

matta commented Jun 29, 2024

EDIT TLDR: yes, building gopls with go1.23rc1 fixes the issue.

I installed gopls v0.16 with go install golang.org/x/tools/gopls@latest and it looks like it was built with go 1.22.3.

...but it looks like building it with go1.23rc1 doesn't change the symptoms I observe.

EDIT I was wrong. Looks like a gopls built with go1.23rc1 works with range-over-func. (I got confused by NeoVim using a version of gopls that it installed itself)

Anyway, for the first attempt I get the symptoms I report in #68248 (comment), namely that GOEXPERIMENT=rangefunc is necessary for the new range-over-func feature to be considered Ok by gopls.

% go version -m $(which gopls)
/home/matt/go/bin/gopls: go1.22.3 X:rangefunc
	path	golang.org/x/tools/gopls
	mod	golang.org/x/tools/gopls	v0.16.0	h1:JOYR1NabC699+pLFI02CxlD9xaPXdaVmPp7f01k0/hE=
	dep	github.com/BurntSushi/toml	v1.2.1	h1:9F2/+DoOYIOksmaJFPw1tGFy1eDnIJXg+UHjuD8lTak=
	dep	github.com/google/go-cmp	v0.6.0	h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
	dep	golang.org/x/exp/typeparams	v0.0.0-20221212164502-fae10dda9338	h1:2O2DON6y3XMJiQRAS1UWU+54aec2uopH3x7MAiqGW6Y=
	dep	golang.org/x/mod	v0.18.0	h1:5+9lSbEzPSdWkH32vYPBwEpX8KwDbM52Ud9xBUvNlb0=
	dep	golang.org/x/sync	v0.7.0	h1:YsImfSBoP9QPYL0xyKJPq0gcaJdG3rInoqxTWbfQu9M=
	dep	golang.org/x/telemetry	v0.0.0-20240607193123-221703e18637	h1:3Wt8mZlbFwG8llny+t18kh7AXxyWePFycXMuVdHxnyM=
	dep	golang.org/x/text	v0.16.0	h1:a94ExnEXNtEwYLGJSIUxnWoxoRz/ZcCsV63ROupILh4=
	dep	golang.org/x/tools	v0.22.1-0.20240620150659-cb3016b76f3e	h1:Cht5EwNRmW9EgQ7ihPmgIswaQW4jR1cPbmzGCNl2++8=
	dep	golang.org/x/vuln	v1.0.4	h1:SP0mPeg2PmGCu03V+61EcQiOjmpri2XijexKdzv8Z1I=
	dep	honnef.co/go/tools	v0.4.7	h1:9MDAWxMoSnB6QoSqiVr7P5mtkT9pOc1kSxchzPCnqJs=
	dep	mvdan.cc/gofumpt	v0.6.0	h1:G3QvahNDmpD+Aek/bNOLrFR2XC6ZAdo62dZu65gmwGo=
	dep	mvdan.cc/xurls/v2	v2.5.0	h1:lyBNOm8Wo71UknhUs4QTFUNNMyxy2JEIaKKo0RWOh+8=
	build	-buildmode=exe
	build	-compiler=gc
	build	DefaultGODEBUG=httplaxcontentlength=1,httpmuxgo121=1,panicnil=1,tls10server=1,tlsrsakex=1,tlsunsafeekm=1
	build	CGO_ENABLED=1
	build	CGO_CFLAGS=
	build	CGO_CPPFLAGS=
	build	CGO_CXXFLAGS=
	build	CGO_LDFLAGS=
	build	GOARCH=amd64
	build	GOEXPERIMENT=rangefunc
	build	GOOS=linux
	build	GOAMD64=v1

After this I ran go1.23rc1 install golang.org/x/tools/gopls@latest and have:

go version -m $(which gopls)
/home/matt/go/bin/gopls: go1.23rc1
	path	golang.org/x/tools/gopls
	mod	golang.org/x/tools/gopls	v0.16.0	h1:JOYR1NabC699+pLFI02CxlD9xaPXdaVmPp7f01k0/hE=
	dep	github.com/BurntSushi/toml	v1.2.1	h1:9F2/+DoOYIOksmaJFPw1tGFy1eDnIJXg+UHjuD8lTak=
	dep	github.com/google/go-cmp	v0.6.0	h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
	dep	golang.org/x/exp/typeparams	v0.0.0-20221212164502-fae10dda9338	h1:2O2DON6y3XMJiQRAS1UWU+54aec2uopH3x7MAiqGW6Y=
	dep	golang.org/x/mod	v0.18.0	h1:5+9lSbEzPSdWkH32vYPBwEpX8KwDbM52Ud9xBUvNlb0=
	dep	golang.org/x/sync	v0.7.0	h1:YsImfSBoP9QPYL0xyKJPq0gcaJdG3rInoqxTWbfQu9M=
	dep	golang.org/x/telemetry	v0.0.0-20240607193123-221703e18637	h1:3Wt8mZlbFwG8llny+t18kh7AXxyWePFycXMuVdHxnyM=
	dep	golang.org/x/text	v0.16.0	h1:a94ExnEXNtEwYLGJSIUxnWoxoRz/ZcCsV63ROupILh4=
	dep	golang.org/x/tools	v0.22.1-0.20240620150659-cb3016b76f3e	h1:Cht5EwNRmW9EgQ7ihPmgIswaQW4jR1cPbmzGCNl2++8=
	dep	golang.org/x/vuln	v1.0.4	h1:SP0mPeg2PmGCu03V+61EcQiOjmpri2XijexKdzv8Z1I=
	dep	honnef.co/go/tools	v0.4.7	h1:9MDAWxMoSnB6QoSqiVr7P5mtkT9pOc1kSxchzPCnqJs=
	dep	mvdan.cc/gofumpt	v0.6.0	h1:G3QvahNDmpD+Aek/bNOLrFR2XC6ZAdo62dZu65gmwGo=
	dep	mvdan.cc/xurls/v2	v2.5.0	h1:lyBNOm8Wo71UknhUs4QTFUNNMyxy2JEIaKKo0RWOh+8=
	build	-buildmode=exe
	build	-compiler=gc
	build	DefaultGODEBUG=asynctimerchan=1,gotypesalias=0,httplaxcontentlength=1,httpmuxgo121=1,httpservecontentkeepheaders=1,panicnil=1,tls10server=1,tls3des=1,tlskyber=0,tlsrsakex=1,tlsunsafeekm=1,winreadlinkvolume=0,winsymlink=0,x509keypairleaf=0,x509negativeserial=1
	build	CGO_ENABLED=1
	build	CGO_CFLAGS=
	build	CGO_CPPFLAGS=
	build	CGO_CXXFLAGS=
	build	CGO_LDFLAGS=
	build	GOARCH=amd64
	build	GOOS=linux
	build	GOAMD64=v1

EDIT: that fixes things.

@matta
Copy link
Author

matta commented Jun 30, 2024

FWIW, I reported a similar issue in staticcheck. There, the fix to build with go1.23rc1 worked as well.

@dominikh also added code to staticcheck that will report an error if the tool is used on a go module requiring versions of go newer than what the tool was built with. gopls could consider a similar check.

@findleyr
Copy link
Contributor

findleyr commented Jul 1, 2024

@dominikh also added code to staticcheck that will report an error if the tool is used on a go module requiring versions of go newer than what the tool was built with. gopls could consider a similar check.

Thanks, that's a good idea. I'll repurpose this issue for that. (EDIT: opted for a new issue)

@findleyr
Copy link
Contributor

findleyr commented Jul 1, 2024

Closing as I believe the issue is now understood.

@findleyr findleyr closed this as not planned Won't fix, can't repro, duplicate, stale Jul 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
gopls Issues related to the Go language server, gopls. Tools This label describes issues relating to any tools in the x/tools repository.
Projects
None yet
Development

No branches or pull requests

6 participants