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

golangci-lint run cannot find golang files in the current directory #1833

Closed
4 tasks done
zealws opened this issue Mar 11, 2021 · 10 comments
Closed
4 tasks done

golangci-lint run cannot find golang files in the current directory #1833

zealws opened this issue Mar 11, 2021 · 10 comments

Comments

@zealws
Copy link

zealws commented Mar 11, 2021

  • 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).
  • Yes, I've tried with the standalone linter if available. (https://golangci-lint.run/usage/linters/)
Description of the problem

golangci-lint run (with no other arguments) cannot find any golang files in a simple repository with one golang file:

zeal@theos ~/go/src/github.com/zealws/test
# ls
main.go

zeal@theos ~/go/src/github.com/zealws/test
# golangci-lint run
ERRO Running error: context loading failed: no go files to analyze 

The main.go file is perfectly valid go:

zeal@theos ~/go/src/github.com/zealws/test
# go run main.go 
hello world!

Things I tried which didn't help:

  • Making the directory a git repository and committing the main.go
  • Moving the directory into my $GOPATH
  • Moving the main.go into a subdirectory
  • Copying main.go into a second file.
  • Running golangci-lint run ./... instead.
  • Changing the package name in main.go to lib

The only way I was able to get golangci-lint run to lint the main.go file was providing it as an argument, i.e.: golangci-lint run main.go

Version of golangci-lint
# golangci-lint --version
golangci-lint has version 1.38.0 built from 507703b on 2021-03-10T00:37:58Z
Config file

None.

Go environment
# go version && go env
go version go1.16 linux/amd64
GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/home/zeal/.cache/go-build"
GOENV="/home/zeal/.config/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=""
GOMODCACHE="/home/zeal/go/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/home/zeal/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/lib/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/lib/go/pkg/tool/linux_amd64"
GOVCS=""
GOVERSION="go1.16"
GCCGO="gccgo"
AR="ar"
CC="gcc"
CXX="g++"
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 -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build3533911002=/tmp/go-build -gno-record-gcc-switches"
Verbose output of running
zeal@theos ~/go/src/github.com/zealws/test
# golangci-lint cache clean
zeal@theos ~/go/src/github.com/zealws/test
# golangci-lint run -v
INFO [config_reader] Config search paths: [./ /home/zeal/go/src/github.com/zealws/test /home/zeal/go/src/github.com/zealws /home/zeal/go/src/github.com /home/zeal/go/src /home/zeal/go /home/zeal /home /] 
INFO [lintersdb] Active 10 linters: [deadcode errcheck gosimple govet ineffassign staticcheck structcheck typecheck unused varcheck] 
INFO [loader] Go packages loading at mode 575 (deps|types_sizes|compiled_files|files|imports|name|exports_file) took 27.460736ms 
ERRO Running error: context loading failed: no go files to analyze 
INFO Memory: 2 samples, avg is 72.1MB, max is 72.1MB 
INFO Execution took 30.037261ms                   
Code example or link to a public repository

This file was at main.go in the current directory, while running golangci-lint run

package main

import "fmt"

func main() {
        fmt.Println("hello world!")
}
@zealws zealws added the bug Something isn't working label Mar 11, 2021
@ldez
Copy link
Member

ldez commented Mar 11, 2021

Hello, could try to add a go.mod file? (go mod init)

or use GO111MODULE=off golangci-lint run

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

zealws commented Mar 11, 2021

GO111MODULE=off did the trick:

# GO111MODULE=off golangci-lint run -v
INFO [config_reader] Config search paths: [./ /home/zeal/go/src/github.com/zealws/test /home/zeal/go/src/github.com/zealws /home/zeal/go/src/github.com /home/zeal/go/src /home/zeal/go /home/zeal /home /] 
INFO [lintersdb] Active 10 linters: [deadcode errcheck gosimple govet ineffassign staticcheck structcheck typecheck unused varcheck] 
INFO [loader] Go packages loading at mode 575 (imports|compiled_files|deps|exports_file|files|name|types_sizes) took 74.020145ms 
INFO [runner/filename_unadjuster] Pre-built 0 adjustments in 73.341µs 
INFO [linters context/goanalysis] analyzers took 0s with no stages 
INFO [linters context/goanalysis] analyzers took 0s with no stages 
INFO [runner] processing took 3.06µs with stages: max_same_issues: 571ns, nolint: 370ns, skip_dirs: 272ns, max_from_linter: 237ns, cgo: 147ns, autogenerated_exclude: 141ns, path_prettifier: 138ns, skip_files: 137ns, diff: 135ns, source_code: 128ns, filename_unadjuster: 123ns, path_shortener: 122ns, uniq_by_line: 121ns, identifier_marker: 100ns, exclude-rules: 60ns, sort_results: 55ns, max_per_file_from_linter: 53ns, severity-rules: 51ns, exclude: 51ns, path_prefixer: 48ns 
INFO [runner] linters took 14.540217ms with stages: goanalysis_metalinter: 14.164405ms, unused: 307.698µs 
INFO File cache stats: 0 entries of total size 0B 
INFO Memory: 2 samples, avg is 71.8MB, max is 72.3MB 
INFO Execution took 92.134879ms                   

Adding a go.mod also worked:

