Skip to content

Running golangci-lint against single file fails with undeclared name error #1574

@jarv

Description

@jarv

I'm using syntastic/vim with which calls golangci-lint 1.33.0 against a single file, this error is pretty simple to reproduce so I'm wondering if golangci-lint is not meant to be invoked this way or there is something else wrong with configuration.

To reproduce:

mkdir hello; cd hello
go mod init example.com/hello

hello.go:

package hello

func Hello() error {
	return nil
}

hello_test.go:

package hello

import "testing"

func TestHello(t *testing.T) {
	Hello()
}

Running golangci-lint against hello_test.go fails with undeclared name: Hello

$ golangci-lint  run -v hello_test.go
 $ golangci-lint  run -v hello_test.go
INFO [config_reader] Config search paths: [./ /Users/jarv/src/hello /Users/jarv/src /Users/jarv /Users /]
INFO [lintersdb] Active 10 linters: [deadcode errcheck gosimple govet ineffassign staticcheck structcheck typecheck unused varcheck]
INFO [loader] Go packages loading at mode 575 (name|deps|exports_file|files|imports|types_sizes|compiled_files) took 522.147221ms
INFO [runner/filename_unadjuster] Pre-built 0 adjustments in 243.409µs
INFO [linters context/goanalysis] analyzers took 2.96162ms with top 10 stages: buildir: 359.267µs, fact_deprecated: 306.248µs, SA4019: 84.881µs, S1020: 56.233µs, S1008: 48.686µs, SA4016: 47.08µs, SA3000: 42.664µs, tests: 41.033µs, S1010: 40.398µs, SA2001: 38.19µs
WARN [runner] Can't run linter goanalysis_metalinter: S1038: failed prerequisites: [(inspect@command-line-arguments [command-line-arguments.test], isgenerated@command-line-arguments [command-line-arguments.test]): analysis skipped: errors in package: [/Users/jarv/src/hello/hello_test.go:7:9: undeclared name: Hello]]
INFO [linters context/goanalysis] analyzers took 841.544µs with top 10 stages: buildir: 734.78µs, U1000: 106.764µs
WARN [runner] Can't run linter unused: buildir: analysis skipped: errors in package: [/Users/jarv/src/hello/hello_test.go:7:9: undeclared name: Hello]
INFO [runner] processing took 4.092µs with stages: max_same_issues: 1.798µs, skip_dirs: 235ns, max_from_linter: 214ns, nolint: 186ns, diff: 178ns, cgo: 173ns, skip_files: 150ns, identifier_marker: 147ns, path_prettifier: 145ns, filename_unadjuster: 144ns, uniq_by_line: 143ns, autogenerated_exclude: 140ns, sort_results: 66ns, path_shortener: 59ns, path_prefixer: 57ns, severity-rules: 56ns, max_per_file_from_linter: 53ns, exclude: 51ns, source_code: 50ns, exclude-rules: 47ns
INFO [runner] linters took 75.04981ms with stages: goanalysis_metalinter: 51.814907ms, unused: 23.184805ms
ERRO Running error: buildir: analysis skipped: errors in package: [/Users/jarv/src/hello/hello_test.go:7:9: undeclared name: Hello]
INFO Memory: 9 samples, avg is 71.9MB, max is 72.3MB
INFO Execution took 785.630035ms

But running it on the directory is fine:

$ golangci-lint  run -v .
INFO [config_reader] Config search paths: [./ /Users/jarv/src/hello /Users/jarv/src /Users/jarv /Users /]
INFO [lintersdb] Active 10 linters: [deadcode errcheck gosimple govet ineffassign staticcheck structcheck typecheck unused varcheck]
INFO [loader] Go packages loading at mode 575 (compiled_files|files|imports|name|types_sizes|deps|exports_file) took 519.928249ms
INFO [runner/filename_unadjuster] Pre-built 0 adjustments in 190.156µs
INFO [linters context/goanalysis] analyzers took 0s with no stages
INFO [linters context/goanalysis] analyzers took 0s with no stages
INFO [runner] Processors filtering stat (out/in): identifier_marker: 1/1, nolint: 1/1, source_code: 1/1, severity-rules: 1/1, path_prefixer: 1/1, sort_results: 1/1, skip_files: 1/1, skip_dirs: 1/1, autogenerated_exclude: 1/1, exclude: 1/1, max_same_issues: 1/1, path_shortener: 1/1, cgo: 1/1, path_prettifier: 1/1, diff: 1/1, filename_unadjuster: 1/1, uniq_by_line: 1/1, max_per_file_from_linter: 1/1, max_from_linter: 1/1, exclude-rules: 1/1
INFO [runner] processing took 208.161µs with stages: nolint: 39.03µs, autogenerated_exclude: 36.473µs, path_prettifier: 32.922µs, identifier_marker: 31.792µs, source_code: 26.606µs, exclude-rules: 23.907µs, skip_dirs: 8.32µs, filename_unadjuster: 2.025µs, max_same_issues: 1.339µs, uniq_by_line: 1.243µs, cgo: 1.006µs, max_from_linter: 928ns, path_shortener: 862ns, max_per_file_from_linter: 321ns, skip_files: 295ns, severity-rules: 268ns, diff: 250ns, exclude: 230ns, sort_results: 224ns, path_prefixer: 120ns
INFO [runner] linters took 22.723433ms with stages: goanalysis_metalinter: 21.862432ms, unused: 550.292µs
hello_test.go:6:7: Error return value of `Hello` is not checked (errcheck)
	Hello()
	     ^
