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: go1.22.2 warning loop variable v captured by func literal loopclosure #66876

Closed
synhi opened this issue Apr 17, 2024 · 8 comments
Assignees
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

@synhi
Copy link

synhi commented Apr 17, 2024

What version of Go, VS Code & VS Code Go extension are you using?

Version Information
  • Run go version to get version of Go from the VS Code integrated terminal.
    • go version go1.22.2 linux/amd64
  • Run gopls -v version to get version of Gopls from the VS Code integrated terminal.
    • golang.org/x/tools/gopls v0.15.3
      golang.org/x/tools/gopls@v0.15.3 h1:zbdOidFrPTc8Bx0YrN5QKgJ0zCjyGi0L27sKQ/bDG5o=
      github.com/BurntSushi/toml@v1.2.1 h1:9F2/+DoOYIOksmaJFPw1tGFy1eDnIJXg+UHjuD8lTak=
      github.com/google/go-cmp@v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
      golang.org/x/exp/typeparams@v0.0.0-20221212164502-fae10dda9338 h1:2O2DON6y3XMJiQRAS1UWU+54aec2uopH3x7MAiqGW6Y=
      golang.org/x/mod@v0.15.0 h1:SernR4v+D55NyBH2QiEQrlBAnj1ECL6AGrA5+dPaMY8=
      golang.org/x/sync@v0.6.0 h1:5BMeUDZ7vkXGfEr1x9B4bRcTH4lpkTkpdh0T/J+qjbQ=
      golang.org/x/telemetry@v0.0.0-20240209200032-7b892fcb8a78 h1:vcVnuftN4J4UKLRcgetjzfU9FjjgXUUYUc3JhFplgV4=
      golang.org/x/text@v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
      golang.org/x/tools@v0.18.1-0.20240412183611-d92ae0781217 h1:uH9jJYgeLCvblH0S+03kFO0qUDxRkbLRLFiKVVDl7ak=
      golang.org/x/vuln@v1.0.1 h1:KUas02EjQK5LTuIx1OylBQdKKZ9jeugs+HiqO5HormU=
      honnef.co/go/tools@v0.4.6 h1:oFEHCKeID7to/3autwsWfnuv69j3NsfcXbvJKuIcep8=
      mvdan.cc/gofumpt@v0.6.0 h1:G3QvahNDmpD+Aek/bNOLrFR2XC6ZAdo62dZu65gmwGo=
      mvdan.cc/xurls/v2@v2.5.0 h1:lyBNOm8Wo71UknhUs4QTFUNNMyxy2JEIaKKo0RWOh+8=
      go: go1.22.2
  • Run code -v or code-insiders -v to get version of VS Code or VS Code Insiders.
    • 1.88.1
  • Check your installed extensions to get the version of the VS Code Go extension
    • v0.41.2
  • Run Ctrl+Shift+P (Cmd+Shift+P on Mac OS) > Go: Locate Configured Go Tools command.

Environment

GOBIN: undefined
toolsGopath:
gopath: /root/go
GOROOT: /usr/local/go
PATH: /root/.vscode-server/bin/e170252f762678dec6ca2cc69aba1570769a5d39/bin/remote-cli:/usr/local/go/bin:/root/go/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/root/.local/bin:/root/.local/bin

Tools

go:	/usr/local/go/bin/go: go version go1.22.2 linux/amd64

gopls:	/root/go/bin/gopls	(version: v0.15.3 built with go: go1.22.2)
gotests:	/root/go/bin/gotests	(version: v1.6.0 built with go: go1.22.2)
gomodifytags:	/root/go/bin/gomodifytags	(version: v1.16.0 built with go: go1.22.2)
impl:	/root/go/bin/impl	(version: v1.1.0 built with go: go1.22.2)
goplay:	/root/go/bin/goplay	(version: v1.0.0 built with go: go1.22.2)
dlv:	/root/go/bin/dlv	(version: v1.22.1 built with go: go1.22.2)
staticcheck:	/root/go/bin/staticcheck	(version: v0.4.7 built with go: go1.22.2)

Go env

Workspace Folder (server):

GO111MODULE=''
GOARCH='amd64'
GOBIN=''
GOCACHE='/root/.cache/go-build'
GOENV='/root/.config/go/env'
GOEXE=''
GOEXPERIMENT=''
GOFLAGS=''
GOHOSTARCH='amd64'
GOHOSTOS='linux'
GOINSECURE=''
GOMODCACHE='/root/go/pkg/mod'
GONOPROXY=''
GONOSUMDB=''
GOOS='linux'
GOPATH='/root/go'
GOPRIVATE=''
GOPROXY='https://proxy.golang.org,direct'
GOROOT='/usr/local/go'
GOSUMDB='sum.golang.org'
GOTMPDIR=''
GOTOOLCHAIN='auto'
GOTOOLDIR='/usr/local/go/pkg/tool/linux_amd64'
GOVCS=''
GOVERSION='go1.22.2'
GCCGO='gccgo'
GOAMD64='v1'
AR='ar'
CC='gcc'
CXX='g++'
CGO_ENABLED='1'
GOMOD='/xxx/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-build445704314=/tmp/go-build -gno-record-gcc-switches'

