actions/setup-go

Add $GOPATH/bin to $PATH

lmittmann opened this issue Β· 25 comments

When installing tools via go get (e.g. golint) they can not be ran without adding $GOPATH/bin to $PATH beforehand.

So for using e.g. golint one would need to write a step:

- run: |
    export PATH=$PATH:$(go env GOPATH)/bin
    go get -u golang.org/x/lint/golint
    golint -set_exit_status ./...

It would be cool, if setup-go could already do the export PATH=$PATH:$(go env GOPATH)/bin part, as this is a common need.

geoah commented

GOBIN would be a better choice I think for this

$GOBIN could be used as well, but it also needs to be added to $PATH. Since go env GOBIN is not set and $GOPATH/bin is the default installation path it's just simpler to add the default path to $PATH.

I think this should probably be resolved together with #12. Given my most recent comment in that issue, are you ok with closing here and following up there?

@damccorm Let's track this issue here, as it seems like #12 is quite fuzzy.

mvdan commented

I think you're overthinking this. The official Go docker images simply hard-code PATH, as you can see here: https://github.com/docker-library/golang/blob/3a6407a6ff134ef6a0364ac061b0808f990ea14e/1.13-rc/alpine3.10/Dockerfile#L59-L60

Why not do the same? If anyone wants to set up a custom GOPATH or GOBIN, they can set up a custom PATH too. I'd prefer PATH to be simple and predictable.

You can avoid this issue by instead using the actions container param to run all the commands in the official golang Docker image. You can see what that looks like here:
https://github.com/stellar/go-xdr/blob/3aa3546/.github/workflows/build.yml

mvdan commented

@leighmcculloch I assume that won't work for testing on mac and windows, though, so fixing this bug is still very useful.

I prefer this solution below. This works on windows, ubuntu, and mac.

    steps:
    - name: setup go
      uses: actions/setup-go@v1
      with:
        go-version: 1.x
    - name: setup env
      run: |
        echo "::set-env name=GOPATH::$(go env GOPATH)"
        echo "::add-path::$(go env GOPATH)/bin"
      shell: bash
    - name: checkout
      uses: actions/checkout@v1
      with:
        fetch-depth: 1
        path: src/github.com/${{ github.repository }}

https://stackoverflow.com/a/59242962/104080

@sanemat that still doesn't look right, as there is a bug in the checkout for that path property/option.

To get it to work, I had to hardcode my path - which is unacceptable to me in a github action as things should be evaluated per ENVs and template vars:

  go-test:
    strategy:
      matrix:
        go-version: [1.7.x, 1.13.x]
        platform: [ubuntu-latest, macos-latest, windows-latest]
    runs-on: ${{ matrix.platform }}
    steps:
    - name: setup env
      shell: bash
      run: |
        echo "::set-env name=GOPATH::${{ github.workspace }}/go"
        echo "::add-path::${{ github.workspace }}/go/bin"
    - name: Install Go
      if: success()
      uses: actions/setup-go@v1
      with:
        go-version: ${{ matrix.go-version }}
    - name: checkout
      uses: actions/checkout@v1
      with:
        fetch-depth: 1
        path: podcast/go/src/github.com/${{ github.repository }}
    - name: Run tests
      run: |
        go test -v -covermode=count 

Notice the path: podcast/go/src/github.com/${{ github.repository }}, which is prefixed with podcast because for some reason, $GITHUB_WORKSPACE is actually:

/home/runner/work/podcast/podcast/

Not what the path downloads to, which is:

/home/runner/work/podcast/

To make matters worse, you cannot access this above directory as there is no ENV var that sets it to be consumed. So it has to be hardcoded!

hopefully we can get to this soon as part of v2-beta.

So, the devs on the checkout action told me there was a newer version @v2 that fixes the WORKSPACE issue.

This worked for all of my other actions, and I can remove the hardcoded podcast prefix I previously had to do in the path attribute above.

  go-test:
    strategy:
      matrix:
        go-version: [1.7.x, 1.13.x]
        platform: [ubuntu-latest, macos-latest, windows-latest]
    runs-on: ${{ matrix.platform }}
    steps:
    - name: setup env
      shell: bash
      run: |
        echo "::set-env name=GOPATH::${{ github.workspace }}/go"
        echo "::add-path::${{ github.workspace }}/go/bin"
    - name: Install Go
      if: success()
      uses: actions/setup-go@v1
      with:
        go-version: ${{ matrix.go-version }}
    - name: checkout
      uses: actions/checkout@v2
      with:
        fetch-depth: 1
        path: go/src/github.com/${{ github.repository }}
    - name: Run tests
      shell: bash
      run: |
        cd $GOPATH/src/github.com/${{ github.repository }}
        go test -v -covermode=count 

I'm happy with this for now, as it's now abstracted and I can copy it across all of my repos with no hardcoding of the repo itself.

However, it still seems very verbose: having to set the GOPATH and BIN in other actions, as well as specifying BASH everywhere so I can pickup that GOPATH.

I completely agree with @eduncan911, also because of this issue I have to run cd $GOPATH/src/github.com/${{ github.repository }} in every step..

- name: Lint
  run: |
    GOBIN=$PWD/bin go install golang.org/x/lint/golint
    ./bin/golint ./...

I'm working on this today. Sorry for the delay, I've been busy :)

I'm adding it to the v2-beta as v1 is sealed and new features are going into the v2 line.

I added this feature to the v2-beta. If some folks here can validate, I can take out of beta.

Here's it is being used and as you can see in "path" step, it's being added to the path now: https://github.com/bryanmacfarlane/actions-playground/runs/537349478?check_suite_focus=true

@bryanmacfarlane works on all my repos that were using #41, thanks for implementing this! πŸŽ‰

Some examples:

@fsouza - awesome. Note that you can also use setup-go@v2-beta and I'll make it v2 soon.

Closing since solution is verified. If anyone has any issues trying it out with @v2-beta let me know and I'll look into it.

@bryanmacfarlane
Hi, I just tried v2 and still having this problem.

Install
0s
##[error]Process completed with exit code 1.
Run go install -v ./...
go install: no install location for directory /home/runner/work/kata/kata/cmd/kata02 outside GOPATH
	For more details see: 'go help gopath'
go install: no install location for directory /home/runner/work/kata/kata/cmd/kata19 outside GOPATH
	For more details see: 'go help gopath'
##[error]Process completed with exit code 1.

https://github.com/arvenil/kata/runs/609649648?check_suite_focus=true
Any ideas?

Nevermind. I got it working. The issues is that package is being put outside of GOPATH by setup-go. So I tried to go mod init; go mod vendor however another issue was that this "toy" project didn't had any other dependencies beside standard library so go mod refused to create vendor dir. I then manually created dummy vendor dir and it worked! Go tooling switched from treating this as project that should be in GOPATH to project that is using vendor dir even though there are no vendored dependencies ;)

mkdir vendor
touch vendor/modules.txt
ydnar commented

@arvenil does your go.mod have a Go 1.14 line in it? Is it in a monorepo with an existing vendor directory?

ydnar commented

@arvenil to fix this, either:

  1. Remove the Go 1.14 line from your go.mod file, or…
  2. Rename vendor to something else. If you have Ruby, for instance, move vendor/bundle to .bundle/vendor and cache gems there.

We had this problem and ended up doing (2).

$GOBIN could be used as well, but it also needs to be added to $PATH. Since go env GOBIN is not set and $GOPATH/bin is the default installation path it's just simpler to add the default path to $PATH.

I know it's just simple common sense to you but to me... you're a freakin' GENIUS! πŸ’Žβœ¨πŸ‘

It makes so much fucking sense to me now!