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

context loading failed: no go files to analyze #825

Closed
rmilejcz opened this issue Oct 16, 2019 · 65 comments · Fixed by #4536
Closed

context loading failed: no go files to analyze #825

rmilejcz opened this issue Oct 16, 2019 · 65 comments · Fixed by #4536
Labels
question Further information is requested
Projects

Comments

@rmilejcz
Copy link

rmilejcz commented Oct 16, 2019

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 rmilejcz changed the title sudden "context loading failed: no go files to analyze sudden "context loading failed: no go files to analyze", v1.21.0 Oct 16, 2019
@rmilejcz
Copy link
Author

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.

@jirfag jirfag changed the title sudden "context loading failed: no go files to analyze", v1.21.0 Invalid error message "context loading failed: no go files to analyze" when can't build project Oct 17, 2019
@jirfag jirfag added the bug Something isn't working label Oct 17, 2019
@jirfag jirfag reopened this Oct 17, 2019
@jirfag
Copy link
Member

jirfag commented Oct 17, 2019

thank you, I think we need to fix error message

@jirfag jirfag added this to Needs triage in Zero Bug via automation Oct 17, 2019
@jirfag jirfag moved this from Needs triage to High priority in Zero Bug Oct 17, 2019
@alejandro-delgado-garcia

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

@rmilejcz
Copy link
Author

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

@dackroyd
Copy link

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

@alejandro-delgado-garcia

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

@fdcds
Copy link

fdcds commented Feb 13, 2020

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

@mr-tron
Copy link

mr-tron commented Mar 23, 2020

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

@sam-github
Copy link

In wtfutil/wtf#895 I used args: ./... with golangci/golangci-lint-action@v0.2.0 to avoid this, though I don't need to when running on my own machine.

@jirfag
Copy link
Member

jirfag commented May 18, 2020

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 Allow specifying directory 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 jirfag changed the title Invalid error message "context loading failed: no go files to analyze" when can't build project context loading failed: no go files to analyze May 18, 2020
jirfag referenced this issue in golang/tools May 18, 2020
Over time we have accumulated a disturbing number of ways to run the Go
command, each of which supported slightly different features and
workarounds. Combine all of them.

This unavoidably introduces some changes in debug logging behavior; I
think we should try to fix them incrementally and consistently.

Updates golang/go#37368.

Change-Id: I664ca8685bf247a226be3cb807789c2fcddf233d
Reviewed-on: https://go-review.googlesource.com/c/tools/+/220737
Run-TryBot: Heschi Kreinick <heschi@google.com>
Reviewed-by: Rebecca Stambler <rstambler@golang.org>
jirfag added a commit that referenced this issue May 18, 2020
In case of timeouts of go/packages loading
we could return such error.

Relates: #825
@wkoszek
Copy link

wkoszek commented Jun 2, 2022

On Go 1.17.1 it worked OK. After I upgrade togo version go1.18.3 linux/amd64 I get an error from below.

go run github.com/jstemmer/gotags *.go > tags                  # tags for easy browsing
go run github.com/golangci/golangci-lint/cmd/golangci-lint run --fix --verbose
INFO [config_reader] Config search paths: [./ /mnt/red/workspace/wkoszek/XXX/cli /mnt/dddd/workspace/wkoszek/xxx /mnt/ddd/workspace/wkoszek /mnt/ddd/workspace /mnt/ddd /mnt / /home/wkoszek]
INFO [config_reader] Used config file .golangci.yml
INFO [lintersdb] Active 14 linters: [deadcode errcheck gofmt goimports gosimple govet ineffassign revive staticcheck structcheck stylecheck typecheck unused varcheck]
INFO [loader] Go packages loading at mode 575 (name|compiled_files|exports_file|files|types_sizes|deps|imports) took 35.407944402s
ERRO Running error: context loading failed: no go files to analyze
INFO Memory: 356 samples, avg is 23.1MB, max is 23.1MB
INFO Execution took 35.413440288s
exit status 5
make: *** [makefile:76: lint] Error 1