Workspace Folder (web):

GO111MODULE=''
GOARCH='amd64'
GOBIN=''
GOCACHE='/root/.cache/go-build'
GOENV='/root/.config/go/env'
GOEXE=''
GOEXPERIMENT=''
GOFLAGS=''
GOHOSTARCH='amd64'
GOHOSTOS='linux'
GOINSECURE=''
GOMODCACHE='/root/go/pkg/mod'
GONOPROXY=''
GONOSUMDB=''
GOOS='linux'
GOPATH='/root/go'
GOPRIVATE=''
GOPROXY='https://proxy.golang.org,direct'
GOROOT='/usr/local/go'
GOSUMDB='sum.golang.org'
GOTMPDIR=''
GOTOOLCHAIN='auto'
GOTOOLDIR='/usr/local/go/pkg/tool/linux_amd64'
GOVCS=''
GOVERSION='go1.22.2'
GCCGO='gccgo'
GOAMD64='v1'
AR='ar'
CC='gcc'
CXX='g++'
CGO_ENABLED='1'
GOMOD='/xxx/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-build300344583=/tmp/go-build -gno-record-gcc-switches'

Describe the bug

go 1.22 alredy fix the loop vairable issue, why waring "loop variable v captured by func literal loopclosure" (go.mod go version 1.22.2)

waring link https://pkg.go.dev/golang.org/x/tools/go/analysis/passes/loopclosure

Steps to reproduce the behavior:

package main

import "fmt"

func main() {
	elem := make([]string, 10)
	for _, v := range elem {
		go func() {
			fmt.Println(v)
		}()
	}
}

Screenshots or recordings

image

@ansaba ansaba transferred this issue from golang/vscode-go Apr 17, 2024
@ansaba ansaba added gopls Issues related to the Go language server, gopls. Tools This label describes issues relating to any tools in the x/tools repository. labels Apr 17, 2024
@ansaba
Copy link

ansaba commented Apr 17, 2024

I tried on go1.22.2 darwin/amd64 and the issue was not reproducible.

@seankhliao seankhliao changed the title go1.22.2 waring loop variable v captured by func literal loopclosure x/tools/gopls: go1.22.2 waring loop variable v captured by func literal loopclosure Apr 17, 2024
@gopherbot gopherbot added this to the Unreleased milestone Apr 17, 2024
@timothy-king
Copy link
Contributor

Looks like the GoVersion is < 1.22.0 for the file. This value is derived from the go.mod file for the module, the build tags for the file, and the compiler version used to build gopls (and the -lang flag in some rare cases). The compiler version used to build gopls is >= 1.22 so that is not the issue.

What does the "go 1.XY" line say in the go.mod file? (Or is there no go.mod file?)

@seankhliao seankhliao added the WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided. label Apr 17, 2024
@synhi
Copy link
Author

synhi commented Apr 18, 2024

Looks like the GoVersion is < 1.22.0 for the file. This value is derived from the go.mod file for the module, the build tags for the file, and the compiler version used to build gopls (and the -lang flag in some rare cases). The compiler version used to build gopls is >= 1.22 so that is not the issue.

What does the "go 1.XY" line say in the go.mod file? (Or is there no go.mod file?)

go.mod: go 1.22.2

@synhi
Copy link
Author

synhi commented Apr 18, 2024

I tried on go1.22.2 darwin/amd64 and the issue was not reproducible.

try this command

go clean -cache -modcache

then Run Ctrl+Shift+P (Cmd+Shift+P on Mac OS) > Go: Install/Update Tools command and select all options

I use vscode remote-ssh extension to connect a linux server

@ohheydom
Copy link

I'm seeing the same issue. Looks like a possible regression -> #65910. gopls 0.15.2 works without issue.

Following the same steps from that issue:

gopls version

