golangci-lint icon indicating copy to clipboard operation
golangci-lint copied to clipboard

unused doesn't work with "exported-is-used" and "exported-fields-are-used" linter settings

Open pietdevries94 opened this issue 1 year ago • 8 comments

Welcome

  • [X] Yes, I'm using a binary release within 2 latest major releases. Only such installations are supported.
  • [X] Yes, I've searched similar issues on GitHub and didn't find any.
  • [X] Yes, I've read the typecheck section of the FAQ (https://golangci-lint.run/usage/faq/#why-do-you-have-typecheck-errors).
  • [X] Yes, I've tried with the standalone linter if available (e.g., gocritic, go vet, etc.). (https://golangci-lint.run/usage/linters/)

Description of the problem

When using unused with either "exported-is-used" or "exported-fields-are-used" linter settings or both, golangci-lint panics.

I have both tried in go 1.20 and 1.21

Version of golangci-lint

$ golangci-lint --version
golangci-lint has version 1.55.2 built with go1.21.3 from e3c2265f on 2023-11-03T12:59:25Z

Configuration

linters:
  enable:
    - unused
linters-settings:
  unused:
    exported-is-used: false
    exported-fields-are-used: false

Go environment

$ go version && go env
go version go1.20.8 linux/amd64
GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/home/piet/.cache/go-build"
GOENV="/home/piet/.config/go/env"
GOEXE=""
GOEXPERIMENT=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=""
GOMODCACHE="/home/piet/go/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/home/piet/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/nix/store/cbfw4zvybzvhvqb14a14pwpsvcnnpjcg-go-1.20.8/share/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/nix/store/cbfw4zvybzvhvqb14a14pwpsvcnnpjcg-go-1.20.8/share/go/pkg/tool/linux_amd64"
GOVCS=""
GOVERSION="go1.20.8"
GCCGO="gccgo"
GOAMD64="v1"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD="/tmp/tmp.PJ3iHlw0ZJ/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 -fdebug-prefix-map=/run/user/1000/go-build3312085719=/tmp/go-build -gno-record-gcc-switches"

Verbose output of running

$ golangci-lint cache clean
$ golangci-lint run -v
INFO [config_reader] Config search paths: [./ /tmp/tmp.PJ3iHlw0ZJ /tmp / /home/piet] 
INFO [config_reader] Used config file .golangci.yml 
INFO [lintersdb] Active 6 linters: [errcheck gosimple govet ineffassign staticcheck unused] 
INFO [loader] Go packages loading at mode 575 (exports_file|types_sizes|compiled_files|deps|files|imports|name) took 49.598673ms 
INFO [runner/filename_unadjuster] Pre-built 0 adjustments in 94.55µs 
INFO [linters_context/goanalysis] analyzers took 779.556298ms with top 10 stages: buildir: 616.212553ms, inspect: 37.842009ms, SA5012: 22.853563ms, printf: 20.040602ms, ctrlflow: 19.631335ms, fact_deprecated: 18.408188ms, fact_purity: 16.0611ms, nilness: 12.353816ms, typedness: 10.489409ms, S1005: 190.001µs 
ERRO [runner] Panic: unused: package "something" (isInitialPkg: true, needAnalyzeSource: true): object "string" has no path but also no column information: goroutine 386 [running]:
runtime/debug.Stack()
        runtime/debug/stack.go:24 +0x5e
github.com/golangci/golangci-lint/pkg/golinters/goanalysis.(*action).analyzeSafe.func1()
        github.com/golangci/golangci-lint/pkg/golinters/goanalysis/runner_action.go:109 +0x27c
panic({0xf8a380?, 0xc0009cc330?})
        runtime/panic.go:914 +0x21f
honnef.co/go/tools/unused.(*SerializedGraph).Merge(0xc001489680, {0xc0001c0a80, 0x4, 0xc0001b5308?})
        honnef.co/go/[email protected]/unused/serialize.go:52 +0xdde
github.com/golangci/golangci-lint/pkg/golinters.getUnusedResults(0xc000f5e5b0, 0x2070696b7320646c?)
        github.com/golangci/golangci-lint/pkg/golinters/unused.go:110 +0x1f6
github.com/golangci/golangci-lint/pkg/golinters.runUnused(0xc000f5e5b0, 0xc0007e8b78?)
        github.com/golangci/golangci-lint/pkg/golinters/unused.go:56 +0x2a
github.com/golangci/golangci-lint/pkg/golinters.NewUnused.func1(0xfd94e0?)
        github.com/golangci/golangci-lint/pkg/golinters/unused.go:30 +0x2d
github.com/golangci/golangci-lint/pkg/golinters/goanalysis.(*action).analyze(0xc000ea4db0)
        github.com/golangci/golangci-lint/pkg/golinters/goanalysis/runner_action.go:195 +0x9d6
github.com/golangci/golangci-lint/pkg/golinters/goanalysis.(*action).analyzeSafe.func2()
        github.com/golangci/golangci-lint/pkg/golinters/goanalysis/runner_action.go:113 +0x17
github.com/golangci/golangci-lint/pkg/timeutils.(*Stopwatch).TrackStage(0xc0014108c0, {0x112dc5e, 0x6}, 0xc000f88748)
        github.com/golangci/golangci-lint/pkg/timeutils/stopwatch.go:111 +0x44
github.com/golangci/golangci-lint/pkg/golinters/goanalysis.(*action).analyzeSafe(0x7d0a7d7b65756575?)
        github.com/golangci/golangci-lint/pkg/golinters/goanalysis/runner_action.go:112 +0x7a
github.com/golangci/golangci-lint/pkg/golinters/goanalysis.(*loadingPackage).analyze.func2(0xc000ea4db0)
        github.com/golangci/golangci-lint/pkg/golinters/goanalysis/runner_loadingpackage.go:80 +0xa8
created by github.com/golangci/golangci-lint/pkg/golinters/goanalysis.(*loadingPackage).analyze in goroutine 142
        github.com/golangci/golangci-lint/pkg/golinters/goanalysis/runner_loadingpackage.go:75 +0x205 
WARN [runner] Can't run linter goanalysis_metalinter: goanalysis_metalinter: unused: package "something" (isInitialPkg: true, needAnalyzeSource: true): object "string" has no path but also no column information 
INFO [runner] processing took 2.764µs with stages: skip_dirs: 531ns, max_same_issues: 521ns, nolint: 201ns, autogenerated_exclude: 150ns, cgo: 140ns, path_prettifier: 130ns, filename_unadjuster: 130ns, max_from_linter: 130ns, skip_files: 121ns, identifier_marker: 120ns, source_code: 120ns, fixer: 110ns, diff: 50ns, exclude-rules: 50ns, path_shortener: 40ns, path_prefixer: 40ns, sort_results: 40ns, severity-rules: 40ns, exclude: 40ns, uniq_by_line: 30ns, max_per_file_from_linter: 30ns 
INFO [runner] linters took 678.204076ms with stages: goanalysis_metalinter: 678.174341ms 
ERRO Running error: 1 error occurred:
        * can't run linter goanalysis_metalinter: goanalysis_metalinter: unused: package "something" (isInitialPkg: true, needAnalyzeSource: true): object "string" has no path but also no column information
 
INFO Memory: 9 samples, avg is 154.4MB, max is 232.3MB 
INFO Execution took 733.799125ms

A minimal reproducible example or link to a public repository

https://github.com/pietdevries94/golangci-lint-unused-reproduction

Validation

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

pietdevries94 avatar Nov 22 '23 08:11 pietdevries94

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

boring-cyborg[bot] avatar Nov 22 '23 08:11 boring-cyborg[bot]

Hello,

sadly we cannot filter the elements that produce the panic because it's related to unexported fields, so I think we will remove the options exported-is-used and exported-fields-are-used.

ldez avatar Nov 22 '23 14:11 ldez

That’s a shame… I would love to properly detect dead code in a rather large repository with no external dependents, so exported types should be imported elsewhere. Nevertheless, if it’s not feasible to fix this, I’ll have to search further

pietdevries94 avatar Nov 22 '23 14:11 pietdevries94

I've been able to use exported-fields-are-used, but I saw the same error with exported-is-used. I want to try to dig in further and see what's going on, since this is the main linting functionality that is only present on Goland.

ZackaryWelch avatar Dec 04 '23 22:12 ZackaryWelch

Looks like problem in dependency (honnef.co/go/tools). It looks like it was not ready for some values.

I enabled exported-is-used: false in golangci-lint repository and hit run with debuger. I got panic on exitcodes.go validation. Iota node has no path and position, but has id.

Screenshot 2024-01-09 at 23 17 09 Screenshot 2024-01-09 at 23 17 17

rusinikita avatar Jan 09 '24 15:01 rusinikita

https://github.com/golangci/golangci-lint/issues/4218#issuecomment-1822837072

ldez avatar Jan 09 '24 16:01 ldez

exported-fields-are-used works fine for me on multiple projects, exported-is-used however doesn't. Probably best to remove that one option from golangci for the time being.

Also see https://github.com/dominikh/go-tools/issues/1474 and https://github.com/dominikh/go-tools/issues/1385.

leonklingele avatar Jan 17 '24 13:01 leonklingele

I came across this today as well. I recently found this tool. That does mark exported code as unused whenever it is.

See also this blog post about it.

wipsel avatar Jan 18 '24 10:01 wipsel

Also ran into this issue. This is the setting that I was most looking forward to 😢. Seems like there hasn't been any movement here?

noahschumacher avatar Jul 31 '24 14:07 noahschumacher