golangci / golangci-lint

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

skip-dirs works with some linters and not with others #913

Open tylerje opened 4 years ago

tylerje commented 4 years ago

Thank you for creating the issue!

Please include the following information:

Version of golangci-lint ```console $ golangci-lint version golangci-lint has version 1.22.2 built from cb2f8ba on 2019-12-30T19:26:28Z``` ```
Config file ```console $ cat .golangci.yml linters: # please, do not use `enable-all`: it's deprecated and will be removed soon. # inverted configuration with `enable-all` and `disable` is not scalable during updates of golangci-lint disable-all: true enable: #notskip- bodyclose #notskip- deadcode #notskip- depguard #- dogsled #- dupl #notskip- errcheck #- funlen #- gochecknoinits #- goconst #notskip- gocritic #- gocyclo #- gofmt #- goimports #- golint #notskip- gosec #notskip- gosimple #notskip- govet #- ineffassign #notskip- interfacer #- lll #- misspell #- nakedret #- scopelint #notskip- staticcheck #notskip- structcheck #notskip- stylecheck #- typecheck - unconvert #notskip- unparam #notskip- unused #notskip- varcheck #- whitespace # don't enable: #- gochecknoglobals #- gocognit #- godox #notskip- maligned #- prealloc run: skip-dirs: - api - vendor ```
Go environment ```console $ go version && go env GOHOSTARCH="amd64" GOHOSTOS="linux" GOOS="linux" GOPATH="/repo/go:/k10/go" GOPROXY="" GORACE="" GOROOT="/usr/local/go" GOTMPDIR="" GOTOOLDIR="/usr/local/go/pkg/tool/linux_amd64" GCCGO="gccgo" CC="gcc" CXX="g++" CGO_ENABLED="1" GOMOD="/repo/go.mod" 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-build302036891=/tmp/go-build -gno-record-gcc-switches" ```
Verbose output of running ```console $ golangci-lint run -v INFO [config_reader] Config search paths: [./ /repo/go/src /repo/go /repo /] INFO [config_reader] Used config file ../../.golangci.yml INFO [lintersdb] Active 1 linters: [unconvert] INFO [lintersdb] Active 1 linters: [unconvert] INFO [loader] Go packages loading at mode 575 (deps|types_sizes|compiled_files|exports_file|files|imports|name) took 2.72833563s INFO [runner/filename_unadjuster] Pre-built 0 adjustments in 76.405679ms INFO [runner/unconvert/goanalysis] analyzers took 85.820662ms with top 10 stages: unconvert: 85.820662ms WARN [runner] Can't run linter unconvert: unconvert: analysis skipped: errors in package: [/repo/go/src/api/.../run.go:57:19: client.Actions undefined (type client.Interface has no field or method Actions) /repo/go/src/api/.../run.go:51:19: client.Actions undefined (type client.Interface has no field or method Actions) /repo/go/src/api/.../retire.go:57:19: client.Actions undefined (type client.Interface has no field or method Actions) /repo/go/src/api/.../retire.go:51:19: client.Actions undefined (type client.Interface has no field or method Actions) /repo/go/src/api/.../restore.go:58:19: client.Actions undefined (type client.Interface has no field or method Actions) /repo/go/src/api/.../restore.go:52:19: client.Actions undefined (type client.Interface has no field or method Actions) /repo/go/src/api/.../import.go:58:19: client.Actions undefined (type client.Interface has no field or method Actions) /repo/go/src/api/.../import.go:52:19: client.Actions undefined (type client.Interface has no field or method Actions) /repo/go/src/api/.../export.go:58:19: client.Actions undefined (type client.Interface has no field or method Actions) /repo/go/src/api/.../export.go:52:19: client.Actions undefined (type client.Interface has no field or method Actions) /repo/go/src/api/.../backup.go:58:19: client.Actions undefined (type client.Interface has no field or method Actions) /repo/go/src/api/.../backup.go:52:19: client.Actions undefined (type client.Interface has no field or method Actions)] INFO [runner] processing took 2.531µs with stages: max_same_issues: 558ns, skip_dirs: 288ns, nolint: 253ns, max_from_linter: 237ns, cgo: 198ns, path_prettifier: 108ns, autogenerated_exclude: 107ns, skip_files: 104ns, filename_unadjuster: 102ns, exclude-rules: 101ns, max_per_file_from_linter: 100ns, diff: 99ns, identifier_marker: 99ns, uniq_by_line: 48ns, exclude: 46ns, source_code: 42ns, path_shortener: 41ns INFO [runner] linters took 1.729615347s with stages: unconvert: 1.729587635s ERRO Running error: unconvert: analysis skipped: errors in package: [/repo/go/src/api/.../run.go:57:19: client.Actions undefined (type client.Interface has no field or method Actions) /repo/go/src/api/.../run.go:51:19: client.Actions undefined (type client.Interface has no field or method Actions) /repo/go/src/api/.../retire.go:57:19: client.Actions undefined (type client.Interface has no field or method Actions) /repo/go/src/api/.../retire.go:51:19: client.Actions undefined (type client.Interface has no field or method Actions) /repo/go/src/api/.../restore.go:58:19: client.Actions undefined (type client.Interface has no field or method Actions) /repo/go/src/api/.../restore.go:52:19: client.Actions undefined (type client.Interface has no field or method Actions) /repo/go/src/api/.../import.go:58:19: client.Actions undefined (type client.Interface has no field or method Actions) /repo/go/src/api/.../import.go:52:19: client.Actions undefined (type client.Interface has no field or method Actions) /repo/go/src/api/.../export.go:58:19: client.Actions undefined (type client.Interface has no field or method Actions) /repo/go/src/api/.../export.go:52:19: client.Actions undefined (type client.Interface has no field or method Actions) /repo/go/src/api/.../backup.go:58:19: client.Actions undefined (type client.Interface has no field or method Actions) /repo/go/src/api/.../backup.go:52:19: client.Actions undefined (type client.Interface has no field or method Actions)] INFO Memory: 47 samples, avg is 176.6MB, max is 404.3MB INFO Execution took 4.545652859s ```
Linters that adhere to skip-dirs ```console dogsled dupl funlen gochecknoinits goconst gocyclo gofmt goimports golint ineffassign lll misspell nakedret scopelint typecheck whitespace gochecknoglobals gocognit godox prealloc ```
Linters that ignore to skip-dirs ```console bodyclose deadcode depguard errcheck gocritic gosec gosimple govet interfacer staticcheck structcheck stylecheck unconvert unparam unused varcheck maligned ```

Perhaps that there is a reason that skip-dirs is ignored with some linters and a non-zero value is returned for errors in those directories, but I cannot seem to find one. Any help would be very welcome.

ernado commented 4 years ago

I think that it is not related to skip-dirs and just another version of #885 Currently we are working on a proper fix, but workaround is described in #885: you can just disable "unused" linter or use earlier version of golangci.

tylerje commented 4 years ago

I'm not sure that would be the case. It happens when I run only one linter at a time. I've tried each of them individually.

ernado commented 4 years ago

Thank you for input, you are right. Probably it is because some linters require types info to begin analysis, and skip-dirs does not exclude packages from parsing and analysis, just ignores the issues for files from that dirs.

tylerje commented 4 years ago

If it just ignored issues for those and returned a 0 instead of a non-zero result, that would be fine, but even when the only issue that is found is in one of those dirs, a non-zero result is returned.

tylerje commented 4 years ago

Through further testing (including tweaking the skip-dirs values and changing default concurrency to 8, I have been able to get all linters except the following 9 in the repo's current .golangci.yml file to work:

bodyclose
gomnd
gosimple
govet
interfacer
staticcheck
stylecheck
unparam
unused
JeanMertz commented 4 years ago

whitespace is another that can cause this issue.

golangci-lint run --no-config --disable-all --skip-dirs pkg/simpletemplate -E whitespace

WARN [runner] Can't run linter whitespace: whitespace: failed to get line <snip>/pkg/simpletemplate/yaccpar:329: failed to get file <snip>/pkg/simpletemplate/yaccpar lines cache: can't get file <snip>/pkg/simpletemplate/yaccpar bytes from cache: can't read file <snip>/pkg/simpletemplate/yaccpar: open <snip>/pkg/simpletemplate/yaccpar: no such file or directory 
ERRO Running error: whitespace: failed to get line <snip>/pkg/simpletemplate/yaccpar:329: failed to get file <snip>/pkg/simpletemplate/yaccpar lines cache: can't get file <snip>/pkg/simpletemplate/yaccpar bytes from cache: can't read file <snip>/pkg/simpletemplate/yaccpar: open <snip>/pkg/simpletemplate/yaccpar: no such file or directory

This error doesn't happen f.e. when swapping whitespace for govet.

Shpota commented 4 years ago

Do you have any progress on this issue?

In my case, this bug blocks the usage golangci-lint completely (at least I haven't found any workaround yet). I have a folder that contains code templates, some of them are .go files. They can not (and should not) be compiled. However, the linter somehow tries to compile them and fails with the error:

level=error msg="Running error: buildir: failed to load package : could not load export data: no export data for \"project-name/db\""
ghost commented 3 years ago

1466 could be linked to this as well, maybe?

kipply commented 3 years ago

I too am experiencing this problem 😞

sumersao commented 3 years ago

I am also experiencing this issue. I am using golangci-lint version 1.36, and it's completely unusable. I've tried commenting out each linter, but none of them work bc they give the warnings for auto_generated files.

davidsteed commented 3 years ago

I agree this is completly unusable with this bug

4k1k0 commented 3 years ago

Any update about this?

I'm using golangci v1.42.0 on macoos.

$ cat .golangci.yml
run:
  skip-dirs:
    - node_modules
    - internal/mocks
linters:
  enable:
    - deadcode
    - dupl
    - errcheck
    - funlen
    - goconst
    - godot
    - gofmt
    - gomnd
    - gosimple
    - govet
    - ifshort
    - ineffassign
    - predeclared
    - structcheck
    - thelper
    - typecheck
    - unparam
    - unused
    - varcheck
    - wastedassign
    - whitespace

linters-settings:
  funlen:
    lines: 70
    statements: 40
  godot:
    scope: toplevel
    capital: true
  gofmt:
    simplify: true
  gomnd:
    settings:
      mnd:
        checks: argument,case,condition,operation,return,assign
  ifshort:
    max-decl-lines: 1
    max-decl-chars: 30
  unparam:
    check-exported: false

I'm getting the following error running the linter:

$ golangci run --fast
WARN [runner] Can't run linter goanalysis_metalinter: buildir: failed to load package : could not load export data: no export data for "github.com/fnproject/fdk-go" 

I currently have these go file inside the node_modules directory

$ find node_modules -type f -name "*go"
node_modules/serverless/lib/plugins/create/templates/aws-go/world/main.go
node_modules/serverless/lib/plugins/create/templates/aws-go/hello/main.go
node_modules/serverless/lib/plugins/create/templates/google-go/fn_test.go
node_modules/serverless/lib/plugins/create/templates/google-go/fn.go
node_modules/serverless/lib/plugins/create/templates/aws-go-mod/world/main.go
node_modules/serverless/lib/plugins/create/templates/aws-go-mod/hello/main.go
node_modules/serverless/lib/plugins/create/templates/aws-go-dep/world/main.go
node_modules/serverless/lib/plugins/create/templates/aws-go-dep/hello/main.go
node_modules/serverless/lib/plugins/create/templates/tencent-go/index.go
node_modules/serverless/lib/plugins/create/templates/fn-go/hello/func.go

The one with the problem seems to be File: node_modules/serverless/lib/plugins/create/templates/fn-go/hello/func.go

Here's the complete output with -v flag.

INFO [config_reader] Config search paths: [./ /Users/wako/Documents/Code/Golang/src/github.com/company/project /Users/wako/Documents/Code/Golang/src/github.com/company /Users/wako/Documents/Code/Golang/src/github.com /Users/wako/Documents/Code/Golang/src /Users/wako/Documents/Code/Golang /Users/wako/Documents/Code /Users/wako/Documents /Users/wako /Users /] 
INFO [config_reader] Used config file .golangci.yml 
INFO [lintersdb] Active 22 linters: [deadcode dupl errcheck funlen goconst godot gofmt gomnd gosimple govet ifshort ineffassign predeclared staticcheck structcheck thelper typecheck unparam unused varcheck wastedassign whitespace] 
INFO [loader] Go packages loading at mode 575 (types_sizes|exports_file|imports|name|compiled_files|deps|files) took 996.763475ms 
INFO [runner/filename_unadjuster] Pre-built 0 adjustments in 1.149501ms 
INFO [linters context/goanalysis] analyzers took 2.223137064s with top 10 stages: buildir: 396.122445ms, buildssa: 329.0585ms, wastedassign: 326.451109ms, dupl: 103.879595ms, gofmt: 83.170988ms, S1039: 43.565525ms, unparam: 29.706385ms, S1003: 29.653647ms, printf: 27.561202ms, SA1004: 26.484876ms 
WARN [runner] Can't run linter goanalysis_metalinter: buildir: failed to load package : could not load export data: no export data for "github.com/fnproject/fdk-go" 
INFO [runner] processing took 2.697µs with stages: max_same_issues: 447ns, skip_dirs: 270ns, cgo: 219ns, max_from_linter: 177ns, nolint: 168ns, skip_files: 135ns, diff: 128ns, uniq_by_line: 127ns, autogenerated_exclude: 127ns, filename_unadjuster: 125ns, path_prettifier: 121ns, identifier_marker: 121ns, source_code: 117ns, exclude: 62ns, exclude-rules: 62ns, path_shortener: 60ns, sort_results: 59ns, max_per_file_from_linter: 59ns, severity-rules: 57ns, path_prefixer: 56ns 
INFO [runner] linters took 467.311453ms with stages: goanalysis_metalinter: 467.27213ms 
INFO File cache stats: 0 entries of total size 0B 
INFO Memory: 16 samples, avg is 118.1MB, max is 339.3MB 
INFO Execution took 1.477032322s    

And my goenv

GO111MODULE=""
GOARCH="amd64"
GOBIN="/Users/wako/Documents/Code/Golang/bin"
GOCACHE="/Users/wako/Library/Caches/go-build"
GOENV="/Users/wako/Library/Application Support/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOINSECURE=""
GOMODCACHE="/Users/wako/Documents/Code/Golang/pkg/mod"
GONOPROXY="github.com/company"
GONOSUMDB="github.com/company"
GOOS="darwin"
GOPATH="/Users/wako/Documents/Code/Golang"
GOPRIVATE="github.com/company"
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/local/Cellar/go/1.16.6/libexec"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/Cellar/go/1.16.6/libexec/pkg/tool/darwin_amd64"
GOVCS=""
GOVERSION="go1.16.6"
GCCGO="gccgo"
AR="ar"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
GOMOD="/dev/null"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -arch x86_64 -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/4f/3wtb18qs50gfvrsvy2wt347h0000gn/T/go-build425526132=/tmp/go-build -gno-record-gcc-switches -fno-common"
stale[bot] commented 1 year ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

emmaLP commented 1 year ago

I'm experiencing this issue with go files exising in node_modules folder.

Golangci-lint version 1.52.2 Go Version 1.19

.golangci.yml

run:
  timeout: 5m
  skip-dirs:
    - node_modules

When I run golangci-lint run ./.., go dependencies in node_module folder still getting picked up: image

We also have a folder that has generated code we want to ignore and the skip-dirs with that folder is ignored as well.

Any solutions found for this issue?

4k1k0 commented 1 year ago

@emmaLP is that the whole error message? I had a similar problem but the whole error message included info about a file and some timezones. This helped: #3087 https://github.com/golangci/golangci-lint/issues/3087#issuecomment-1226508818

emmaLP commented 1 year ago

@4k1k0 that was the whole error message.