Build info
----------
golang.org/x/tools/gopls v0.15.3
    golang.org/x/tools/gopls@v0.15.3 h1:zbdOidFrPTc8Bx0YrN5QKgJ0zCjyGi0L27sKQ/bDG5o=
    github.com/BurntSushi/toml@v1.2.1 h1:9F2/+DoOYIOksmaJFPw1tGFy1eDnIJXg+UHjuD8lTak=
    github.com/google/go-cmp@v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
    golang.org/x/exp/typeparams@v0.0.0-20221212164502-fae10dda9338 h1:2O2DON6y3XMJiQRAS1UWU+54aec2uopH3x7MAiqGW6Y=
    golang.org/x/mod@v0.15.0 h1:SernR4v+D55NyBH2QiEQrlBAnj1ECL6AGrA5+dPaMY8=
    golang.org/x/sync@v0.6.0 h1:5BMeUDZ7vkXGfEr1x9B4bRcTH4lpkTkpdh0T/J+qjbQ=
    golang.org/x/telemetry@v0.0.0-20240209200032-7b892fcb8a78 h1:vcVnuftN4J4UKLRcgetjzfU9FjjgXUUYUc3JhFplgV4=
    golang.org/x/text@v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
    golang.org/x/tools@v0.18.1-0.20240412183611-d92ae0781217 h1:uH9jJYgeLCvblH0S+03kFO0qUDxRkbLRLFiKVVDl7ak=
    golang.org/x/vuln@v1.0.1 h1:KUas02EjQK5LTuIx1OylBQdKKZ9jeugs+HiqO5HormU=
    honnef.co/go/tools@v0.4.6 h1:oFEHCKeID7to/3autwsWfnuv69j3NsfcXbvJKuIcep8=
    mvdan.cc/gofumpt@v0.6.0 h1:G3QvahNDmpD+Aek/bNOLrFR2XC6ZAdo62dZu65gmwGo=
    mvdan.cc/xurls/v2@v2.5.0 h1:lyBNOm8Wo71UknhUs4QTFUNNMyxy2JEIaKKo0RWOh+8=
go: go1.22.2

go env

GO111MODULE=''
GOARCH='arm64'
GOBIN=''
GOCACHE='/Users/dominic/Library/Caches/go-build'
GOENV='/Users/dominic/Library/Application Support/go/env'
GOEXE=''
GOEXPERIMENT=''
GOFLAGS=''
GOHOSTARCH='arm64'
GOHOSTOS='darwin'
GOINSECURE=''
GOMODCACHE='/Users/dominic/go/pkg/mod'
GONOPROXY='gitlab.morningconsult.com'
GONOSUMDB='gitlab.morningconsult.com'
GOOS='darwin'
GOPATH='/Users/dominic/go'
GOPRIVATE='gitlab.morningconsult.com'
GOPROXY='https://proxy.golang.org,direct'
GOROOT='/usr/local/go'
GOSUMDB='sum.golang.org'
GOTMPDIR=''
GOTOOLCHAIN='auto'
GOTOOLDIR='/usr/local/go/pkg/tool/darwin_arm64'
GOVCS=''
GOVERSION='go1.22.2'
GCCGO='gccgo'
AR='ar'
CC='clang'
CXX='clang++'
CGO_ENABLED='1'
GOMOD='/Users/dominic/Projects/loopclosure/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/r3/5ch28yq55hv_8fk9bd_7jdnm0000gn/T/go-build1471649584=/tmp/go-build -gno-record-gcc-switches -fno-common'

What did you do?

With the following main.go:

package main

import (
	"fmt"
)

func main() {
	for i := range 10 {
		go func() { fmt.Println(i) }()
	}
}

and go.mod

module example.com/loopclosure

go 1.22.2

I ran

gopls check main.go

What did you see happen?

/Users/dominic/Projects/sandbox/main.go:9:27-28: loop variable i captured by func literal

What did you expect to see?

No error message

@findleyr findleyr self-assigned this Apr 19, 2024
@findleyr
Copy link
Contributor

Thanks, yes this is a regression in gopls@v0.15.3, specifically this CL: https://go.dev/cl/576678

That code should be comparing language versions, not toolchain versions :(

Fix incoming. To disable this analysis in the meantime: "analyses": { "loopclosure": false }

@findleyr
Copy link
Contributor

Aha, we didn't catch this because it isn't broken at master.
The problem is that gopls-release-branch.0.15 lacks https://go.dev/cl/567635.

@seankhliao seankhliao removed the WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided. label Apr 19, 2024
@findleyr findleyr changed the title x/tools/gopls: go1.22.2 waring loop variable v captured by func literal loopclosure x/tools/gopls: go1.22.2 warning loop variable v captured by func literal loopclosure Apr 19, 2024
@findleyr findleyr modified the milestones: Unreleased, gopls/v0.15.4 Apr 22, 2024
@findleyr
Copy link
Contributor

Closing as this has been cherry-picked to the release branch, and we are preparing the v0.15.4 release.
The release branch was fixed in https://go.dev/cl/579758 (the issue number was wrong in that CL, which is why it wasn't associated here).

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

7 participants