golangci-lint icon indicating copy to clipboard operation
golangci-lint copied to clipboard

context loading failed: no go files to analyze

Open rmilejcz opened this issue 6 years ago • 65 comments

I have regularly run golangci-lint run from the root of my directory without issue for the last several weeks. Today, after successfully running golangci-lint run, I fixed all the reported issues and then ran the command in the same terminal but I got the output:

ERRO Running error: context loading failed: no go files to analyze

even though nothing seems to have changed. The project structure is basically 60 or so folders, each having a main.go, a main_test.go and two auto generated files which are ignored by golangci-lint. If I specify a package it lints without issue e.g.:

golangci-lint run ActivityLog

runs the linter on that package without any error.

Version of golangci-lint
$ golangci-lint --version
golangci-lint has version 1.21.0 built from 645e794 on 2019-10-15T18:15:04Z
Config file
$ cat .golangci.yml
run:
  concurrency: 4
  deadline: 2m
  issues-exit-code: 1
  tests: true

output:
  format: colored-line-number
  print-issued-lines: true
  print-linter-name: true

linter-settings:
  errcheck:
    check-type-assertions: true
    check-blank: true

  govet:
    check-shadowing: true
    settings:
      printf:
        funcs:
          - (github.com/golangci/golangci-lint/pkg/logutils.Log).Infof
          - (github.com/golangci/golangci-lint/pkg/logutils.Log).Warnf
          - (github.com/golangci/golangci-lint/pkg/logutils.Log).Errorf
          - (github.com/golangci/golangci-lint/pkg/logutils.Log).Fatalf
  golint:
    min-confidence: 0.8

  gofmt:
    simplify: true

  goimports:
    local-prefixes: kalos-core

  gocyclo:
    min-complexity: 20

  maligned:
    suggest-new: true

  goconst:
    min-len: 3
    min-occurrences: 3

  misspell:
    locale: US
    ignore-words:
      - supercalifragilisticexpialidocious

  lll:
    line-length: 120
    tab-width: 2

  unused:
    check-exported: false

  unparam:
    check-exported: false

  prealloc:
    simple: true
    range-loops: true
    for-loops: false

  gocritic:
    enabled-checks:
      - appendAssign
      - argOrder
      - assignOp
      - badCond
      - boolExprSimplify
      - builtinShadow
      - captLocal
      - caseOrder
      - commendOutCode
      - commentFormatting
      - commentedOutImport
      - defaultCaseOrder
      - deprecatedComment
      - docStub
      - dupArg
      - dupBranchBody
      - dupCase
      - dupSubExpr
      - elseif
      - emptyFallthrough
      - emptyStringTest
      - equalFold
      - exitAfterDefer
      - flagDeref
      - flagName
      - hexLiteral
      - hugeParam
      - ifElseChain
      - importShadow
      - indexAlloc
      - initClause
      - methodExprCall
      - nestingReduce
      - nilValReturn
      - octalLiteral
      - offBy1
      - paramTypeCombine
      - ptrToRefParam
      - rangeExprCopy
      - rangeValCopy
      - regexpMust
      - singleCaseSwitch
      - sloppyLen
      - sloppyReassign
      - stringXbytes
      - switchTrue
      - typeAssertChain
      - typeSwitchVar
      - typeUnparen
      - underef
      - unlabelStmt
      - unlambda
      - unnecessaryBlock
      - unslice
      - valSwap
      - weakCond
      - wrapperFunc
      - yodaStyleExpr

    disabled-checks:
      - codegenComment
      - unnamedResult

linters:
  enable:
    - bodyclose
    - megacheck
    - govet
    - errcheck
    - staticcheck
    - unused
    - gosimple
    - structcheck
    - varcheck
    - ineffassign
    - deadcode
    - typecheck
    - gosec
    - interfacer
    - unconvert
    - goconst
    - gocyclo
    - goimports
    - maligned
    - misspell
    - unparam
    - scopelint
    - gocritic
    - gochecknoinits
  disable:
    - dupl
    - lll
    - stylecheck
