golangci / golangci-lint

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

contextcheck: panic with runtime error #3048

Closed korprulu closed 10 months ago

korprulu commented 2 years ago

Welcome

Description of the problem

I got this error when I ran golangci-lint in the CircleCI's official image cimg/go:1.8. Then I tried to use image golangci/golangci-lint:v1.47.3 to run it, I got the same error.

ERRO [runner] Panic: contextcheck: package "xxxx" (isInitialPkg: true, needAnalyzeSource: true): runtime error: invalid memory address or nil pointer dereference: goroutine 8127 [running]:
runtime/debug.Stack()
    runtime/debug/stack.go:24 +0x65
github.com/golangci/golangci-lint/pkg/golinters/goanalysis.(*action).analyzeSafe.func1()
    github.com/golangci/golangci-lint/pkg/golinters/goanalysis/runner_action.go:101 +0x155
panic({0xf11b60, 0x1953a50})
    runtime/panic.go:838 +0x207
github.com/sylvia7788/contextcheck.(*runner).buildPkg(0xc00bff7d10?, 0xc0073ee340)
    github.com/sylvia7788/contextcheck@v1.0.4/contextcheck.go:273 +0x32
github.com/sylvia7788/contextcheck.(*runner).checkFuncWithCtx(0xc0011add08, 0xc004831860)
    github.com/sylvia7788/contextcheck@v1.0.4/contextcheck.go:327 +0x350
github.com/sylvia7788/contextcheck.(*runner).run(0xc0011add08, 0xc00d26a820)
    github.com/sylvia7788/contextcheck@v1.0.4/contextcheck.go:101 +0x2a5
github.com/sylvia7788/contextcheck.NewRun.func1(0xf00880?)
    github.com/sylvia7788/contextcheck@v1.0.4/contextcheck.go:57 +0x33
github.com/golangci/golangci-lint/pkg/golinters/goanalysis.(*action).analyze(0xc0031f90c0)
    github.com/golangci/golangci-lint/pkg/golinters/goanalysis/runner_action.go:187 +0x9c4
github.com/golangci/golangci-lint/pkg/golinters/goanalysis.(*action).analyzeSafe.func2()
    github.com/golangci/golangci-lint/pkg/golinters/goanalysis/runner_action.go:105 +0x1d
github.com/golangci/golangci-lint/pkg/timeutils.(*Stopwatch).TrackStage(0xc0015bb180, {0x1095583, 0xc}, 0xc001e3e748)
    github.com/golangci/golangci-lint/pkg/timeutils/stopwatch.go:111 +0x4a
github.com/golangci/golangci-lint/pkg/golinters/goanalysis.(*action).analyzeSafe(0xc001044120?)
    github.com/golangci/golangci-lint/pkg/golinters/goanalysis/runner_action.go:104 +0x85
github.com/golangci/golangci-lint/pkg/golinters/goanalysis.(*loadingPackage).analyze.func2(0xc0031f90c0)
    github.com/golangci/golangci-lint/pkg/golinters/goanalysis/runner_loadingpackage.go:80 +0xb4
created by github.com/golangci/golangci-lint/pkg/golinters/goanalysis.(*loadingPackage).analyze
    github.com/golangci/golangci-lint/pkg/golinters/goanalysis/runner_loadingpackage.go:75 +0x1eb

Version of golangci-lint

golangci-lint has version 1.47.3 built from d186efe9 on 2022-08-01T11:01:02Z

Configuration file

```yml run: # default concurrency is a available CPU number concurrency: 4 # timeout for analysis, e.g. 30s, 5m, default is 1m deadline: 5m # which dirs to skip: they won't be analyzed; # can use regexp here: generated.*, regexp is applied on full path; # default value is empty list, but next dirs are always skipped independently # from this option's value: # vendor$, third_party$, testdata$, examples$, Godeps$, builtin$ skip-dirs: - genfiles$ - vendor$ - internal/pkg* # which files to skip: they will be analyzed, but issues from them # won't be reported. Default value is empty list, but there is # no need to include all autogenerated files, we confidently recognize # autogenerated files. If it's not please let us know. skip-files: - ".*\\.pb\\.go" - ".*\\.gen\\.go" # all available settings of specific linters linters-settings: godot: scope: toplevel capital: false errcheck: # report about not checking of errors in type assetions: `a := b.(MyStruct)`; # default is false: such cases aren't reported by default. check-type-assertions: true # report about assignment of errors to blank identifier: `num, _ := strconv.Atoi(numStr)`; # default is false: such cases aren't reported by default. check-blank: false govet: # report about shadowed variables check-shadowing: true golint: # minimal confidence for issues, default is 0.8 min-confidence: 0.0 gofmt: # simplify code: gofmt with `-s` option, true by default simplify: true gocyclo: # minimal code complexity to report, 30 by default (but we recommend 10-20) min-complexity: 10 maligned: # print struct with more effective memory layout or not, false by default suggest-new: true dupl: # tokens count to trigger issue, 150 by default threshold: 100 goconst: # minimal length of string constant, 3 by default min-len: 3 # minimal occurrences count to trigger, 3 by default min-occurrences: 3 depguard: list-type: blacklist include-go-root: false packages: - github.com/davecgh/go-spew/spew misspell: # Correct spellings using locale preferences for US or UK. # Default is to use a neutral variety of English. # Setting locale to US will correct the British spelling of 'colour' to 'color'. locale: US lll: # max line length, lines longer will be reported. Default is 120. # '\t' is counted as 1 character by default, and can be changed with the tab-width option line-length: 160 # tab width in spaces. Default to 1. tab-width: 1 unused: # treat code as a program (not a library) and report unused exported identifiers; default is false. # XXX: if you enable this setting, unused will report a lot of false-positives in text editors: # if it's called for subdir of a project it can't find funcs usages. All text editor integrations # with golangci-lint call it on a directory with the changed file. check-exported: false unparam: # call graph construction algorithm (cha, rta). In general, use cha for libraries, # and rta for programs with main packages. Default is cha. algo: cha # Inspect exported functions, default is false. Set to true if no external program/library imports your code. # XXX: if you enable this setting, unparam will report a lot of false-positives in text editors: # if it's called for subdir of a project it can't find external interfaces. All text editor integrations # with golangci-lint call it on a directory with the changed file. check-exported: false nakedret: # make an issue if func has more lines of code than this setting and it has naked returns; default is 30 max-func-lines: 30 prealloc: # XXX: we don't recommend using this linter before doing performance profiling. # For most programs usage of prealloc will be a premature optimization. # Report preallocation suggestions only on simple loops that have no returns/breaks/continues/gotos in them. # True by default. simple: true range-loops: true # Report preallocation suggestions on range loops, true by default for-loops: false # Report preallocation suggestions on for loops, false by default linters: enable: # - godot - deadcode - goimports - golint - govet - gosimple - interfacer - lll - misspell - staticcheck - structcheck - ineffassign - unconvert - unparam - unused - varcheck enable-all: false disable: - gocritic - prealloc - typecheck - megacheck - nakedret - scopelint - gochecknoglobals - gochecknoinits - gosec # start here to enable back all gometalinter linters - errcheck - goconst - maligned disable-all: false presets: - bugs - unused fast: false issues: # List of regexps of issue texts to exclude, empty list by default. # But independently from this option we use default exclude patterns, # it can be disabled by `exclude-use-default: false`. To list all # excluded by default patterns execute `golangci-lint run --help` exclude: - "type name will be used as service.ServiceAccount by other packages, and that stutters; consider calling this Account" # Independently from option `exclude` we use default exclude patterns, # it can be disabled by this option. To list all # excluded by default patterns execute `golangci-lint run --help`. # Default value for this option is true. exclude-use-default: true # Maximum issues count per one linter. Set to 0 to disable. Default is 50. max-per-linter: 0 # Maximum count of issues with the same text. Set to 0 to disable. Default is 3. max-same-issues: 0 include: - EXC0002 # disable excluding of issues about comments from golint ```

Go environment

