Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
3 tasks done
jarv opened this issue Dec 23, 2020 · 9 comments
Closed
3 tasks done
Labels
question Further information is requested

Comments

@jarv
Copy link

jarv commented Dec 23, 2020

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
@jarv jarv added the bug Something isn't working label Dec 23, 2020
@boring-cyborg
Copy link

boring-cyborg bot commented Dec 23, 2020

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

@Ch00k
Copy link

Ch00k commented Feb 22, 2021

I second this. I would like to use golangci-lint with coc.nvim, which would also mean that I'd need to be able to run linting on separate files as opposed to the entire project. Perhaps a config option could be added that would allow running only the linters that are actually able to run on a single file?

@Zalgo2462
Copy link

Running into this issue after updating my linters in vscode.

@ldez
Copy link
Member

ldez commented Mar 21, 2021

FYI you can run the following linters on a single file:

  • dogsled
  • dupl
  • forbidigo
  • funlen
  • gochecknoglobals
  • gochecknoinits
  • gocognit
  • goconst
  • gocyclo
  • godot
  • godox
  • gofmt
  • gofumpt
  • goimports
  • gomnd
  • goprintffuncname
  • ifshort
  • lll
  • misspell
  • nakedret
  • nestif
  • nolintlint
  • prealloc
  • predeclared
  • scopelint
  • whitespace
  • wsl

In the next release (v1.39.0), the following linters will also be handle to run on a single file:

  • asciicheck
  • forcetypeassert
  • gci
  • goheader
  • gomodguard
  • ineffassign
  • nlreturn
  • paralleltest
  • testpackage

All the linters are not able to run like that, because they require information that is not available in a single file.

@ldez ldez added question Further information is requested and removed bug Something isn't working labels Mar 21, 2021
@ldez
Copy link
Member

ldez commented Mar 23, 2021

As we fixed the --fast flag in #1844 (available in the next release [v1.39.0]), you will be able to use this flag as a filter: all the "fast" linters work on a single file.

name fast single file
ineffassign x x
asciicheck x x
dogsled x x
dupl x x
forbidigo x x
forcetypeassert x x
funlen x x
gci x x
gochecknoglobals x x
gochecknoinits x x
gocognit x x
goconst x x
gocyclo x x
godot x x
godox x x
gofmt x x
gofumpt x x
goheader x x
goimports x x
gomnd x x
gomoddirectives x x
gomodguard x x
goprintffuncname x x
ifshort x x
lll x x
misspell x x
nakedret x x
nestif x x
nlreturn x x
nolintlint x x
paralleltest x x
prealloc x x
predeclared x x
scopelint x x
testpackage x x
whitespace x x
wsl x x
deadcode ~
errcheck ~
gosimple ~
govet ~
staticcheck ~
structcheck ~
typecheck ~
unused ~
varcheck ~
bodyclose ~
cyclop ~
depguard ~
durationcheck ~
errorlint ~
exhaustive ~
exhaustivestruct ~
exportloopref ~
gocritic ~
goerr113 ~
golint ~
gosec ~
importas ~
interfacer ~
makezero ~
maligned ~
nilerr ~
noctx ~
revive x
rowserrcheck ~
sqlclosecheck ~
stylecheck ~
thelper ~
tparallel ~
unconvert ~
unparam ~
wastedassign ~
wrapcheck ~

@ldez
Copy link
Member

ldez commented Mar 23, 2021

In summary:

  • all the linters are not able to run on a single, because they require information that is not available in a single file.
  • the linters that are able to run on a single can be filtered by the flag --fast

So I will close this issue 😃

@ldez ldez closed this as completed Mar 23, 2021
@th3coop
Copy link

th3coop commented May 14, 2021

Hi @ldez, just wanted to confirm, if i'm running golangci-lint version 1.40, I should be able to run it on a single file using the --fast option and it will only run the linters that work on a single file?

If yes, that's unfortunately not the behaviour I'm seeing but want to confirm before I open an issue. I've had to manually strip out any linters from my config file that require the entire project, even if I have the --fast option included in the cmd.

@ldez
Copy link
Member

ldez commented May 14, 2021

Hi @th3coop, I think you are talking about this: #1909

@th3coop
Copy link

th3coop commented May 14, 2021

Oh yep, that looks like what my situation is. We too are using disable-all. Thanks for the quick response @ldez.

sudeepdino008 added a commit to covalenthq/bsp-agent that referenced this issue Mar 29, 2022
- running on each file isn't as comprehensive as running against the entire repo. See this [issue](golangci/golangci-lint#1574)
sudeepdino008 added a commit to covalenthq/bsp-agent that referenced this issue Mar 29, 2022
- running on each file isn't as comprehensive as running against the entire repo. See this [issue](golangci/golangci-lint#1574)
guilhem added a commit to guilhem/super-linter that referenced this issue Jun 15, 2022
All tests can't be run agaist single files.
golangci-lint have a dedicated list of linter adapted to single file
usage.
golangci/golangci-lint#1574

Signed-off-by: Guilhem Lettron <guilhem@barpilot.io>
admiralAwkbar pushed a commit to super-linter/super-linter that referenced this issue Jun 16, 2022
All tests can't be run agaist single files.
golangci-lint have a dedicated list of linter adapted to single file
usage.
golangci/golangci-lint#1574

Signed-off-by: Guilhem Lettron <guilhem@barpilot.io>
carhartl added a commit to carhartl/lefthook-config that referenced this issue Aug 15, 2023
The built-in typecheck cannot run on single files and will cause a
compilation error as soon as we attempt to lint a file that requires a
file from the same package. As a best-effort workaround now linting
the entire package a file has been changed in in the pre-commit hook.

golangci/golangci-lint#1574 (comment)
golangci/golangci-lint#2912
wandering-tales added a commit to wandering-tales/megalinter that referenced this issue Apr 26, 2024
It seems `list_of_files` is not the right choice as default lint
mode for the `golangci-lint` linter, for several reasons.

In such mode, the changed `.go` files are sent in a single call to the
linter.