Go environment
$ go version && go env
go version go1.13.1 darwin/amd64
GO111MODULE="on"
GOARCH="amd64"
GOBIN="/Users/robertmilejczak/Documents/Projects/bin"
GOCACHE="/Users/robertmilejczak/Library/Caches/go-build"
GOENV="/Users/robertmilejczak/Library/Application Support/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GONOPROXY=""
GONOSUMDB=""
GOOS="darwin"
GOPATH="/Users/robertmilejczak/Documents/Projects"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/local/Cellar/go/1.13.1/libexec"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/Cellar/go/1.13.1/libexec/pkg/tool/darwin_amd64"
GCCGO="gccgo"
AR="ar"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
GOMOD="/Users/robertmilejczak/Documents/Projects/src/github.com/rmilejcz/kalos-core/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 -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/kv/npdslxvd0ml09ntv_0rsmqcr0000gn/T/go-build078154239=/tmp/go-build -gno-record-gcc-switches -fno-common"
Verbose output of running
$ golangci-lint run -v
INFO [config_reader] Config search paths: [./ /Users/robertmilejczak/Documents/Projects/src/github.com/rmilejcz/kalos-core/server /Users/robertmilejczak/Documents/Projects/src/github.com/rmilejcz/kalos-core /Users/robertmilejczak/Documents/Projects/src/github.com/rmilejcz /Users/robertmilejczak/Documents/Projects/src/github.com /Users/robertmilejczak/Documents/Projects/src /Users/robertmilejczak/Documents/Projects /Users/robertmilejczak/Documents /Users/robertmilejczak /Users /] 
INFO [config_reader] Used config file ../.golangci.yml 
INFO [lintersdb] Active 23 linters: [bodyclose deadcode errcheck gochecknoinits goconst gocritic gocyclo goimports gosec gosimple govet ineffassign interfacer maligned misspell scopelint staticcheck structcheck typecheck unconvert unparam unused varcheck] 
INFO [loader] Go packages loading at mode 575 (types_sizes|deps|imports|name|compiled_files|exports_file|files) took 1.436796726s 
ERRO Running error: context loading failed: no go files to analyze 
INFO Memory: 16 samples, avg is 68.9MB, max is 68.9MB 
INFO Execution took 1.451511334s

rmilejcz avatar Oct 16 '19 16:10 rmilejcz

Disregard, for those getting a similar error message make sure you can build your project. I was able to figure out the issue by running

go test -v ./...

If possible, the error message context loading failed: no go files to analyze should be improved, especially when specifying verbose ouput.

rmilejcz avatar Oct 16 '19 16:10 rmilejcz

thank you, I think we need to fix error message

jirfag avatar Oct 17 '19 10:10 jirfag

Hi, I'm having the same issue even in latest version 1.21.0, any ideas what could be the issue? Thanks.

The only advice I could give is to make sure your code is error free, run go build or go test to make sure that everything works as expected

rmilejcz avatar Oct 25 '19 19:10 rmilejcz

This is tripping up our code formatting, as we've moved that to GolangCI Lint (running gofmt and goimports) rather than running those tools using locally installed versions. This is to ensure that a consistent toolset is used across all developers machines.

Goimports would normally clean up any import paths that don't exist (e.g. from code that has been removed), however when running in GolangCI Lint, this gives the no go files to analyze error instead

dackroyd avatar Nov 13 '19 02:11 dackroyd

We had to downgrade to 1.17 to make it to work and about goimports we had to disable it and use it standalone without golangci lint. Maybe we are doing something wrong but at least we found a way. I think the linter doesn't play nice with a multi go modules repository in our case, as we are using a mono repository with multiple go modules in it one per each service

This issue has several duplicates: #855, #865, #870, #916.

fdcds avatar Feb 13 '20 18:02 fdcds

For me it was connected to unupdated vendoring directory with enabled readonly mode.

mr-tron avatar Mar 23 '20 10:03 mr-tron

In https://github.com/wtfutil/wtf/pull/895 I used args: ./... with golangci/[email protected] to avoid this, though I don't need to when running on my own machine.

sam-github avatar May 16 '20 19:05 sam-github

