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 error: context loading failed: failed to load program with go/packages: could not determine GOARCH and Go compiler #21

Open
yarelm opened this issue Dec 30, 2019 · 24 comments

Comments

@yarelm
Copy link

yarelm commented Dec 30, 2019

Hi,
I'm trying to use the linter with one of my private github go projects.

on: [pull_request]
jobs:
  golangci-lint:
    name: runner / golangci-lint
    runs-on: ubuntu-latest
    steps:
      - name: Install Go
        uses: actions/setup-go@v1
        with:
          go-version: 1.13
      - name: Check out code into the Go module directory
        uses: actions/checkout@v1
      - name: Configure git for private modules
        env:
          TOKEN: ${{ secrets.PERSONAL_ACCESS_TOKEN }}
        run: git config --global url."https://MY_USER:${TOKEN}@github.com".insteadOf "https://github.com"
      - name: vendor 
        run: go mod vendor
        env:
          GOPRIVATE: github.com/MYORG/*
      - name: golangci-lint
        uses: reviewdog/action-golangci-lint@v1
        with:
          github_token: ${{ secrets.github_token }}
          golangci_lint_flags: "--config=.golangci.yml -v"

My .golangci.yml file:

run:
  modules-download-mode: vendor

This project uses Go Modules, and references a private repo in Github

the golangci-lint step fails with this error:

level=warning msg="Failed to discover go env: failed to run 'go env': exit status 2"
level=info msg="[lintersdb] Active 10 linters: [deadcode errcheck gosimple govet ineffassign staticcheck structcheck typecheck unused varcheck]"
level=info msg="[loader] Go packages loading at mode 575 (imports|name|types_sizes|compiled_files|exports_file|files|deps) took 5.496611ms"
level=error msg="Running error: context loading failed: failed to load program with go/packages: could not determine GOARCH and Go compiler"```
@haya14busa
Copy link
Member

Can you try reviewdog/action-golangci-lint@v1.1.2? It could be regression by #17.
cc/ @shogo82148

@haya14busa
Copy link
Member

I also released v1.1.5 which uses the latest golangci version ( v1.22). It can also fix the problem, but i'm not sure.

@ClaudiaJ
Copy link

ClaudiaJ commented Jan 6, 2020

Problem reproducible upstream w/ golangci-lint docker image:
golangci/golangci-lint#897

I'm having the same issue in the Action as well as locally w/ the same upstream docker image:

Ran with the step as follows:

      - name: Run golangci-lint with reviewdog
        uses: docker://reviewdog/action-golangci-lint:v1.1
        with:
          # GITHUB_TOKEN.
          github_token: ${{ secrets.GITHUB_TOKEN }}
          golangci_lint_flags: -v --build-tags=integration

Same error given the suggested docker run invocation from golangci-lint readme:

docker run --rm -v $(pwd):/app -w /app golangci/golangci-lint:v1.22-alpine golangci-lint run -v
level=info msg="[config_reader] Config search paths: [./ /app /]"
level=info msg="[lintersdb] Active 10 linters: [deadcode errcheck gosimple govet ineffassign staticcheck structcheck typecheck unused varcheck]"
level=info msg="[lintersdb] Active 10 linters: [deadcode errcheck gosimple govet ineffassign staticcheck structcheck typecheck unused varcheck]"
level=info msg="[loader] Go packages loading at mode 575 (compiled_files|deps|files|imports|exports_file|name|types_sizes) took 671.7705ms"
level=error msg="Running error: context loading failed: failed to load program with go/packages: could not determine GOARCH and Go compiler"
level=info msg="Memory: 8 samples, avg is 68.7MB, max is 68.7MB"
level=info msg="Execution took 681.0258ms"

@haya14busa
Copy link
Member

@ClaudiaJ

Can you try reviewdog/action-golangci-lint@v1.1.2?

#21 (comment)

@ClaudiaJ
Copy link

ClaudiaJ commented Jan 7, 2020

Tested v1.1.2 just now, unfortunately the same error:

Run docker://reviewdog/action-golangci-lint:v1.1.2
  with:
    github_token: ***
    golangci_lint_flags: -v --build-tags=integration
/usr/bin/docker run --name reviewdogactiongolangcilintv112_12e461 --label 671ee6 --workdir /github/workspace --rm -e INPUT_GITHUB_TOKEN -e INPUT_GOLANGCI_LINT_FLAGS -e HOME -e GITHUB_REF -e GITHUB_SHA -e GITHUB_REPOSITORY -e GITHUB_ACTOR -e GITHUB_WORKFLOW -e GITHUB_HEAD_REF -e GITHUB_BASE_REF -e GITHUB_EVENT_NAME -e GITHUB_WORKSPACE -e GITHUB_ACTION -e GITHUB_EVENT_PATH -e RUNNER_OS -e RUNNER_TOOL_CACHE -e RUNNER_TEMP -e RUNNER_WORKSPACE -e ACTIONS_RUNTIME_URL -e ACTIONS_RUNTIME_TOKEN -e GITHUB_ACTIONS=true -v "/var/run/docker.sock":"/var/run/docker.sock" -v "/home/runner/work/_temp/_github_home":"/github/home" -v "/home/runner/work/_temp/_github_workflow":"/github/workflow" -v "/home/runner/work/smoke-stack/smoke-stack":"/github/workspace" reviewdog/action-golangci-lint:v1.1.2
level=info msg="[config_reader] Config search paths: [./ /github/workspace /github /]"
level=info msg="[lintersdb] Active 10 linters: [deadcode errcheck gosimple govet ineffassign staticcheck structcheck typecheck unused varcheck]"
level=info msg="[loader] Go packages loading at mode 575 (types_sizes|compiled_files|deps|imports|name|exports_file|files) took 658.016349ms"
level=error msg="Running error: context loading failed: failed to load program with go/packages: could not determine GOARCH and Go compiler"
level=info msg="Memory: 8 samples, avg is 68.4MB, max is 68.4MB"
level=info msg="Execution took 670.83099ms"
2020/01/07 14:35:10 [golangci-lint] reported: https://github.com/Mattel/smoke-stack/runs/377591212

@haya14busa
Copy link
Member

haya14busa commented Jan 7, 2020

Hmm, it's interesting if v1.1.2 doesn't work.
Just to confirm, does golangci-lint itself work with your repository?

@ClaudiaJ
Copy link

ClaudiaJ commented Jan 7, 2020

It does work outside of Docker, e.g. from host's PATH;
version v1.22.2 built from cb2f8ba on 2019-12-30T19:26:28Z.

The docker container for golangci-lint:v1.22-alpine does not work with the same error as noted above.

@haya14busa
Copy link
Member

Do you use go mod vendor or have private repo dependencies?

@ClaudiaJ
Copy link

ClaudiaJ commented Jan 7, 2020

I do not use go mod vendor, but I do recently as of this PR have private repo dependencies.

Let me try adding this step before golangci-lint:

- name: Configure git for private modules
  env:
    TOKEN: ${{ secrets.PERSONAL_ACCESS_TOKEN }}
  run: git config --global url."https://username:${TOKEN}@github.com".insteadOf "https://github.com"

and get back to you if that works, one moment please

@ClaudiaJ
Copy link

ClaudiaJ commented Jan 7, 2020

Nope, that didn't work. Let me try removing that dependency then and see if that's the cause, or add a step to go mod download before-hand 🤔

@haya14busa
Copy link
Member

It seems golangci-lint doesn't use latest go/packages. https://github.com/golangci/golangci-lint/blob/d3e36a97cdc1c68566b0a274df367a44cd766cfb/go.mod#L55

So it doesn't include this commit to improve the error message.... golang/tools@622ba90

@ClaudiaJ
Copy link

ClaudiaJ commented Jan 7, 2020

I can now confirm that dumping my dependency on private repo does result in the golangci-lint docker image working.

Adding go mod download before the step did not seem to work around this.

@haya14busa
Copy link
Member

go mod download

Probably because downloaded pkgs are not mounted (and i'm not sure how to do that).

Removing --global from git config may work?

@ClaudiaJ
Copy link

ClaudiaJ commented Jan 7, 2020

git config in actions w/o --global fails:
fatal: could not read Username for 'https://github.com': terminal prompts disabled

Not sure what that would buy me either if the downloaded packages aren't being mounted anyway?
I'm figuring either I'd need access to pull private modules from within the container, or I'd need the private modules available to the container so that it wouldn't need to pull them.

@ClaudiaJ
Copy link

ClaudiaJ commented Jan 8, 2020

I can confirm if we could mount the host's modules into the Action's container, this should work.
To test, I've downloaded modules for my project at the host, then tested the same Docker invocation that we found to fail upstream, mounting ${GOPATH}/pkg/mod into the container, and passing --modules-download-mode=readonly

go mod download
docker run --rm \
        -v ${GOPATH}/pkg/mod:/go/pkg/mod \
        -v $(pwd):/app \
        -w /app \
        golangci/golangci-lint:v1.22.2-alpine \
        golangci-lint run -v --modules-download-mode=readonly

Reading GH Action's documentation to see if we can mount a volume now.

@muesli
Copy link

muesli commented Jan 9, 2020

I can also reproduce this and do not have any private Go modules imported.

@uzzz
Copy link

uzzz commented Jan 20, 2020

Having the same issue

@yokogawa-k
Copy link

yokogawa-k commented Jan 22, 2020

Maybe it's because they use actions/setup-go in same job.

I think "golangci-lint image" and "reviewdog image" based on it expect "GOROOT" to be "/go" "/usr/local/go".
But, using actions/setup-go will implicitly replace "GOROOT" with every step in the same job.
(and docker run with "-e GOROOT" options)
Please check the log of "Actions" that is folded.

I think it will be solved by dividing job or override it with env.

https://github.com/actions/setup-go/blob/9fbc767707c286e568c92927bbf57d76b73e0892/lib/installer.js#L122
https://github.com/actions/toolkit/tree/master/packages/core#exporting-variables

@StupidScience
Copy link

Got same error without any private deps.

@qaisjp
Copy link

qaisjp commented Jan 28, 2020

I got this issue by running this on a private repo. I fixed it by making the repo open source. 🧀

@smutel
Copy link

smutel commented Apr 5, 2020

I have the same issue. I found the workaround to remove this error:
level=warning msg="Failed to discover go env: failed to run 'go env': exit status 2".

I set the GOROOT variables only for golangci job (thanks @yokogawa-k)

- name: Lint                                                              
        uses: reviewdog/action-golangci-lint@v1.1.3                             
        env:                                                                    
          GOROOT: "/go"                                                         
        with:                                                                   
          golangci_lint_flags: "--disable-all -E errcheck"                      
          github_token: ${{ secrets.github_token }}

But I still have this issue:
level=error msg="Running error: context loading failed: failed to load program with go/packages: could not determine GOARCH and Go compiler"

Any workaround ?

@yokogawa-k
Copy link

@smutel

My previous GOROOT configuration was wrong, and I misunderstood it as GOPATH. I'm sorry.
However, it is still a problem to replace GOROOT, so could you try to change GOROOT to /usr/local/go (not /go)?

haya14busa added a commit that referenced this issue Aug 15, 2020
https://docs.github.com/en/actions/creating-actions/creating-a-composite-run-steps-action

It should be able to reuse the go build cache and share the same build
context/environment by using the same default workspace instead of using Docker.

It should resolve an issue such as #21 as well.
@haya14busa
Copy link
Member

#46 should fix this issue. Can anyone try the new version?

@yokogawa-k
Copy link

@haya14busa
I tried it on the sample repository for research, the process runs as expected with the old version resulting in an error and the new version with no errors.

Thanks for the fix.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

9 participants