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

Caching broken when go.mod not at top level

Open gary-lloyd opened this issue 3 years ago • 1 comments

I'm trying to run the action in a sub directory of my mono repo but it seems that the caching isn't working properly meaning my linting job is always slow as it has to build from scratch.

Step defined as

- name: golangci-lint
   uses: golangci/golangci-lint-action@v2
     with:
       version: v1.41
       working-directory: service

I noticed the following in the post Job action log Cache hit occurred on the primary key golangci-lint.cache-2707-nogomod, not saving cache.

which surprised me as I do have a go.mod file.

Looking at the code for the action I think it is because the cache key build doesn't respect the change of working directory and as the linter is running in the top level of the repository looks for the go.mod file there.

I've been able to work around by handling caching myself as below but would reduce complexity if the listing action could handle this.

golang-lint-with-cache:
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v2

      - uses: actions/setup-go@v2
        with:
          go-version: '^1.17.3'

      - id: go-cache-paths
        run: |
          echo "::set-output name=go-build::$(go env GOCACHE)"
          echo "::set-output name=go-mod::$(go env GOMODCACHE)"

      - name: Go Build Cache
        uses: actions/cache@v2
        with:
          path: ${{ steps.go-cache-paths.outputs.go-build }}
          key: ${{ runner.os }}-go-build-${{ hashFiles('**/go.sum') }}

      - name: Go Mod Cache
        uses: actions/cache@v2
        with:
          path: ${{ steps.go-cache-paths.outputs.go-mod }}
          key: ${{ runner.os }}-go-mod-${{ hashFiles('**/go.sum') }}

      - name: golangci-lint
        uses: golangci/golangci-lint-action@v2
        with:
          version: v1.41
          working-directory: service
          skip-pkg-cache: true
          skip-build-cache: true
          skip-go-installation: true

gary-lloyd avatar Nov 19 '21 00:11 gary-lloyd

I think the caching worked for you, it even says so in the message: "Cache hit occurred". Not entirely as expected, though, as it ignores the go.mod, as you noticed. This seems to be due to the working-directory input being used only for running the linter ("run golangci-lint"), but not for restoring the cache (happens in "prepare environment"), during which go.mod existence is checked, and which runs in the repository root.

https://github.com/golangci/golangci-lint-action/blob/009d23987ca1c6b5c26c83318bfdb6974971f298/src/cache.ts#L63

I suppose either working-directory input should be respected during environment preparation or another input should be provided.

okonos avatar Apr 01 '22 08:04 okonos

Fixed by #629

ldez avatar May 04 '24 00:05 ldez