``` go version go1.18.5 linux/amd64 GO111MODULE="" GOARCH="amd64" GOBIN="" GOCACHE="/home/circleci/.cache/go-build" GOENV="/home/circleci/.config/go/env" GOEXE="" GOEXPERIMENT="" GOFLAGS="" GOHOSTARCH="amd64" GOHOSTOS="linux" GOINSECURE="" GOMODCACHE="/home/circleci/go/pkg/mod" GONOPROXY="" GONOSUMDB="" GOOS="linux" GOPATH="/home/circleci/go" GOPRIVATE="" GOPROXY="https://proxy.golang.org,direct" GOROOT="/usr/local/go" GOSUMDB="sum.golang.org" GOTMPDIR="" GOTOOLDIR="/usr/local/go/pkg/tool/linux_amd64" GOVCS="" GOVERSION="go1.18.5" GCCGO="gccgo" GOAMD64="v1" AR="ar" CC="gcc" CXX="g++" CGO_ENABLED="1" GOMOD="/home/circleci/project/go.mod" GOWORK="" CGO_CFLAGS="-g -O2" CGO_CPPFLAGS="" CGO_CXXFLAGS="-g -O2" CGO_FFLAGS="-g -O2" CGO_LDFLAGS="-g -O2" PKG_CONFIG="pkg-config" GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build3188226263=/tmp/go-build -gno-record-gcc-switches" ```

Verbose output of running