The first issue with that is that, as the linter
[Quick Start](https://golangci-lint.run/welcome/quick-start/) guide
explicitly states, files must come from the same package.

The second issue is that the linter tries to compile each file via
`types.Checker` and, unless the source file is self-contained and has no
references to things declared in other source files of the codebase, the
compilation will fail, even if the references are towards the same
package.

Such compilation errors are reported by `golangci-lint` and labeled as
`typecheck` (which is not a linter) (see
[why do you have `typecheck` errors?](https://golangci-lint.run/welcome/faq/#why-do-you-have-typecheck-errors)
section of the documentation).

The same problem has also been reported in
[`golangci-lint oxsecurity#1574`](golangci/golangci-lint#1574)
by an user who configured his editor to run the linter on single files.
As commented by a core maintainer
[here](golangci/golangci-lint#1574 (comment))
`typecheck` is not included in the "fast" linters list and, therefore,
cannot be run on single files.
wandering-tales added a commit to wandering-tales/megalinter that referenced this issue Apr 26, 2024
It seems `list_of_files` is not the right choice as default lint
mode for the `golangci-lint` linter, for several reasons.

In such mode, the changed `.go` files are sent in a single call to the
linter.

The first issue with that is that, as the linter
[Quick Start](https://golangci-lint.run/welcome/quick-start/) guide
explicitly states, files must come from the same package.

The second issue is that the linter tries to compile each file via
`types.Checker` and, unless the source file is self-contained and has no
references to things declared in other source files of the codebase, the
compilation will fail, even if the references are towards the same
package.

Such compilation errors are reported by `golangci-lint` and labeled as
`typecheck` (which is not a linter) (see
[why do you have `typecheck` errors?](https://golangci-lint.run/welcome/faq/#why-do-you-have-typecheck-errors)
section of the documentation).

The same problem has also been reported in
[`golangci-lint oxsecurity#1574`](golangci/golangci-lint#1574)
by an user who configured his editor to run the linter on single files.
As commented by a core maintainer
[here](golangci/golangci-lint#1574 (comment))
`typecheck` is not included in the "fast" linters list and, therefore,
cannot be run on single files.
wandering-tales added a commit to wandering-tales/megalinter that referenced this issue Apr 26, 2024
It seems `list_of_files` is not the right choice as default lint
mode for the `golangci-lint` linter, for several reasons.

In such mode, the changed `.go` files are sent in a single call to the
linter.

The first issue with that is that, as the linter
[Quick Start](https://golangci-lint.run/welcome/quick-start/) guide
explicitly states, files must come from the same package.

The second issue is that the linter tries to compile each file via
`types.Checker` and, unless the source file is self-contained and has no
references to things declared in other source files of the codebase, the
compilation will fail, even if the references are towards the same
package.

Such compilation errors are reported by `golangci-lint` and labeled as
`typecheck` (which is not a linter) (see
[why do you have `typecheck` errors?](https://golangci-lint.run/welcome/faq/#why-do-you-have-typecheck-errors)
section of the documentation).

The same problem has also been reported in
[`golangci-lint oxsecurity#1574`](golangci/golangci-lint#1574)
by an user who configured his editor to run the linter on single files.
As commented by a core maintainer
[here](golangci/golangci-lint#1574 (comment))
`typecheck` is not included in the "fast" linters list and, therefore,
cannot be run on single files.
wandering-tales added a commit to wandering-tales/megalinter that referenced this issue Apr 26, 2024
It seems `list_of_files` is not the right choice as default lint
mode for the `golangci-lint` linter, for several reasons.

In such mode, the changed `.go` files are sent in a single call to the
linter.

The first issue with that is that, as the linter
[Quick Start](https://golangci-lint.run/welcome/quick-start/) guide
explicitly states, files must come from the same package.

The second issue is that the linter tries to compile each file via
`types.Checker` and, unless the source file is self-contained and has no
references to things declared in other source files of the codebase, the
compilation will fail, even if the references are towards the same
package.

Such compilation errors are reported by `golangci-lint` and labeled as
`typecheck` (which is not a linter) (see
[why do you have `typecheck` errors?](https://golangci-lint.run/welcome/faq/#why-do-you-have-typecheck-errors)
section of the documentation).

The same problem has also been reported in
[`golangci-lint oxsecurity#1574`](golangci/golangci-lint#1574)
by an user who configured his editor to run the linter on single files.
As commented by a core maintainer
[here](golangci/golangci-lint#1574 (comment))
`typecheck` is not included in the "fast" linters list and, therefore,
cannot be run on single files.
wandering-tales added a commit to wandering-tales/megalinter that referenced this issue Apr 26, 2024
It seems `list_of_files` is not the right choice as default lint
mode for the `golangci-lint` linter, for several reasons.

In such mode, the changed `.go` files are sent in a single call to the
linter.

The first issue with that is that, as the linter
[Quick Start](https://golangci-lint.run/welcome/quick-start/) guide
explicitly states, files must come from the same package.

The second issue is that the linter tries to compile each file via
`types.Checker` and, unless the source file is self-contained and has no
references to things declared in other source files of the codebase, the
compilation will fail, even if the references are towards the same
package.

Such compilation errors are reported by `golangci-lint` and labeled as
`typecheck` (which is not a linter) (see
[why do you have `typecheck` errors?](https://golangci-lint.run/welcome/faq/#why-do-you-have-typecheck-errors)
section of the documentation).

The same problem has also been reported in
[`golangci-lint oxsecurity#1574`](golangci/golangci-lint#1574)
by an user who configured his editor to run the linter on single files.
As commented by a core maintainer
[here](golangci/golangci-lint#1574 (comment))
`typecheck` is not included in the "fast" linters list and, therefore,
cannot be run on single files.
wandering-tales added a commit to wandering-tales/megalinter that referenced this issue Apr 26, 2024
It seems `list_of_files` is not the right choice as default lint
mode for the `golangci-lint` linter, for several reasons.

In such mode, the changed `.go` files are sent in a single call to the
linter.

The first issue with that is that, as the linter
[Quick Start](https://golangci-lint.run/welcome/quick-start/) guide
explicitly states, files must come from the same package.

The second issue is that the linter tries to compile each file via
`types.Checker` and, unless the source file is self-contained and has no
references to things declared in other source files of the codebase, the
compilation will fail, even if the references are towards the same
package.

Such compilation errors are reported by `golangci-lint` and labeled as
`typecheck` (which is not a linter) (see
[why do you have `typecheck` errors?](https://golangci-lint.run/welcome/faq/#why-do-you-have-typecheck-errors)
section of the documentation).

The same problem has also been reported in
[`golangci-lint oxsecurity#1574`](golangci/golangci-lint#1574)
by an user who configured his editor to run the linter on single files.
As commented by a core maintainer
[here](golangci/golangci-lint#1574 (comment))
`typecheck` is not included in the "fast" linters list and, therefore,
cannot be run on single files.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

5 participants