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

ERRO Running error: context loading failed: failed to load program with go/packages #395

Closed
Rico1900 opened this issue Feb 15, 2019 · 29 comments
Labels
bug Something isn't working

Comments

@Rico1900
Copy link

  1. Version of golangci-lint: golangci-lint --version (or git commit if you don't use binary distribution)
    golangci-lint has version 1.14.0 built from 6c4d290 on 2019-02-11T06:59:20Z
  2. Config file: cat .golangci.yml
run:
  # default concurrency is a available CPU number
  concurrency: 4

  # timeout for analysis, e.g. 30s, 5m, default is 1m
  deadline: 5m

  # which dirs to skip: they won't be analyzed;
  # can use regexp here: generated.*, regexp is applied on full path;
  # default value is empty list, but next dirs are always skipped independently
  # from this option's value:
  #   	vendor$, third_party$, testdata$, examples$, Godeps$, builtin$
  skip-dirs:
    - genfiles$
    - vendor$

  # which files to skip: they will be analyzed, but issues from them
  # won't be reported. Default value is empty list, but there is
  # no need to include all autogenerated files, we confidently recognize
  # autogenerated files. If it's not please let us know.
  skip-files:
    - ".*\\.pb\\.go"
    - ".*\\.gen\\.go"


# all available settings of specific linters
linters-settings:
  errcheck:
    # report about not checking of errors in type assetions: `a := b.(MyStruct)`;
    # default is false: such cases aren't reported by default.
    check-type-assertions: false

    # report about assignment of errors to blank identifier: `num, _ := strconv.Atoi(numStr)`;
    # default is false: such cases aren't reported by default.
    check-blank: false
  govet:
    # report about shadowed variables
    check-shadowing: false
  golint:
    # minimal confidence for issues, default is 0.8
    min-confidence: 0.0
  gofmt:
    # simplify code: gofmt with `-s` option, true by default
    simplify: true
  goimports:
    # put imports beginning with prefix after 3rd-party packages;
    # it's a comma-separated list of prefixes
    local-prefixes: istio.io/
  gocyclo:
    # minimal code complexity to report, 30 by default (but we recommend 10-20)
    min-complexity: 10
  maligned:
    # print struct with more effective memory layout or not, false by default
    suggest-new: true
  dupl:
    # tokens count to trigger issue, 150 by default
    threshold: 100
  goconst:
    # minimal length of string constant, 3 by default
    min-len: 3
    # minimal occurrences count to trigger, 3 by default
    min-occurrences: 3
  depguard:
    list-type: blacklist
    include-go-root: false
    packages:
      - github.com/davecgh/go-spew/spew
  misspell:
    # Correct spellings using locale preferences for US or UK.
    # Default is to use a neutral variety of English.
    # Setting locale to US will correct the British spelling of 'colour' to 'color'.
    locale: US
  lll:
    # max line length, lines longer will be reported. Default is 120.
    # '\t' is counted as 1 character by default, and can be changed with the tab-width option
    line-length: 160
    # tab width in spaces. Default to 1.
    tab-width: 1
  unused:
    # treat code as a program (not a library) and report unused exported identifiers; default is false.
    # XXX: if you enable this setting, unused will report a lot of false-positives in text editors:
    # if it's called for subdir of a project it can't find funcs usages. All text editor integrations
    # with golangci-lint call it on a directory with the changed file.
    check-exported: false
  unparam:
    # call graph construction algorithm (cha, rta). In general, use cha for libraries,
    # and rta for programs with main packages. Default is cha.
    algo: cha

    # Inspect exported functions, default is false. Set to true if no external program/library imports your code.
    # XXX: if you enable this setting, unparam will report a lot of false-positives in text editors:
    # if it's called for subdir of a project it can't find external interfaces. All text editor integrations
    # with golangci-lint call it on a directory with the changed file.
    check-exported: false
  nakedret:
    # make an issue if func has more lines of code than this setting and it has naked returns; default is 30
    max-func-lines: 30
  prealloc:
    # XXX: we don't recommend using this linter before doing performance profiling.
    # For most programs usage of prealloc will be a premature optimization.

    # Report preallocation suggestions only on simple loops that have no returns/breaks/continues/gotos in them.
    # True by default.
    simple: true
    range-loops: true # Report preallocation suggestions on range loops, true by default
    for-loops: false # Report preallocation suggestions on for loops, false by default

linters:
  enable:
    - deadcode
    - goimports
    - golint
    - govet
    - gosimple
    - interfacer
    - lll
    - misspell
    - staticcheck
    - structcheck
    - ineffassign
    - unconvert
    - unparam
    - unused
    - varcheck
  enable-all: false
  disable:
    - gocritic
    - prealloc
    - typecheck
    - megacheck
    - nakedret
    - scopelint
    - gochecknoglobals
    - gochecknoinits
    - gosec
      # start here to enable back all gometalinter linters
    - errcheck
    - goconst
    - maligned
  disable-all: false
  presets:
    - bugs
    - unused
  fast: false


issues:
  # List of regexps of issue texts to exclude, empty list by default.
  # But independently from this option we use default exclude patterns,
  # it can be disabled by `exclude-use-default: false`. To list all
  # excluded by default patterns execute `golangci-lint run --help`
  exclude: []

  # Independently from option `exclude` we use default exclude patterns,
  # it can be disabled by this option. To list all
  # excluded by default patterns execute `golangci-lint run --help`.
  # Default value for this option is true.
  exclude-use-default: true

  # Maximum issues count per one linter. Set to 0 to disable. Default is 50.
  max-per-linter: 0

  # Maximum count of issues with the same text. Set to 0 to disable. Default is 3.
  max-same-issues: 0
  1. Go environment: go version && go env
$ go version && go env
go version go1.11.2 linux/amd64
GOARCH="amd64"
GOBIN=""
GOCACHE="/home/admin/.cache/go-build"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH="/home/admin/go"
GOPROXY=""
GORACE=""
GOROOT="/home/admin/linka-tools/go"
GOTMPDIR=""
GOTOOLDIR="/home/admin/linka-tools/go/pkg/tool/linux_amd64"
GCCGO="gccgo"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD=""
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-build140861160=/tmp/go-build -gno-record-gcc-switches"
  1. Verbose output of running: golangci-lint run -v
INFO [config_reader] Config search paths: [./ /home/admin/scanning/task-source/sofa-mosn /home/admin/scanning/task-source /home/admin/scanning /home/admin /home /]
INFO [config_reader] Used config file .golangci.yml
INFO [lintersdb] Active 12 linters: [deadcode goimports golint govet ineffassign interfacer lll misspell structcheck unconvert unparam varcheck]
INFO [lintersdb] Active presets: [bugs unused]
INFO [loader] Go packages loading at mode load deps types and syntax took 548.623388ms
ERRO Running error: context loading failed: failed to load program with go/packages: go [list -e -json -compiled -test=true -export=false -deps=true -find=false -- ./...]: exit status 1: go build github.com/.../pkg/admin: no Go files in
go build github.com/.../pkg/buffer: no Go files in
go build github.com/.../pkg/config: no Go files in
......
@jirfag
Copy link
Contributor

jirfag commented Feb 18, 2019

hi, thank you for reporting!
github.com/.../pkg/buffer: no Go files in means that this package wasn't loaded: most often this happens when dependencies weren't installed. Is it dependency?

@Rico1900
Copy link
Author

hi, thank you for reporting!
github.com/.../pkg/buffer: no Go files in means that this package wasn't loaded: most often this happens when dependencies weren't installed. Is it dependency?

yes, it's dependency, and I probably know the reason. thx for your reply.

@MichaelMure
Copy link

@Rico1900

image

@DonRenando
Copy link

in my case (on jenkins), it solved my problem:
add in .golangci.yml

run:
  modules-download-mode: vendor  

@andig
Copy link

andig commented Jun 2, 2019

Feel I've just hit the same blocker. My program uses modules, so the vendorsolution would not apply.

I'm first running go generate to get around any compile errors, then run linter. The build works locally but fails on travis:

make
rm -rf bin/ pkg/ dist/ *.zip
Generating embedded assets
GO111MODULE=on go generate ./...
go: finding github.com/google/go-github v17.0.0+incompatible
go: finding github.com/gorilla/mux v1.7.2
go: finding github.com/mjibson/esc v0.2.0
go: finding github.com/tcnksm/go-latest v0.0.0-20170313132115-e3007ae9052e
go: finding github.com/gorilla/handlers v1.4.0
go: finding golang.org/x/sys v0.0.0-20190516110030-61b9204099cb
go: finding golang.org/x/tools v0.0.0-20190517183331-d88f79806bbd
go: finding golang.org/x/text v0.3.2
go: finding golang.org/x/crypto v0.0.0-20190513172903-22d7a77e9e5f
go: finding gopkg.in/urfave/cli.v1 v1.20.0
go: finding github.com/google/go-querystring v1.0.0
go: finding github.com/eclipse/paho.mqtt.golang v1.2.0
go: finding github.com/gorilla/websocket v1.4.0
go: finding golang.org/x/sys v0.0.0-20190412213103-97732733099d
go: finding github.com/grid-x/serial v0.0.0-20171015094157-98648473d6ff

golangci-lint run
level=error msg="Running error: context loading failed: failed to load program with go/packages: 
go [list -e -json -compiled -test=true -export=false -deps=true -find=false -- ./...]: exit status 1: 
go build github.com/eclipse/paho.mqtt.golang: no Go files in 
go build github.com/gorilla/handlers: no Go files in 
go build github.com/gorilla/mux: no Go files in 
go build github.com/gorilla/websocket: no Go files in 
go build github.com/grid-x/modbus: no Go files in 
go build github.com/tcnksm/go-latest: no Go files in 
go build gopkg.in/urfave/cli.v1: no Go files in 
"
make: *** [checks] Error 3
The command "make" exited with 2.

Sources are in https://github.com/andig/mbmd/tree/rename.

@adasari
Copy link

adasari commented Sep 26, 2019

may i know fix/workaround to resolve the issue ?

@tpounds tpounds added the bug Something isn't working label Oct 5, 2019
@jirfag
Copy link
Contributor

jirfag commented Oct 15, 2019

you need just to install all dependencies

@tsauvajon
Copy link

Sorry to comment on a closed issue but how to make golangci-lint work with Go Modules?

I had to manually install all the dependencies (go get -u github.com/.../...), while they were already there with Modules (go get) and GO111MODULE was set to on.

Is there anything I'm missing here?


A possible solution, I guess, would be to disable Go Modules, go get, enable modules again, run golangci-lint. I'd like to avoid that path if there is a better solution though

@jiacai2050
Copy link

jiacai2050 commented Nov 4, 2019

@tsauvajon Have you tried this?

run:
  modules-download-mode: vendor 

@oneumyvakin
Copy link

Same via command line: --modules-download-mode vendor

@stephanebruckert
Copy link

Adding a .golintci.yml file would solve the issue locally, but the CI would then show the real problem:

import cycle not allowed

which was a useful error as two files were importing each other

@Hellseher
Copy link

It's failing without flag --moduls-download-mode vendor

level=error msg="Running error: context loading failed: failed to load packages: failed to load with go/packages:
 err: exit status 1:
 stderr: go: github.com/<private-package>:
 invalid version: git fetch -f origin refs/heads/*:refs/heads/* refs/tags/*:refs/tags/* in /home/runner/go/pkg/mod/cache/vcs/<edited>: exit status 128:\n\t
fatal: could not read Username for 'https://github.com': terminal prompts disabled\n"

and it's failing with multiples errors when flag added

multiple (per each modules which is listed in go.mod)

...
...could not import...
...

@kierun
Copy link

kierun commented Oct 23, 2020

#1466 might be related to this one.

As a side note, notice the modules-download-mode: vendor in the configuration file: sadly, this did not help at all.

@nickackerman42
Copy link

nickackerman42 commented Jan 12, 2021

I stopped getting this error after turning on Go Modules like so: GO111MODULE=on golangci-lint .... Then I removed the variable and it continued to work. I'm not sure why.

Also maybe relevant is that $GOPATH was set and that I was executing the linting from within $GOPATH. Maybe the linter assumes Modules are off if you're within $GOPATH and that breaks it somehow?

@yash97
Copy link

yash97 commented Mar 8, 2021

It's failing without flag --moduls-download-mode vendor

level=error msg="Running error: context loading failed: failed to load packages: failed to load with go/packages:
 err: exit status 1:
 stderr: go: github.com/<private-package>:
 invalid version: git fetch -f origin refs/heads/*:refs/heads/* refs/tags/*:refs/tags/* in /home/runner/go/pkg/mod/cache/vcs/<edited>: exit status 128:\n\t
fatal: could not read Username for 'https://github.com': terminal prompts disabled\n"

and it's failing with multiples errors when flag added

multiple (per each modules which is listed in go.mod)

...
...could not import...
...

I am facing the same error does anyone have any solution?Also tried GO111MODULE=on no luck.

@echoface

This comment was marked as off-topic.

@withlin

This comment was marked as off-topic.

@o0tigerliu0o

This comment was marked as off-topic.

@kbakdev
Copy link

kbakdev commented Jun 21, 2021

The answer is simple.

First

go get -u

And then

export GO111MODULE=on

In my case, it worked.

@Hellseher
Copy link

For those who might face this issue:

if you use it in GithHub Actions with some of your dependencies located in private repositories
set TOKEN or SSH key first before go get

@jneese
Copy link

jneese commented Aug 10, 2021

If you get this error only in pipelines then insure your go.sum file for your project is tracked and up to date in remote.

@coanor
Copy link

coanor commented Sep 18, 2021

Faced the same problem, but fixed:

  • I'm still using vendor for packages, but my global GO111MODULE set to on.
  • After add GO111MODULE=off before golangci-lint run, the problem gone.

@emwalker
Copy link

After add GO111MODULE=off before golangci-lint run, the problem gone.

After looking around for a while, this is what finally fixed a similar issue for me. Any idea what the underlying issue might be?

@o0tigerliu0o

This comment was marked as off-topic.

@max-rocket-internet
Copy link

It's not really clear from any of these posts how to solve it if you are using Github actions. Basically you need to add the env var already posted into the golangci/golangci-lint-action step. Here's an example:

jobs:
  golangci:
    name: lint
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - uses: actions/setup-go@v4
        with:
          go-version: '1.20'
          cache: false
        # Add these 2 lines below
        env:
          GO111MODULE: off
      - name: golangci-lint
        uses: golangci/golangci-lint-action@v3
        with:
          version: v1.54

@golangci golangci deleted a comment from o0tigerliu0o Aug 23, 2023
@ahernandez-evi
Copy link

Just in case this is helpful for somebody else: verify that you have a .golangci.yml file in the root of your project. That seemed to fix the issue for me.

@stmcginnis
Copy link

One other thing you can look out for. In my case, I found the issue was actually in my go setup. I wasn't quoting the go version to be installed, so somehow it was being parsed as requesting go 1.2. This older version obviously was not happy with go modules.

      - uses: actions/setup-go@v5
        with:
          go-version: 1.20

Worked after changing to:

      - uses: actions/setup-go@v5
        with:
          go-version: '1.20'

@k3n
Copy link

k3n commented Jun 7, 2024

I'm not sure what's up, but i was facing this as well.

First, I was able to get it working using the CLI only:

golangci-lint run -v --show-stats --no-config --modules-download-mode readonly

Like I said, that worked -- but I wanted to use a config file, so I tried:

run:
  modules-download-mode: readonly
output:
  show-stats: true

Invoked via:

golangci-lint run --config ./.golangci.yml

And that failed with the error from the OP.

I'm sure there's some difference between --no-config and --config ./.golangci.yml, but I don't have time to chase it down right now. I hope this is useful to someone else though.

@bon-ami

This comment has been minimized.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests