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

Can't run linter goanalysis_metalinter: buildssa: failed to load package : could not load export data: no export data for package #3939

Closed switchupcb closed 1 year ago

switchupcb commented 1 year ago

Welcome

Description of the problem

I can't run the command golangci-lint run ./... in this folder from ./_gen with GOWORK=off. This operation should lint all .go files (except those specified by skip-dirs in this configuration file.

Error

level=error msg="Running error: 1 error occurred:\n\t* can't run linter goanalysis_metalinter: buildssa: failed to load package : could not load export data: no export data for \"github.com/switchupcb/disgo/wrapper\"\n\n"

\"github.com/switchupcb/disgo/wrapper\" is only imported by the copygen folder (specified in skip-dirs).

Otherwise, the package is being loaded from outside the directory even though it isn't referenced by any of the packages that are linted.

Version of golangci-lint

```console $ golangci-lint --version # golangci-lint has version v1.53.3 built with go1.20.2 ```

Configuration file

[this configuration file](https://github.com/switchupcb/disgo/blob/v10/_gen/.golangci.yml)

Go environment

```console $ go version && go env set GO111MODULE= set GOARCH=amd64 set GOBIN= set GOCACHE=C:\Users\User\AppData\Local\go-build set GOENV=C:\Users\User\AppData\Roaming\go\env set GOEXE=.exe set GOEXPERIMENT= set GOFLAGS= set GOHOSTARCH=amd64 set GOHOSTOS=windows set GOINSECURE= set GOMODCACHE=C:\Users\User\go\pkg\mod set GOOS=windows set GOPATH=C:\Users\User\go set GOPROXY=https://proxy.golang.org,direct set GOROOT=C:\Program Files\Go set GOSUMDB=sum.golang.org set GOTMPDIR= set GOTOOLDIR=C:\Program Files\Go\pkg\tool\windows_amd64 set GOVCS= set GOVERSION=go1.20.2 set GCCGO=gccgo set GOAMD64=v1 set AR=ar set CC=gcc set CXX=g++ set CGO_ENABLED=1 set GOMOD=C:\Users\Caleb\Documents\Github\disgo\_gen\go.mod set GOWORK=off set CGO_CFLAGS=-O2 -g set CGO_CPPFLAGS= set CGO_CXXFLAGS=-O2 -g set CGO_FFLAGS=-O2 -g set CGO_LDFLAGS=-O2 -g set PKG_CONFIG=pkg-config ```

Verbose output of running

```console $ golangci-lint cache clean $ golangci-lint run -v level=info msg="[config_reader] Config search paths: [./ C:\\Users\\User\\Documents\\Github\\disgo\\_gen C:\\Users\\User\\Documents\\Github\\disgo C:\\Users\\User\\Documents\\Github C:\\Users\\User\\Documents C:\\Users\\User C:\\Users C:\\]" level=info msg="[config_reader] Used config file .golangci.yml" level=info msg="[lintersdb] Active 88 linters: [asasalint asciicheck bidichk bodyclose containedctx contextcheck cyclop decorder dogsled dupl dupword durationcheck errcheck errchkjson errname errorlint execinquery exhaustive exhaustruct exportloopref forcetypeassert funlen ginkgolinter gocheckcompilerdirectives gochecknoinits gocognit goconst gocritic gocyclo godot godox gofmt goheader goimports gomnd gomoddirectives gomodguard goprintffuncname gosec gosimple gosmopolitan govet grouper importas ineffassign interfacebloat ireturn lll loggercheck maintidx makezero mirror misspell nakedret nestif nilerr nilnil nlreturn noctx nolintlint nonamedreturns nosprintfhostport paralleltest prealloc predeclared promlinter reassign revive rowserrcheck sqlclosecheck staticcheck tagalign tagliatelle tenv testableexamples testpackage thelper tparallel typecheck unconvert unparam unused usestdlibvars varnamelen wastedassign whitespace wrapcheck zerologlint]" level=info msg="[loader] Go packages loading at mode 575 (exports_file|name|types_sizes|compiled_files|deps|files|imports) took 1.0928646s" level=info msg="[runner/filename_unadjuster] Pre-built 0 adjustments in 5.5264ms" 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 7.6722409s with top 10 stages: the_only_name: 7.083917s, wastedassign: 78.461ms, dupl: 74.9813ms, unconvert: 51.7826ms, goimports: 47.9228ms, gocritic: 44.1646ms, bidichk: 40.1671ms, buildir: 27.5617ms, contextcheck: 27.2222ms, gosec: 26.1155ms" level=warning msg="[runner] Can't run linter goanalysis_metalinter: buildssa: failed to load package : could not load export data: no export data for \"github.com/switchupcb/disgo/wrapper\"" level=info msg="[runner] processing took 0s with stages: identifier_marker: 0s, max_per_file_from_linter: 0s, source_code: 0s, sort_results: 0s, skip_files: 0s, skip_dirs: 0s, exclude-rules: 0s, path_prefixer: 0s, path_prettifier: 0s, autogenerated_exclude: 0s, nolint: 0s, uniq_by_line: 0s, diff: 0s, max_same_issues: 0s, cgo: 0s, filename_unadjuster: 0s, fixer: 0s, max_from_linter: 0s, severity-rules: 0s, exclude: 0s, path_shortener: 0s" level=info msg="[runner] linters took 6.0413727s with stages: goanalysis_metalinter: 6.0408585s" level=error msg="Running error: 1 error occurred:\n\t* can't run linter goanalysis_metalinter: buildssa: failed to load package : could not load export data: no export data for \"github.com/switchupcb/disgo/wrapper\"\n\n" level=info msg="Memory: 74 samples, avg is 69.7MB, max is 89.6MB" level=info msg="Execution took 7.2415069s" ```

Code example or link to a public repository

https://github.com/switchupcb/disgo (`_gen`)
ldez commented 1 year ago

Hello,

you have a compilation issue, to diagnose:

FYI skip-dirs just excludes the final reports but not the analysis if your code depends on this package.

ldez commented 1 year ago
$ docker run --rm -it golang:1.20-alpine sh
/go # apk add -q curl git
/go # curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(go env GOPATH)/bin v1.53.3
golangci/golangci-lint info checking GitHub for tag 'v1.53.3'
golangci/golangci-lint info found version: 1.53.3 for v1.53.3/linux/amd64
golangci/golangci-lint info installed /go/bin/golangci-lint
/go # git clone --quiet https://github.com/switchupcb/disgo.git
/go # cd disgo/_gen/
/go/disgo/_gen # go build ./...
pattern ./...: directory prefix . does not contain modules listed in go.work or their selected dependencies
/go/disgo/_gen # go build .
main module (github.com/switchupcb/disgo) does not contain package github.com/switchupcb/disgo/_gen
/go/disgo/_gen # go build ./...
pattern ./...: directory prefix . does not contain modules listed in go.work or their selected dependencies
/go/disgo/_gen # export GOWORK=off
/go/disgo/_gen # go build ./...
go: downloading golang.org/x/exp v0.0.0-20221114191408-850992195362
tools/copygen/events/setup.go:4:2: no required module provides package github.com/switchupcb/disgo/wrapper; to add it:
        go get github.com/switchupcb/disgo/wrapper
tools/copygen/events/template/generate.go:10:2: no required module provides package github.com/switchupcb/copygen/cli/models; to add it:
        go get github.com/switchupcb/copygen/cli/models
/go/disgo/_gen # 
ldez commented 1 year ago

Everything works as expected, skip-dirs just excludes the final reports but not the analysis, your code doesn't compile, so golangci-lint cannot perform the analysis.

switchupcb commented 1 year ago

Nevermind, you are attempting to build using go build ./... while I built using ./.

ldez commented 1 year ago

https://github.com/golangci/golangci-lint/issues/3939#issuecomment-1620830161

Note: it's LDEZ not IDEZ

switchupcb commented 1 year ago

How do I exclude the files from the build?

I suppose the real solution is to move the code outside of the generator.

ldez commented 1 year ago

You can use build tags.

switchupcb commented 1 year ago

Another solution is also to rename the folder to start with _ so it gets excluded from the build. So I renamed copygen to _copygen and it worked.

switchupcb commented 1 year ago

Thanks for the help!