Hi, there at least two different cases:

  1. no go.mod on the root, it's only in the subdirectory. I think it should be fixed by specifying a working directory because all built-in Go tooling doesn't work with such cases. I've made golangci/golangci-lint-action#15 for our GitHub action.
  2. timeout occurred during go/packages loading. The bug is in go/packages. We need help in fixing it and making a pull request to x/tools. The bug is in:
  • pkgs, err := packages.Load(conf, args...)
  • func goListDriver(cfg *Config, patterns ...string) (*driverResponse, error) {
  • func (state *golistState) createDriverResponse(words ...string) (*driverResponse, error) {
  • func (state *golistState) invokeGo(verb string, args ...string) (*bytes.Buffer, error) {
  • stdout, stderr, _, err := gocmdRunner.RunRaw(cfg.Context, inv) - here we ignore firendlyErr which contains a context cancellation error. I guess the bug was introduced in this commit. To reproduce it run in the repo of golangci-lint:
golangci-lint cache clean && go clean -modcache -cache -i
golangci-lint run -v --timeout=5s

jirfag avatar May 18 '20 07:05 jirfag

After merging #1154, do you have examples of public repos where we can reproduce the problem with no go files?

jirfag avatar May 19 '20 14:05 jirfag

One way to reproduce this with golangci itself is to:

  1. Set modules-download-mode: readonly in golangci.yml
  2. Remove a line from go.sum that does NOT have /go.mod in the line (EG: github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ=)
  3. run golangci:
╰─ ./golangci-lint run ./...
ERRO Running error: context loading failed: no go files to analyze

ryancurrah avatar May 31 '20 01:05 ryancurrah

Seems to be packages.Load(conf, args...) does not return an error even though the debug output with export GL_DEBUG=loader set shows a debug line from packages of:

DEBU [loader] 191.382563ms for GOROOT=/Users/ryancurrah/.go/current GOPATH= GO111MODULE= GOPROXY= PWD=/Users/ryancurrah/git/golangci-lint go [go list -mod=readonly -e -json -compiled=true -test=true -export=true -deps=true -find=false -mod=readonly -- ./...]

And that command from the debug log fails when I run it manually:

╰─ go list -mod=readonly -e -json -compiled=true -test=true -export=true -deps=true -find=false -mod=readonly -- ./...
go: updates to go.sum needed, disabled by -mod=readonly

Why is packages.Load not returning an error? I'll try and figure this out

https://github.com/golangci/golangci-lint/blob/master/pkg/lint/load.go#L200

ryancurrah avatar May 31 '20 02:05 ryancurrah

Ok after a bunch of time spent debugging the tools package from golang I found the issue and created a Pull Request here: https://github.com/golang/tools/pull/231.

If this fix is accepted packages.Load will properly raise errors related to go.sum issues instead of returning a empty slice of packages.

Then updating golang.org/x/tools dependancy will solve this particular issue.

Note it may not solve all no go files to analyze issues because the function invokeGo selectively chooses which errors to raise based on stderr text. Meaning not all errors are caught and this is done on purpose.

ryancurrah avatar May 31 '20 06:05 ryancurrah

Just another thought. Whenever someone is having this issue they should run:

go list -mod=readonly -e -json -compiled=true -test=true -export=true -deps=true -find=false -- ./...

In their project to see if a error occurs. If one does; another pull request to the tools package to catch that error may be required.

If you don’t want to fix it, the command will at least tell you what’s really wrong.

That command is the same one the tools package uses.

ryancurrah avatar May 31 '20 06:05 ryancurrah

We're observing the same, but what's not clear to me from the reports above: do others see this intermittently, or consistently? We do see it intermittently. We use the GitHub action, and rerunning sometimes fixes the issue. Running golangci-lint locally never produced the error so far with our repo. If there's more input I can provide, I'll be happy to do so.

2020-07-03T14:38:06.8296454Z Running [/home/runner/golangci-lint-1.27.0-linux-amd64/golangci-lint run --out-format=github-actions -E gofmt] in [/home/runner/work/xxx] ...
2020-07-03T14:39:06.9584039Z level=error msg="Running error: context loading failed: no go files to analyze"
2020-07-03T14:39:06.9645099Z level=error msg="Timeout exceeded: try increasing it by passing --timeout option"

Edit: it turns out that there was nothing wrong with golangci-lint at all. Even the suggestion about the timeout was correct. It just really took a lot of time to lint our app for the first time (about two minutes). I was under the impression that this couldn't be correct behaviour, as on my local machine linting would take at most 10 seconds. The answer lies in caching compiled go objects. Every Go GitHub actions 101 will explain that you should cache your dependencies, but don't forget - especially for larger projects - to cache your build. Also, don't be a smart-ass like me and just use golangci-lint action, it's good.

Apologies.

rollulus avatar Jul 03 '20 15:07 rollulus

The same here with golangci-lint 1.28.3. After deletion go.sum and running go mod tidy the problem was resolved for me and golangci-lint successfully started. Note, in my case the go.sum is in the .gitignore

wtask avatar Jul 12 '20 17:07 wtask

For us an upstream repo had a force tag push in it that was cached in go.sum.

To fix (on local machine):

rm go.sum
go clean -modcache
go mod tidy

then commit new go.sum, push, and ci worked again

As @ryancurrah suggested

go list -mod=readonly -e -json -compiled=true -test=true -export=true -deps=true -find=false -- ./...

was very helpful and showed the error right away.

nyetwurk avatar Jul 30 '20 02:07 nyetwurk

For us an upstream repo had a force tag push in it that was cached in go.sum.

To fix (on local machine):

rm go.sum
go clean -modcache
go mod tidy

then commit new go.sum, push, and ci worked again

As @ryancurrah suggested

go list -mod=readonly -e -json -compiled=true -test=true -export=true -deps=true -find=false -- ./...

was very helpful and showed the error right away.

This fixed my problem.

penglongli avatar Sep 02 '20 02:09 penglongli

I got this error today on OS X because ~/Library/Caches/go-build was owned by root. The go list command mentioned by others above also tracked this problem down.

joemcmahon avatar Dec 02 '20 22:12 joemcmahon

Hi there,

I notice that this problem occurs also with golangci-lint v1.37.1 (the latest version), while it never occurs with v1.36.0.

Here is the reproduction via Docker. ⬇️

Given test.go in the current directory as below:

package main

var unused int

func main() {
	print()
}

Passed with v1.36.0:

$ docker run --rm -v $(pwd):/app -w /app golangci/golangci-lint:v1.36.0 golangci-lint run
test.go:3:5: `unused` is unused (deadcode)
var unused int
    ^

$ docker run --rm -v $(pwd):/app -w /app golangci/golangci-lint:v1.36.0 golangci-lint version
golangci-lint has version v1.36.0 built from 6c25d068 on 2021-01-26T13:24:48Z

$ docker run --rm -v $(pwd):/app -w /app golangci/golangci-lint:v1.36.0 go version
go version go1.15.7 linux/amd64

Failed with v1.37.1:

$ docker run --rm -v $(pwd):/app -w /app golangci/golangci-lint:v1.37.1 golangci-lint run
level=error msg="Running error: context loading failed: no go files to analyze"

$ docker run --rm -v $(pwd):/app -w /app golangci/golangci-lint:v1.37.1 golangci-lint version
golangci-lint has version v1.37.1 built from b39dbcd6 on 2021-02-20T11:59:15Z

$ docker run --rm -v $(pwd):/app -w /app golangci/golangci-lint:v1.37.1 go version
go version go1.16 linux/amd64

Maybe it seems due to Go 1.16... 🤔

ybiquitous avatar Feb 26 '21 08:02 ybiquitous

I don't reproduce:

$ golangci-lint version
golangci-lint has version 1.37.1 built from b39dbcd6 on 2021-02-20T11:48:06Z
$ golangci-lint run
foo.go:3:5: `unused` is unused (deadcode)
var unused int
    ^

ldez avatar Feb 26 '21 09:02 ldez

@ldez What version of Go?

ybiquitous avatar Feb 26 '21 09:02 ybiquitous

the local version doesn't have any impact because golanci-lint is binary.

$ go version
go version go1.15.8 linux/amd64
$ golangci-lint version
golangci-lint has version 1.37.1 built from b39dbcd6 on 2021-02-20T11:48:06Z
$ golangci-lint run
foo.go:3:5: `unused` is unused (deadcode)
var unused int
    ^
$ go version
go version go1.16 linux/amd64
$ golangci-lint version
golangci-lint has version 1.37.1 built from b39dbcd6 on 2021-02-20T11:48:06Z
$ golangci-lint run
foo.go:3:5: `unused` is unused (deadcode)
var unused int
    ^

ldez avatar Feb 26 '21 09:02 ldez

Oh, it’s weird. the Docker version seems something wrong... 🤔

ybiquitous avatar Feb 26 '21 09:02 ybiquitous

Sorry but your problem seems offtopic

ldez avatar Feb 26 '21 09:02 ldez

Right. I will create a new issue. Thank you!

ybiquitous avatar Feb 26 '21 09:02 ybiquitous

Is there anything that can be done to help find a solution to this issue? It seems to replicate for us very consistently when running using the golangci-lint-action but never when run on development machines.

This fix did not solve the problem for us.

These repro steps did not replicate on our development machines.

Unfortunately the repo we have this issue with is not public.

chriscasola avatar Mar 03 '21 16:03 chriscasola

For us it was a checksum mismatch due to a re-released dependency under the same tag.

applike-ss avatar Apr 09 '21 06:04 applike-ss

I just ran into this. Looks like golangci-lint gets upset if you ask it to lint a subdirectory and it can't find main in a parent directory.

dkegel-fastly avatar Apr 11 '21 16:04 dkegel-fastly