golangci / golangci-lint

Fast linters runner for Go
https://golangci-lint.run
GNU General Public License v3.0
15.77k stars 1.39k forks source link

fix: improve Go detection #5112

Closed ldez closed 2 weeks ago

ldez commented 2 weeks ago

To reproduce:

before ```console $ docker run --rm -v $(pwd):/app -w /app -it golang:1.23.2-alpine sh /app # GOTOOLCHAIN=go1.22.1 go install github.com/golangci/golangci-lint/cmd/golangci-lint@v1.61.0 ... /app # golangci-lint version golangci-lint has version v1.61.0 built with go1.22.1 from (unknown, modified: ?, mod sum: "h1:VvbOLaRVWmyxCnUIMTbf1kDsaJbTzH20FAMXTAlQGu8=") on (unknown) /app # rm -rf /go/pkg/mod/golang.org/toolchain@* /app # go version go version go1.23.2 linux/amd64 /app # golangci-lint run ###### High memory and CPU consumption and kill ```
after ```console $ docker run --rm -v $(pwd):/app -w /app -it golang:1.23.2-alpine sh /app # ./golangci-lint version golangci-lint has version (devel) built with go1.22.9 from (ac4d2c5c57fb41b42287cc0bad8b908eede8d01a, modified: false, mod sum: "") on 2024-11-07T14:40:46Z /app # ./golangci-lint run Error: can't load config: the Go language version (go1.22) used to build golangci-lint is lower than the targeted Go version (1.23.1) Failed executing command with error: can't load config: the Go language version (go1.22) used to build golangci-lint is lower than the targeted Go version (1.23.1) /app # ```

There is still an unmanaged case:

But this case cannot be caught without calling the Go binary, and I think this is a bad idea.

Also technically, the go version inside the go.mod is enough to analyze but the problem happens when Go handles the toolchain version before running golangci-lint. In this case, Go will install a newer version of Go (go1.23), and the problem will happen.

toolchain defines the minimum Go compiler version used to build. go defines the minimum Go language version used to write the code.

The difference is really important: if your module is a lib, then the user of this lib will rely on go and not on toolchain. This is the same thing for the tool pattern, only go will be used and not on toolchain.


Related to #4909 Related to #4938