``` INFO [config_reader] Config search paths: [./ /home/circleci/project /home/circleci /home /] INFO [config_reader] Used config file .golangci.yml INFO [lintersdb] Active 27 linters: [asasalint asciicheck bidichk bodyclose contextcheck deadcode durationcheck errchkjson errorlint exhaustive exportloopref goimports golint govet ineffassign interfacer lll makezero misspell nilerr noctx rowserrcheck sqlclosecheck structcheck unconvert unparam varcheck] INFO [lintersdb] Active presets: [bugs unused] INFO [loader] Go packages loading at mode 575 (imports|name|types_sizes|compiled_files|deps|exports_file|files) took 7.834113296s WARN [runner] The linter 'interfacer' is deprecated (since v1.38.0) due to: The repository of the linter has been archived by the owner. WARN [runner] The linter 'golint' is deprecated (since v1.41.0) due to: The repository of the linter has been archived by the owner. Replaced by revive. INFO [runner/filename_unadjuster] Pre-built 0 adjustments in 139.325458ms INFO [linters context/goanalysis] analyzers took 1m37.665507056s with top 10 stages: bidichk: 51.983283571s, buildssa: 16.530643426s, exhaustive: 4.134317241s, interfacer: 3.892241126s, goimports: 3.243275211s, unparam: 3.189968162s, inspect: 3.014200658s, unconvert: 2.024223295s, misspell: 1.957786295s, golint: 1.640103005s ERRO [runner] Panic: contextcheck: package "xxxx" (isInitialPkg: true, needAnalyzeSource: true): runtime error: invalid memory address or nil pointer dereference: goroutine 8395 [running]: runtime/debug.Stack() runtime/debug/stack.go:24 +0x65 github.com/golangci/golangci-lint/pkg/golinters/goanalysis.(*action).analyzeSafe.func1() github.com/golangci/golangci-lint/pkg/golinters/goanalysis/runner_action.go:101 +0x155 panic({0xf11b60, 0x1953a50}) runtime/panic.go:838 +0x207 github.com/sylvia7788/contextcheck.(*runner).buildPkg(0xc00db51720?, 0xc0062816c0) github.com/sylvia7788/contextcheck@v1.0.4/contextcheck.go:273 +0x32 github.com/sylvia7788/contextcheck.(*runner).checkFuncWithCtx(0xc00006dd08, 0xc0040d2ea0) github.com/sylvia7788/contextcheck@v1.0.4/contextcheck.go:327 +0x350 github.com/sylvia7788/contextcheck.(*runner).run(0xc00006dd08, 0xc01020cdd0) github.com/sylvia7788/contextcheck@v1.0.4/contextcheck.go:101 +0x2a5 github.com/sylvia7788/contextcheck.NewRun.func1(0xf00880?) github.com/sylvia7788/contextcheck@v1.0.4/contextcheck.go:57 +0x33 github.com/golangci/golangci-lint/pkg/golinters/goanalysis.(*action).analyze(0xc001c490c0) github.com/golangci/golangci-lint/pkg/golinters/goanalysis/runner_action.go:187 +0x9c4 github.com/golangci/golangci-lint/pkg/golinters/goanalysis.(*action).analyzeSafe.func2() github.com/golangci/golangci-lint/pkg/golinters/goanalysis/runner_action.go:105 +0x1d github.com/golangci/golangci-lint/pkg/timeutils.(*Stopwatch).TrackStage(0xc00193bdb0, {0x1095583, 0xc}, 0xc002bb3748) github.com/golangci/golangci-lint/pkg/timeutils/stopwatch.go:111 +0x4a github.com/golangci/golangci-lint/pkg/golinters/goanalysis.(*action).analyzeSafe(0xc002de3ce0?) github.com/golangci/golangci-lint/pkg/golinters/goanalysis/runner_action.go:104 +0x85 github.com/golangci/golangci-lint/pkg/golinters/goanalysis.(*loadingPackage).analyze.func2(0xc001c490c0) github.com/golangci/golangci-lint/pkg/golinters/goanalysis/runner_loadingpackage.go:80 +0xb4 created by github.com/golangci/golangci-lint/pkg/golinters/goanalysis.(*loadingPackage).analyze github.com/golangci/golangci-lint/pkg/golinters/goanalysis/runner_loadingpackage.go:75 +0x1eb WARN [runner] Can't run linter goanalysis_metalinter: goanalysis_metalinter: contextcheck: package "xxxx" (isInitialPkg: true, needAnalyzeSource: true): runtime error: invalid memory address or nil pointer dereference WARN [linters context] rowserrcheck is disabled because of go1.18. You can track the evolution of the go1.18 support by following the https://github.com/golangci/golangci-lint/issues/2649. WARN [linters context] sqlclosecheck is disabled because of go1.18. You can track the evolution of the go1.18 support by following the https://github.com/golangci/golangci-lint/issues/2649. WARN [linters context] structcheck is disabled because of go1.18. You can track the evolution of the go1.18 support by following the https://github.com/golangci/golangci-lint/issues/2649. INFO [runner] processing took 332.328µs with stages: max_same_issues: 77.75µs, nolint: 42.792µs, max_from_linter: 39.25µs, skip_dirs: 23.916µs, cgo: 21.75µs, path_prettifier: 10.25µs, autogenerated_exclude: 10.125µs, source_code: 9.791µs, filename_unadjuster: 9.041µs, exclude: 8.875µs, skip_files: 8.791µs, uniq_by_line: 8.666µs, exclude-rules: 8.583µs, identifier_marker: 8.541µs, diff: 8.5µs, max_per_file_from_linter: 8.5µs, severity-rules: 8.416µs, path_shortener: 8.375µs, sort_results: 8.25µs, path_prefixer: 2.166µs INFO [runner] linters took 24.321013137s with stages: goanalysis_metalinter: 24.31942972s, rowserrcheck: 149.125µs, sqlclosecheck: 27.666µs, structcheck: 22.167µs ERRO Running error: 1 error occurred: * can't run linter goanalysis_metalinter: goanalysis_metalinter: contextcheck: package "xxxx" (isInitialPkg: true, needAnalyzeSource: true): runtime error: invalid memory address or nil pointer dereference INFO Memory: 306 samples, avg is 271.6MB, max is 490.5MB INFO Execution took 32.449738724s ```

Code example or link to a public repository

N/A (private repo)

boring-cyborg[bot] commented 2 years ago

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

ldez commented 2 years ago

Hello,

can you provide a minimal reproducible code example?

Currently, I recommend disabling contextcheck.

arriven commented 2 years ago

@ldez not sure about minimal, but here's a gh-actions run on small OSS codebase (about 5k lines of go code in total) that triggers this https://github.com/arriven/db1000n/runs/7636407897?check_suite_focus=true

ldez commented 2 years ago

@arriven thank you :+1:

Yes, it's not minimal but it's a public repo so I think I will be able to work with that.

Antonboom commented 1 year ago

Reproduced, but another error:

$ go version
go version go1.21.0 darwin/arm64

$ golangci-lint version
golangci-lint has version 1.54.0 built with go1.21.0 from c1d8c565 on 2023-08-09T11:50:00Z

$ pwd
/tmp/db1000n

$ go clean -modcache
$ go mod tidy

$ ▶ golangci-lint run --disable-all --enable contextcheck ./...
ERRO Running error: 1 error occurred:
    * can't run linter goanalysis_metalinter: buildssa: failed to load package mapstructure: could not load export data: no export data for "github.com/mitchellh/mapstructure"

Related to https://github.com/golangci/golangci-lint/discussions/1920

ldez commented 10 months ago
go1.20 and golangci-lint v1.54.0 => no panic ```console /go/db1000n # $ docker run --rm -it golang:1.20-alpine sh /go # apk add -q git curl /go # curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(go env GOPATH)/bin v1.54.0 golangci/golangci-lint info checking GitHub for tag 'v1.54.0' golangci/golangci-lint info found version: 1.54.0 for v1.54.0/linux/amd64 golangci/golangci-lint info installed /go/bin/golangci-lint /go # golangci-lint version golangci-lint has version 1.54.0 built with go1.21.0 from c1d8c565 on 2023-08-09T11:50:00Z /go # go version go version go1.20.13 linux/amd64 /go # git clone --quiet https://github.com/arriven/db1000n.git && cd db1000n/ /go/db1000n # git config --global core.pager "more" /go/db1000n # git log -1 --oneline d4f0eb7 (HEAD -> main, tag: v0.9.22, origin/main, origin/HEAD) add more arm targets /go/db1000n # go mod tidy go: downloading gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b ... go: downloading github.com/kr/text v0.1.0 /go/db1000n # golangci-lint run --no-config --disable-all --enable contextcheck ```
go1.20 and golangci-lint v1.55.2 => no panic ```console $ docker run --rm -it golang:1.20-alpine sh /go # apk add -q git curl /go # curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(go env GOPATH)/bin v1.55.2 golangci/golangci-lint info checking GitHub for tag 'v1.55.2' golangci/golangci-lint info found version: 1.55.2 for v1.55.2/linux/amd64 golangci/golangci-lint info installed /go/bin/golangci-lint /go # golangci-lint version golangci-lint has version 1.55.2 built with go1.21.3 from e3c2265f on 2023-11-03T12:59:25Z /go # go version go version go1.20.13 linux/amd64 /go # git clone --quiet https://github.com/arriven/db1000n.git && cd db1000n/ /go/db1000n # git config --global core.pager "more" /go/db1000n # git log -1 --oneline d4f0eb7 (HEAD -> main, tag: v0.9.22, origin/main, origin/HEAD) add more arm targets /go/db1000n # go mod tidy go: downloading go.uber.org/zap v1.21.0 ... go: downloading github.com/kr/text v0.1.0 /go/db1000n # golangci-lint run --no-config --disable-all --enable contextcheck /go/db1000n # ```
go1.21 and golangci-lint v1.55.2 => no panic ```console $ docker run --rm -it golang:1.21-alpine sh /go # apk add -q git curl /go # curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(go env GOPATH)/bin v1.55.2 golangci/golangci-lint info checking GitHub for tag 'v1.55.2' golangci/golangci-lint info found version: 1.55.2 for v1.55.2/linux/amd64 golangci/golangci-lint info installed /go/bin/golangci-lint /go # golangci-lint version golangci-lint has version 1.55.2 built with go1.21.3 from e3c2265f on 2023-11-03T12:59:25Z /go # go version go version go1.21.6 linux/amd64 /go # git clone --quiet https://github.com/arriven/db1000n.git && cd db1000n/ /go/db1000n # git config --global core.pager "more" /go/db1000n # git log -1 --oneline d4f0eb7 (HEAD -> main, tag: v0.9.22, origin/main, origin/HEAD) add more arm targets /go/db1000n # go mod tidy go: downloading gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b ... go: downloading github.com/kr/text v0.1.0 /go/db1000n # golangci-lint run --no-config --disable-all --enable contextcheck /go/db1000n # ```

https://github.com/kkHAIKE/contextcheck/issues/4#issuecomment-1216563222