INFO File cache stats: 1 entries of total size 75B
INFO Memory: 9 samples, avg is 71.9MB, max is 72.3MB
INFO Execution took 732.694796ms

Thank you for creating the issue!

  • Yes, I'm using a binary release within 2 latest major releases. Only such installations are supported.
  • Yes, I've searched similar issues on GitHub and didn't find any.
  • Yes, I've included all information below (version, config, etc).

Please include the following information:

Version of golangci-lint
$ golangci-lint --version
golangci-lint has version 1.33.0 built from b90551c on 2020-11-23T05:15:36Z
Config file
$ cat .golangci.yml
# paste output here
Go environment
$ go version && go env
go version go1.14.6 darwin/amd64
GO111MODULE="on"
GOARCH="amd64"
GOBIN=""
GOCACHE="/Users/jarv/Library/Caches/go-build"
GOENV="/Users/jarv/Library/Application Support/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOINSECURE=""
GONOPROXY=""
GONOSUMDB=""
GOOS="darwin"
GOPATH="/Users/jarv/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/Users/jarv/.asdf/installs/golang/1.14.6/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/Users/jarv/.asdf/installs/golang/1.14.6/go/pkg/tool/darwin_amd64"
GCCGO="gccgo"
AR="ar"
CC="clang"
CXX="clang++"
CGO_ENABLED="0"
GOMOD="/Users/jarv/src/hello/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 -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/wd/n_zjv6p14txgjgm15twd7rgr0000gp/T/go-build154262324=/tmp/go-build -gno-record-gcc-switches -fno-common"
Verbose output of running
$ golangci-lint cache clean
$ golangci-lint run -v
INFO [config_reader] Config search paths: [./ /Users/jarv/src/hello /Users/jarv/src /Users/jarv /Users /]
INFO [lintersdb] Active 10 linters: [deadcode errcheck gosimple govet ineffassign staticcheck structcheck typecheck unused varcheck]
INFO [loader] Go packages loading at mode 575 (deps|files|name|types_sizes|compiled_files|exports_file|imports) took 526.849067ms
INFO [runner/filename_unadjuster] Pre-built 0 adjustments in 199.933µs
INFO [linters context/goanalysis] analyzers took 808.421527ms with top 10 stages: buildir: 632.371327ms, fact_purity: 46.066411ms, inspect: 40.169017ms, ctrlflow: 37.389512ms, printf: 27.350795ms, fact_deprecated: 23.368568ms, ineffassign: 117.225µs, isgenerated: 69.413µs, structcheck: 52.343µs, SA4021: 48.119µs
INFO [linters context/goanalysis] analyzers took 7.422431ms with top 10 stages: buildir: 7.173865ms, U1000: 248.566µs
INFO [runner] processing took 4.045µs with stages: max_same_issues: 1.605µs, skip_dirs: 329ns, max_from_linter: 245ns, nolint: 226ns, filename_unadjuster: 163ns, cgo: 162ns, autogenerated_exclude: 148ns, uniq_by_line: 148ns, path_prettifier: 146ns, skip_files: 141ns, diff: 140ns, identifier_marker: 138ns, path_shortener: 63ns, severity-rules: 60ns, sort_results: 60ns, exclude-rules: 59ns, source_code: 54ns, max_per_file_from_linter: 54ns, exclude: 54ns, path_prefixer: 50ns
INFO [runner] linters took 1.380644653s with stages: goanalysis_metalinter: 1.352787535s, unused: 27.815289ms
INFO File cache stats: 0 entries of total size 0B
INFO Memory: 22 samples, avg is 136.4MB, max is 207.1MB
INFO Execution took 2.093703496s

Metadata

Metadata

Assignees

No one assigned

    Labels

    questionFurther information is requested

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions