go icon indicating copy to clipboard operation
go copied to clipboard

x/tools/gopls: InvalidRangeExpr when using GOEXPERIMENT=rangefunc on iter.Seq

Open stephenafamo opened this issue 1 year ago • 4 comments

gopls version

golang.org/x/tools/gopls v0.15.0-pre.2 golang.org/x/tools/[email protected] h1:A48ndEGf3pvzJ0k22HneSU7msCaggzcpg56ELTEuapQ= github.com/BurntSushi/[email protected] h1:9F2/+DoOYIOksmaJFPw1tGFy1eDnIJXg+UHjuD8lTak= github.com/google/[email protected] h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI= golang.org/x/exp/[email protected] h1:2O2DON6y3XMJiQRAS1UWU+54aec2uopH3x7MAiqGW6Y= golang.org/x/[email protected] h1:dGoOF9QVLYng8IHTm7BAyWqCqSheQ5pYWGhzW00YJr0= golang.org/x/[email protected] h1:5BMeUDZ7vkXGfEr1x9B4bRcTH4lpkTkpdh0T/J+qjbQ= golang.org/x/[email protected] h1:Nr7eTQpQZ/ytesxDJpQgaf0t4sdLnnDtAbmtViTrSUo= golang.org/x/[email protected] h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ= golang.org/x/[email protected] h1:WL/PnseOKWuDtRbgfYKDpuAmMpy3OrX84rv7uQFZJK4= golang.org/x/[email protected] h1:KUas02EjQK5LTuIx1OylBQdKKZ9jeugs+HiqO5HormU= honnef.co/go/[email protected] h1:oFEHCKeID7to/3autwsWfnuv69j3NsfcXbvJKuIcep8= mvdan.cc/[email protected] h1:G3QvahNDmpD+Aek/bNOLrFR2XC6ZAdo62dZu65gmwGo= mvdan.cc/xurls/[email protected] h1:lyBNOm8Wo71UknhUs4QTFUNNMyxy2JEIaKKo0RWOh+8= go: go1.22.0

go env

GO111MODULE=''
GOARCH='arm64'
GOBIN=''
GOCACHE='/Users/stephen/Library/Caches/go-build'
GOENV='/Users/stephen/Library/Application Support/go/env'
GOEXE=''
GOEXPERIMENT=''
GOFLAGS=''
GOHOSTARCH='arm64'
GOHOSTOS='darwin'
GOINSECURE=''
GOMODCACHE='/Users/stephen/go/pkg/mod'
GONOPROXY=''
GONOSUMDB=''
GOOS='darwin'
GOPATH='/Users/stephen/go'
GOPRIVATE=''
GOPROXY='https://proxy.golang.org,direct'
GOROOT='/Users/stephen/go/pkg/mod/golang.org/[email protected]'
GOSUMDB='sum.golang.org'
GOTMPDIR=''
GOTOOLCHAIN='go1.22.0+auto'
GOTOOLDIR='/Users/stephen/go/pkg/mod/golang.org/[email protected]/pkg/tool/darwin_arm64'
GOVCS=''
GOVERSION='go1.22.0'
GCCGO='gccgo'
AR='ar'
CC='clang'
CXX='clang++'
CGO_ENABLED='1'
GOMOD='/dev/null'
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/9d/1smj789x4r5bdjccy3tqss0c0000gn/T/go-build1153037537=/tmp/go-build -gno-record-gcc-switches -fno-common'

What did you do?

https://go.dev/play/p/rMbWTBEtveQ

What did you see happen?

gopls indicates an error even if it should be considered valid when GOEXPERIMENT=rangefunc is set

What did you expect to see?

No error shown

Editor and settings

Neovim with the following gopls configuration

            lspconfig.gopls.setup({
                capabilities = addCmpCapabilities(lspconfig.gopls.capabilities),
                settings = {
                    gopls = {
                        allowModfileModifications = false,
                        gofumpt = true,
                        codelenses = {
                            generate = true,
                            tidy = true,
                            upgrade_dependency = true,
                            vendor = true,
                        },
                        buildFlags = { "-tags", "goexperiment.rangefunc" },
                        directoryFilters = { "-**/node_modules", "-**/.git" },
                    },
                },
            })

Logs

No response

stephenafamo avatar Feb 12 '24 03:02 stephenafamo

It works for me.

In your go env output, I see GOEXPERIMENT=''. Are you sure you set GOEXPERIMENT in your environment?

I just did:

export GOEXPERIMENT=rangefunc
nvim main.go

I'm using coc.nvim, but wouldn't expect neovim native LSP to behave differently.

findleyr avatar Feb 15 '24 15:02 findleyr

When I try to set it as environment variables, I get

go: unknown GOEXPERIMENT rangefunc

I had previously tried to use the environment variable to add the configuration of gopls and got the same error, but I thought that was an issue with how I was setting up the configuration.

I have tried it again, but I run into the same error:

            lspconfig.gopls.setup({
                capabilities = addCmpCapabilities(lspconfig.gopls.capabilities),
                settings = {
                    gopls = {
                        allowModfileModifications = false,
                        gofumpt = true,
                        codelenses = {
                            generate = true,
                            tidy = true,
                            upgrade_dependency = true,
                            vendor = true,
                        },
                        buildFlags = { "-tags", "goexperiment.rangefunc" },
                        directoryFilters = { "-**/node_modules", "-**/.git" },
                        env = {
                            GOEXPERIMENT = "rangefunc",
                        },
                    },
                },
            })

stephenafamo avatar Feb 15 '24 15:02 stephenafamo

go: unknown GOEXPERIMENT rangefunc

Hmm, do you have multiple versions of go installed? Is it possible that the go your neovim sees is not go 1.22?

findleyr avatar Feb 15 '24 16:02 findleyr

So I updated my go binary (not just the toolchain) to go1.22 and got rid of the error:

go: unknown GOEXPERIMENT rangefunc

However, the InvalidRangeExpr error is still reported despite adding GOEXPERIMENT=rangefunc to everywhere I could:

  • go env with go env -w GOEXPERIMENT=rangefunc
  • The local project env with .envrc
  • The gopls configuration as outlined above.

stephenafamo avatar Feb 15 '24 16:02 stephenafamo

Could you share gopls logs from a short session that demonstrates the problem? That would help us investigate why it isn't working in your session. https://github.com/golang/tools/blob/master/gopls/doc/troubleshooting.md#capture-logs

findleyr avatar Feb 19 '24 23:02 findleyr

My bad. I was using Mason on Neovim, which installs its own gopls which is at v14.2, when I force it to use my installed gopls v15.0-pre.3, I was able to get it to work, but only with a local env variable

  • Setting go env with go env -w GOEXPERIMENT=rangefunc did not work
  • Setting the go pls configurations (both env and buildFlags also did not work.

stephenafamo avatar Feb 26 '24 00:02 stephenafamo

@stephenafamo hi, did you solve this problem?

RunThem avatar Apr 16 '24 07:04 RunThem

@RunThem The solution is to update gopls in Mason:

:MasonInstall gopls

alassek avatar Apr 24 '24 21:04 alassek