dominikh / go-tools

Staticcheck - The advanced Go linter
https://staticcheck.dev
MIT License
6.18k stars 377 forks source link

unused linter panics if exported-is-used is set in golangci-lint #1474

Open hanzei opened 11 months ago

hanzei commented 11 months ago

Code base: https://github.com/mattermost/mattermost/tree/master/server Version:

golangci-lint --version
golangci-lint has version v1.55.2 built with go1.21.3 from (unknown, mod sum: "h1:yllEIsSJ7MtlDBwDJ9IMBkyEUz2fYE0b5B8IUgO1oP8=") on (unknown)

Config:

linters-settings:
  unused:
    exported-is-used: false

Logs:

golangci-lint run ./... -c .golangci.yml
ERRO [runner] Panic: unused: package "main" (isInitialPkg: true, needAnalyzeSource: true): object "string" has no path but also no column information: goroutine 16617 [running]:
runtime/debug.Stack()
    /usr/local/go/src/runtime/debug/stack.go:24 +0x5e
github.com/golangci/golangci-lint/pkg/golinters/goanalysis.(*action).analyzeSafe.func1()
    /home/user/go/pkg/mod/github.com/golangci/golangci-lint@v1.55.2/pkg/golinters/goanalysis/runner_action.go:109 +0x27c
panic({0x159fbe0?, 0xc031736540?})
    /usr/local/go/src/runtime/panic.go:914 +0x21f
honnef.co/go/tools/unused.(*SerializedGraph).Merge(0xc004b71680, {0xc00f3a1b00, 0x1d, 0xc03a7206e0?})
    /home/user/go/pkg/mod/honnef.co/go/tools@v0.4.6/unused/serialize.go:52 +0xdde
github.com/golangci/golangci-lint/pkg/golinters.getUnusedResults(0xc035a78410, 0x0?)
    /home/user/go/pkg/mod/github.com/golangci/golangci-lint@v1.55.2/pkg/golinters/unused.go:110 +0x1f6
github.com/golangci/golangci-lint/pkg/golinters.runUnused(0xc035a78410, 0xc0006aab78?)
    /home/user/go/pkg/mod/github.com/golangci/golangci-lint@v1.55.2/pkg/golinters/unused.go:56 +0x2a
github.com/golangci/golangci-lint/pkg/golinters.NewUnused.func1(0x161d6a0?)
    /home/user/go/pkg/mod/github.com/golangci/golangci-lint@v1.55.2/pkg/golinters/unused.go:30 +0x2d
github.com/golangci/golangci-lint/pkg/golinters/goanalysis.(*action).analyze(0xc004778440)
    /home/user/go/pkg/mod/github.com/golangci/golangci-lint@v1.55.2/pkg/golinters/goanalysis/runner_action.go:195 +0x9d6
github.com/golangci/golangci-lint/pkg/golinters/goanalysis.(*action).analyzeSafe.func2()
    /home/user/go/pkg/mod/github.com/golangci/golangci-lint@v1.55.2/pkg/golinters/goanalysis/runner_action.go:113 +0x17
github.com/golangci/golangci-lint/pkg/timeutils.(*Stopwatch).TrackStage(0xc001ce7e50, {0x179057b, 0x6}, 0xc000f4bf48)
    /home/user/go/pkg/mod/github.com/golangci/golangci-lint@v1.55.2/pkg/timeutils/stopwatch.go:111 +0x44
github.com/golangci/golangci-lint/pkg/golinters/goanalysis.(*action).analyzeSafe(0xc002779da0?)
    /home/user/go/pkg/mod/github.com/golangci/golangci-lint@v1.55.2/pkg/golinters/goanalysis/runner_action.go:112 +0x7a
github.com/golangci/golangci-lint/pkg/golinters/goanalysis.(*loadingPackage).analyze.func2(0xc004778440)
    /home/user/go/pkg/mod/github.com/golangci/golangci-lint@v1.55.2/pkg/golinters/goanalysis/runner_loadingpackage.go:80 +0xa8
created by github.com/golangci/golangci-lint/pkg/golinters/goanalysis.(*loadingPackage).analyze in goroutine 392
    /home/user/go/pkg/mod/github.com/golangci/golangci-lint@v1.55.2/pkg/golinters/goanalysis/runner_loadingpackage.go:75 +0x205 
WARN [runner] Can't run linter goanalysis_metalinter: goanalysis_metalinter: unused: package "main" (isInitialPkg: true, needAnalyzeSource: true): object "string" has no path but also no column information 
ERRO Running error: 1 error occurred:
    * can't run linter goanalysis_metalinter: goanalysis_metalinter: unused: package "main" (isInitialPkg: true, needAnalyzeSource: true): object "string" has no path but also no column information
echlebek commented 11 months ago

This isn't the golangci-lint project, so the issue should be reproduced using only staticcheck, or filed to golangci-lint.

dominikh commented 11 months ago

To clarify: the unused analyzer has a number of options to modify its behavior. These options aren't exposed in Staticcheck because they aren't fully working and not supported. golangci-lint chose to make the options available.

The bug is technically valid in that the code crashes with that option set. However, it isn't relevant to us, because we don't intend for it to be set.

hanzei commented 10 months ago

Thanks for the responses :+1:

If configuring unused.Options is not supported, should the field be unexported? That way it's clear that it's unsupported.

leonklingele commented 9 months ago

Reproducible here: https://git.helsinki.tools/leon.klingele/unusedbug

$ git clone https://git.helsinki.tools/leon.klingele/unusedbug
$ cd unusedbug
$ golangci-lint run -v ./...
level=info msg="[config_reader] Config search paths: [./ [etc]]"
level=info msg="[config_reader] Used config file .golangci.yml"
level=info msg="[lintersdb] Active 6 linters: [errcheck gosimple govet ineffassign staticcheck unused]"
level=info msg="[loader] Go packages loading at mode 575 (exports_file|files|compiled_files|deps|imports|name|types_sizes) took 15.117493ms"
level=info msg="[runner/filename_unadjuster] Pre-built 0 adjustments in 141.103µs"
level=info msg="[linters_context/goanalysis] analyzers took 8.535794ms with top 10 stages: ctrlflow: 1.363773ms, printf: 1.314935ms, buildir: 1.033822ms, fact_deprecated: 1.004305ms, fact_purity: 496.049µs, SA4014: 490.846µs, SA5012: 306.318µs, nilness: 287.386µs, typedness: 237.783µs, SA1015: 170.414µs"
level=error msg="[runner] Panic: unused: package \"migrations\" (isInitialPkg: true, needAnalyzeSource: true): object \"error\" has no path but also no column information: goroutine 293 [running]:\nruntime/debug.Stack()\n\truntime/debug/stack.go:24 +0x5e\ngithub.com/golangci/golangci-lint/pkg/golinters/goanalysis.(*action).analyzeSafe.func1()\n\tgithub.com/golangci/golangci-lint/pkg/golinters/goanalysis/runner_action.go:109 +0x27c\npanic({0x1609ce0?, 0xc000ed6300?})\n\truntime/panic.go:914 +0x21f\nhonnef.co/go/tools/unused.(*SerializedGraph).Merge(0xc000ee9680, {0xc000eea000, 0x5, 0xc000749598?})\n\thonnef.co/go/tools@v0.4.6/unused/serialize.go:52 +0xdde\ngithub.com/golangci/golangci-lint/pkg/golinters.getUnusedResults(0xc000ed5520, 0x96bb05?)\n\tgithub.com/golangci/golangci-lint/pkg/golinters/unused.go:110 +0x1f6\ngithub.com/golangci/golangci-lint/pkg/golinters.runUnused(0xc000ed5520, 0xc000836b78?)\n\tgithub.com/golangci/golangci-lint/pkg/golinters/unused.go:56 +0x2a\ngithub.com/golangci/golangci-lint/pkg/golinters.NewUnused.func1(0x1687b40?)\n\tgithub.com/golangci/golangci-lint/pkg/golinters/unused.go:30 +0x2d\ngithub.com/golangci/golangci-lint/pkg/golinters/goanalysis.(*action).analyze(0xc000fdff10)\n\tgithub.com/golangci/golangci-lint/pkg/golinters/goanalysis/runner_action.go:195 +0x9d6\ngithub.com/golangci/golangci-lint/pkg/golinters/goanalysis.(*action).analyzeSafe.func2()\n\tgithub.com/golangci/golangci-lint/pkg/golinters/goanalysis/runner_action.go:113 +0x17\ngithub.com/golangci/golangci-lint/pkg/timeutils.(*Stopwatch).TrackStage(0xc000fb03c0, {0x17f69db, 0x6}, 0xc00105f748)\n\tgithub.com/golangci/golangci-lint/pkg/timeutils/stopwatch.go:111 +0x44\ngithub.com/golangci/golangci-lint/pkg/golinters/goanalysis.(*action).analyzeSafe(0x0?)\n\tgithub.com/golangci/golangci-lint/pkg/golinters/goanalysis/runner_action.go:112 +0x7a\ngithub.com/golangci/golangci-lint/pkg/golinters/goanalysis.(*loadingPackage).analyze.func2(0xc000fdff10)\n\tgithub.com/golangci/golangci-lint/pkg/golinters/goanalysis/runner_loadingpackage.go:80 +0xa8\ncreated by github.com/golangci/golangci-lint/pkg/golinters/goanalysis.(*loadingPackage).analyze in goroutine 97\n\tgithub.com/golangci/golangci-lint/pkg/golinters/goanalysis/runner_loadingpackage.go:75 +0x205\n"
level=warning msg="[runner] Can't run linter goanalysis_metalinter: goanalysis_metalinter: unused: package \"migrations\" (isInitialPkg: true, needAnalyzeSource: true): object \"error\" has no path but also no column information"
level=info msg="[runner] processing took 3.488µs with stages: max_same_issues: 557ns, skip_dirs: 408ns, nolint: 277ns, max_per_file_from_linter: 162ns, uniq_by_line: 162ns, cgo: 146ns, max_from_linter: 145ns, severity-rules: 145ns, autogenerated_exclude: 142ns, path_prettifier: 139ns, exclude: 137ns, skip_files: 137ns, filename_unadjuster: 136ns, source_code: 134ns, exclude-rules: 127ns, fixer: 122ns, identifier_marker: 117ns, path_shortener: 79ns, diff: 79ns, sort_results: 75ns, path_prefixer: 62ns"
level=info msg="[runner] linters took 4.866146ms with stages: goanalysis_metalinter: 4.754298ms"
level=error msg="Running error: 1 error occurred:\n\t* can't run linter goanalysis_metalinter: goanalysis_metalinter: unused: package \"migrations\" (isInitialPkg: true, needAnalyzeSource: true): object \"error\" has no path but also no column information\n\n"
level=info msg="Memory: 2 samples, avg is 24.3MB, max is 26.3MB"
level=info msg="Execution took 27.720611ms"
leonklingele commented 9 months ago

Found this to be a dup of https://github.com/dominikh/go-tools/issues/1385.