zeal@theos ~/go/src/github.com/zealws/test
# go mod init
go: creating new go.mod: module github.com/zealws/test
go: to add module requirements and sums:
        go mod tidy

zeal@theos ~/go/src/github.com/zealws/test
# golangci-lint run -v
INFO [config_reader] Config search paths: [./ /home/zeal/go/src/github.com/zealws/test /home/zeal/go/src/github.com/zealws /home/zeal/go/src/github.com /home/zeal/go/src /home/zeal/go /home/zeal /home /] 
INFO [lintersdb] Active 10 linters: [deadcode errcheck gosimple govet ineffassign staticcheck structcheck typecheck unused varcheck] 
INFO [loader] Go packages loading at mode 575 (deps|exports_file|files|types_sizes|compiled_files|imports|name) took 83.654275ms 
INFO [runner/filename_unadjuster] Pre-built 0 adjustments in 62.835µs 
INFO [linters context/goanalysis] analyzers took 0s with no stages 
INFO [linters context/goanalysis] analyzers took 0s with no stages 
INFO [runner] processing took 3.334µs with stages: nolint: 453ns, skip_dirs: 424ns, max_same_issues: 420ns, cgo: 210ns, filename_unadjuster: 198ns, max_per_file_from_linter: 180ns, uniq_by_line: 173ns, diff: 166ns, max_from_linter: 164ns, source_code: 163ns, autogenerated_exclude: 160ns, identifier_marker: 136ns, path_prettifier: 128ns, skip_files: 113ns, sort_results: 47ns, exclude: 47ns, path_shortener: 39ns, path_prefixer: 39ns, severity-rules: 37ns, exclude-rules: 37ns 
INFO [runner] linters took 12.839378ms with stages: goanalysis_metalinter: 12.650511ms, unused: 150.299µs 
INFO File cache stats: 0 entries of total size 0B 
INFO Memory: 2 samples, avg is 72.0MB, max is 72.1MB 
INFO Execution took 99.79092ms                    

@zealws
Copy link
Author

zealws commented Mar 11, 2021

If you don't want to patch golangci-lint to make it work without a go.mod file, can you at least update the documentation to make it clear that a go.mod file is required in order to run golangci-lint?

@zealws
Copy link
Author

zealws commented Mar 11, 2021

Also please don't close github issues when there's still work to be done on them. The issue was intended to be more than a simple question, and I don't have access to re-open the issue to reflect that.

@bombsimon
Copy link
Member

But it is working without a go.mod file, you just have to manually set GO111MODULE to off. GO111MODULE has been around since 1.11 as you can tell by the name but has been set to auto since. From 1.16 this is now defaulting to off which seems reasonable after having it set to auto for over two years, see changelog.

If you haven't migrated to modules yet, maybe you are OK to set GO111MODULE to a proper value according to the Go documentation in your environment?

I also don't like closing issues that's not resolved but you confirmed it was working with both suggested alternatives so I saw the issue where golangci-lint not finding Go packages with a faulty GO111MODULE value was resolved. I can re-open the issue and re-name it but it's easier to keep track and find issues by searching if we keep the title I think.

@zealws
Copy link
Author

zealws commented Mar 11, 2021

I took a stab at the documentation changes with: #1835

@zealws
Copy link
Author

zealws commented Mar 11, 2021

If you haven't migrated to modules yet, maybe you are OK to set GO111MODULE to a proper value according to the Go documentation in your environment?

I only found this while trying to create a minimal repro for #1832. Most of my go projects are go modules, so this isn't a problem for any actual projects.

However, I do feel like there's either an issue with the go file searching, or something missing from the documentation, which is why I flagged the issue in the first place.

I saw the issue where golangci-lint not finding Go packages with a faulty GO111MODULE value was resolved

This feels more like a work-around than an actual fix.

I can re-open the issue and re-name it but it's easier to keep track and find issues by searching if we keep the title I think.

It wasn't clear to me that you had created another issue for this problem. Of course, I'm okay with you organizing the issues for your project in whatever way makes the most sense for you.

Fwiw, the documentation changes I suggested in #1835 may not be the best way to resolve this gap. I'll defer to you as to the best way to fix the code and/or update the documentation, but I'm happy to edit the documentation since that's relatively easy. :)

@bombsimon
Copy link
Member

Sorry, absolutely not trying to be a jerk here! I'll re-open the issue until we've found a proper solution regarding documentation, changes to the code or another suggestion, I shouldn't by myself decide what's best to do here!

@bombsimon bombsimon reopened this Mar 11, 2021
@bombsimon bombsimon added area: docs and removed question Further information is requested labels Mar 11, 2021
@zealws
Copy link
Author

zealws commented Mar 11, 2021

Sure. I understand. :)

I think in the future if you cut a new issue to reorganize work, you might leave a comment on the old issues (like this one) with an FYI to that effect. That would probably cut down the confusion.

We can discuss on the PR what the best way to update the docs is.

Thanks for the clarification!

@stale
Copy link

stale bot commented Mar 30, 2022

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.

@stale stale bot added the stale No recent correspondence or work activity label Mar 30, 2022
@stale stale bot closed this as completed Apr 29, 2022
@ldez ldez removed the stale No recent correspondence or work activity label Mar 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants