golangci / golangci-lint

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

golangci-lint incorrectly complains about Go version being lower than needed (Go workspace) #4894

Open ghost opened 3 months ago

ghost commented 3 months ago

Welcome

Description of the problem

go list -m -json outputs multiple JSON documents when in a workspace, but gomoddirectives only decodes the first one. That can lead to golangci-lint wrongly complaining

WARN [linters_context] copyloopvar: this linter is disabled because the Go version (1.20) of your project is lower than Go 1.22 WARN [linters_context] intrange: this linter is disabled because the Go version (1.20) of your project is lower than Go 1.22

https://github.com/golangci/golangci-lint/blob/90664f6c85340d8a598e1d99a6dc4f318ea0c051/pkg/config/config.go#L79 https://github.com/ldez/gomoddirectives/blob/7ed1901522539e1461a03c3237fba32af0f70523/module.go#L24-L33

Version of golangci-lint

```console $ golangci-lint --version golangci-lint has version 1.59.1 built with go1.22.3 from 1a55854a on 2024-06-09T18:08:33Z ```

Configuration

```console linters: enable: - asasalint - asciicheck - bidichk - bodyclose - canonicalheader - containedctx - contextcheck - copyloopvar - decorder - dogsled - dupword - durationcheck - err113 - errcheck - errchkjson - errname - errorlint - exhaustive - exhaustruct - exportloopref - fatcontext - forbidigo - forcetypeassert - gci - ginkgolinter - gocheckcompilerdirectives - gochecknoglobals - gochecknoinits - gochecksumtype - goconst - gocritic - gocyclo - godot - godox - gofmt - gofumpt - goheader - goimports - gomoddirectives - gomodguard - goprintffuncname - gosec - gosimple - gosmopolitan - govet - grouper - importas - inamedparam - ineffassign - interfacebloat - intrange - ireturn - loggercheck - maintidx - makezero - mirror - misspell - musttag - nakedret - nestif - nilerr - nilnil - noctx - nolintlint - nonamedreturns - nosprintfhostport - paralleltest - prealloc - predeclared - promlinter - protogetter - reassign - revive - rowserrcheck - sloglint - spancheck - sqlclosecheck - staticcheck - stylecheck - tagalign - tenv - testableexamples - testifylint - testpackage - thelper - tparallel - unconvert - unparam - unused - wastedassign - whitespace - wrapcheck - zerologlint ```

Go environment

```console $ go version && go env GO111MODULE='' GOARCH='amd64' GOBIN='' GOCACHE='/home/xxx/.cache/go-build' GOENV='/home/xxx/.config/go/env' GOEXE='' GOEXPERIMENT='' GOFLAGS='' GOHOSTARCH='amd64' GOHOSTOS='linux' GOINSECURE='' GOMODCACHE='/home/xxx/gocode/pkg/mod' GONOPROXY='' GONOSUMDB='' GOOS='linux' GOPATH='/home/xxx/gocode' GOPRIVATE='' GOPROXY='direct' GOROOT='/home/xxx/go' GOSUMDB='off' GOTMPDIR='' GOTOOLCHAIN='auto' GOTOOLDIR='/home/xxx/go/pkg/tool/linux_amd64' GOVCS='' GOVERSION='go1.22.5' GCCGO='gccgo' GOAMD64='v1' AR='ar' CC='gcc' CXX='g++' CGO_ENABLED='1' GOMOD='/home/xxx/gosource/app/go.mod' GOWORK='/home/xxx/gosource/go.work' 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-build634417022=/tmp/go-build -gno-record-gcc-switches' ```

Verbose output of running

```console $ golangci-lint cache clean $ golangci-lint run -v level=info msg="golangci-lint has version 1.59.1 built with go1.22.3 from 1a55854a on 2024-06-09T18:08:33Z" level=info msg="[config_reader] Config search paths: [./ /home/xxx/gosource/autocrack /home/xxx/gosource /home/xxx /home /]" level=info msg="[config_reader] Used config file .golangci.yml" level=info msg="[lintersdb] Active 93 linters: [asasalint asciicheck bidichk bodyclose canonicalheader containedctx contextcheck copyloopvar decorder dogsled dupword durationcheck err113 errcheck errchkjson errname errorlint exhaustive exhaustruct exportloopref fatcontext forbidigo forcetypeassert gci ginkgolinter gocheckcompilerdirectives gochecknoglobals gochecknoinits gochecksumtype goconst gocritic gocyclo godot godox gofmt gofumpt goheader goimports gomoddirectives gomodguard goprintffuncname gosec gosimple gosmopolitan govet grouper importas inamedparam ineffassign interfacebloat intrange ireturn loggercheck maintidx makezero mirror misspell musttag nakedret nestif nilerr nilnil noctx nolintlint nonamedreturns nosprintfhostport paralleltest prealloc predeclared promlinter protogetter reassign revive rowserrcheck sloglint spancheck sqlclosecheck staticcheck stylecheck tagalign tenv testableexamples testifylint testpackage thelper tparallel unconvert unparam unused wastedassign whitespace wrapcheck zerologlint]" level=info msg="[loader] Go packages loading at mode 575 (compiled_files|exports_file|imports|name|types_sizes|deps|files) took 120.087359ms" level=info msg="[runner/filename_unadjuster] Pre-built 0 adjustments in 967.688µs" level=warning msg="[linters_context] copyloopvar: this linter is disabled because the Go version (1.20) of your project is lower than Go 1.22" level=info msg="[linters_context] importas settings found, but no aliases listed. List aliases under alias: key." level=info msg="[linters_context/goanalysis] analyzers took 6.988948422s with top 10 stages: buildir: 3.312416762s, buildssa: 1.424201739s, the_only_name: 596.519998ms, exhaustive: 427.059617ms, inspect: 245.522072ms, ctrlflow: 196.423534ms, fact_deprecated: 128.238874ms, printf: 102.011834ms, fact_purity: 76.415965ms, nilness: 60.41538ms" level=warning msg="[linters_context] intrange: this linter is disabled because the Go version (1.20) of your project is lower than Go 1.22" level=info msg="[runner] Issues before processing: 70, after processing: 0" level=info msg="[runner] Processors filtering stat (out/in): filename_unadjuster: 70/70, autogenerated_exclude: 70/70, cgo: 70/70, path_prettifier: 70/70, skip_dirs: 70/70, exclude: 70/70, invalid_issue: 70/70, skip_files: 70/70, identifier_marker: 70/70, exclude-rules: 16/70, nolint: 0/16" level=info msg="[runner] processing took 2.489774ms with stages: nolint: 967.061µs, exclude-rules: 741.818µs, identifier_marker: 651.015µs, path_prettifier: 58.283µs, autogenerated_exclude: 53.146µs, skip_dirs: 6.119µs, invalid_issue: 4.443µs, cgo: 3.747µs, filename_unadjuster: 1.825µs, max_same_issues: 460ns, uniq_by_line: 316ns, fixer: 278ns, sort_results: 186ns, skip_files: 179ns, exclude: 174ns, max_from_linter: 153ns, diff: 145ns, source_code: 112ns, path_shortener: 91ns, max_per_file_from_linter: 81ns, path_prefixer: 73ns, severity-rules: 69ns" level=info msg="[runner] linters took 3.042585283s with stages: goanalysis_metalinter: 3.040012914s, copyloopvar: 11.791µs, intrange: 7.46µs" level=info msg="File cache stats: 8 entries of total size 23.3KiB" level=info msg="Memory: 33 samples, avg is 266.3MB, max is 349.9MB" level=info msg="Execution took 3.165790382s" ```

A minimal reproducible example or link to a public repository

```go // add your code here ```

Validation

Supporter

boring-cyborg[bot] commented 3 months ago

Hey, thank you for opening your first Issue ! 🙂 If you would like to contribute we have a guide for contributors.

ldez commented 3 months ago

Hello,

You checked:

[x] Yes, I've included all the information above (version, config, etc.).

But you didn't provide the required minimal reproducible example or link to a public repository.

You can use the following option:

run:
  go: '1.22'
ghost commented 3 months ago

To reproduce:

$ mkdir golangci
$ cd golangci
$ go work init
$ mkdir a b
$ echo module a > a/go.mod
$ echo go 1.20 >> a/go.mod
$ echo module b > b/go.mod
$ echo go 1.22 >> b/go.mod
$ echo package main > b/main.go
$ go work use a
$ go work use b
$ cd b
$ golangci-lint run --enable=copyloopvar,intrange
WARN [linters_context] copyloopvar: this linter is disabled because the Go version (1.20) of your project is lower than Go 1.22 
WARN [linters_context] intrange: this linter is disabled because the Go version (1.20) of your project is lower than Go 1.22 
TaylorBarrick commented 2 months ago

This is very noticeable when leveraging generics and a version > 1.18, yielding predeclared any requires go1.18 or later (typecheck). Reverting to version 1.60.1 does not yield the incorrect error.

ldez commented 2 months ago

@TaylorBarrick I flagged your comment as offtopic because your error is not related to this issue.

I think it's related to this issue: https://github.com/golangci/golangci-lint/issues/4942 If it's not a duplicate you can open a new dedicated issue.