manno pushed a commit to manno/fleet-staging that referenced this issue Jun 10, 2022
golangci-lint fails with a context error
(maybe golangci/golangci-lint#825)

Using the opportunity to bump the helm version, which was ~2 years
old.
@xhd2015
Copy link

xhd2015 commented Jul 18, 2022

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

This answer should be promoted since it clearly addresses that the issue is actually caused by error resides in your project, not that golangci-lint cannot find any go files.

@WGrape
Copy link

WGrape commented Aug 28, 2022

The main reason is that your code failed to compile, maybe you forget to run godep save or go mod download in a new container.

I also got this problem with the output when running golangci-lint run --timeout=5m in gitlab-ci:

level=error msg="Running error: context loading failed: no go files to analyze"

When i add the command go build before golangci-lint run --timeout=5m,and i got this new output :

go: cannot find main module, but found Godeps/Godeps.json in xxx/xxx
	to create a module there, run:
	go mod init

So, i realized that i need download it's dependencies and compile it successfully.

@stevekuznetsov
Copy link

Just hit this because I needed to run go mod tidy. Would love to see an improvement to the error message. Using v1.49.0

@dianapicnic
Copy link

In my case the command below helped:

go mod tidy && go mod vendor

@berndschatz
Copy link

berndschatz commented Dec 12, 2022

i can confirm this solution:

make

[...]
RRO Running error: context loading failed: no go files to analyze 
make: *** [Makefile:46: check-style] Error 5

$ go build
go: updates to go.mod needed; to update it:
	go mod tidy

$ go mod tidy
go: downloading github.com/jhump/protoreflect v1.6.0
go: downloading gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c
go: downloading github.com/xtgo/uuid v0.0.0-20140804021211-a0b114877d4c
go: downloading github.com/google/go-cmp v0.5.6
go: downloading github.com/kr/pretty v0.3.0
go: downloading github.com/dustin/go-humanize v1.0.0
go: downloading github.com/sirupsen/logrus v1.8.1
go: downloading github.com/BurntSushi/toml v0.3.1
go: downloading github.com/kr/text v0.2.0
go: downloading github.com/rogpeppe/go-internal v1.8.0
go: downloading golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1

[...]

$ make
Checking for style guide compliance
cd webapp && npm run lint

[...]

plugin built at: .......

Error message is not very helpful.

@nyetwurk
Copy link

nyetwurk commented Nov 8, 2023

We had a race condition in our CI where go mod tidy was not run before the cache was created, so golangci-lint was being run in an image with no go/pkg/mod files. There really should be a better error message for this.

However, even an additional go mod tidy does not always fix the problem.

$ ls -al go.mod go.sum
-rw-rw-rw-    1 root     root          6123 Nov  8 19:36 go.mod
-rw-rw-rw-    1 root     root        200250 Nov  8 19:36 go.sum
$ ls -al .go/pkg/mod || go mod tidy
total 72
drwxr-xr-x   18 root     root          4096 Nov  8 19:31 .
drwxrwxrwx    3 root     root          4096 Nov  8 19:36 ..
drwxr-xr-x    4 root     root          4096 Nov  8 19:31 cache
drwxr-xr-x   11 root     root          4096 Nov  8 19:31 cosmossdk.io
drwxr-xr-x    3 root     root          4096 Nov  8 19:31 filippo.io
drwxr-xr-x   95 root     root          4096 Nov  8 19:31 github.com
drwxr-xr-x    3 root     root          4096 Nov  8 19:31 gitlab.com
drwxr-xr-x    6 root     root          4096 Nov  8 19:31 go.blockdaemon.com
drwxr-xr-x    3 root     root          4096 Nov  8 19:31 go.etcd.io
drwxr-xr-x    6 root     root          4096 Nov  8 19:31 go.uber.org
drwxr-xr-x    3 root     root          4096 Nov  8 19:31 golang.org
drwxr-xr-x    6 root     root          4096 Nov  8 19:31 google.golang.org
drwxr-xr-x    6 root     root          4096 Nov  8 19:31 gopkg.in
drwxr-xr-x    3 root     root          4096 Nov  8 19:31 gotest.tools
dr-xr-xr-x   14 root     root          4096 Nov  8 19:31 gotest.tools@v2.2.0+incompatible
drwxr-xr-x    3 root     root          4096 Nov  8 19:31 nhooyr.io
drwxr-xr-x    3 root     root          4096 Nov  8 19:31 pgregory.net
drwxr-xr-x    3 root     root          4096 Nov  8 19:31 sigs.k8s.io
$ golangci-lint --version
golangci-lint has version v1.55.2 built with go1.21.3 from e3c2265f on 2023-11-03T13:27:48Z
$ golangci-lint -v run --timeout 10m --issues-exit-code 0
level=info msg="[config_reader] Config search paths: [./ XXX  / /root]"
level=info msg="[lintersdb] Active 6 linters: [errcheck gosimple govet ineffassign staticcheck unused]"
level=info msg="[loader] Go packages loading at mode 575 (types_sizes|compiled_files|exports_file|imports|name|deps|files) took 1.328024256s"
level=error msg="Running error: context loading failed: no go files to analyze"
level=info msg="Memory: 15 samples, avg is 22.8MB, max is 22.8MB"
level=info msg="Execution took 1.333339766s"

@dkegel-fastly
Copy link

We just had a problem in CI where, through a long chain of quirks, the source tree was incomplete... and we didn't verify it built before running lint. Hilarity ensued (with an error like can't run linter goanalysis_metalinter: inspect: failed to load package foo: could not load export data: no export data for...).

Could golangci-lint check somehow that the project had built? In CI, this could be rather helpful.

@BedivereZero
Copy link

When obtaining go modules fails, golangci-lint will report the error "no go files to analyze". The error message is inaccurate.

$ go mod download
...
verifying devops.example.org/Organization/Project/_git/go-common@v1.0.4: checksum mismatch
	downloaded: h1:Ai9O0FGo9bcG8dkMxjZ2KPPMcrchs3lDItWduc2ht5Q=
	go.sum:     h1:Wto2v4Zv6gZrSppDzaSIJIuwFOMNM77EckvPIoVbsRw=

$ golangci-lint
Running error: context loading failed: no go files to analyze

@ldez
Copy link
Member

ldez commented Dec 14, 2023

go mod download != go mod tidy

You have to use go mod tidy

@BedivereZero
Copy link

go mod download != go mod tidy

You have to use go mod tidy

To reproduce this error, these conditions are required:

  1. already run go mod tidy, there are go.mod and go.sum in repository's root
  2. a go module in go.mod has change,for example the git tag v1.0.4 has been re-created to another git commit
  3. there is no go model local cache, like pipeline environment
  4. run golangci-lint run

@ldez
Copy link
Member

ldez commented Dec 15, 2023

a go module in go.mod has change,for example the git tag v1.0.4 has been re-created to another git commit

This should never happen: tag re-creation is strictly forbidden by the Go modules system.
A tag should be retracted but never re-created.

@Integralist
Copy link

I'm seeing this issue currently.

I can run golangci-lint run --verbose ./cmd/check/... successfully and the CLI will report back a ton of issues with the code...

But the moment I open that file in Neovim I only see this...

Screenshot 2024-01-11 at 17 56 41

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
No open projects
Zero Bug
  
High priority
Development

Successfully merging a pull request may close this issue.