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

feat: Github Actions - set working directory in step #66

Open
pixo-wnobrien opened this issue Feb 14, 2024 · 7 comments
Open

feat: Github Actions - set working directory in step #66

pixo-wnobrien opened this issue Feb 14, 2024 · 7 comments

Comments

@pixo-wnobrien
Copy link

I have a repo with a couple different projects in it, and I would like to set up a workflow that tests all the projects on a PR. It would be great if I could set the working directory in the step like below:

      - name: Check Test Coverage
        uses: vladopajic/go-test-coverage@v2
        with:
          working-directory: app1
          config: .coverage.yaml

      - name: Check Test Coverage
        uses: vladopajic/go-test-coverage@v2
        with:
          working-directory: app2
          config: .coverage.yaml

I usually use golangci-lint in the same pipeline before I run my tests, and the Github Action for that works in this way

      - name: Lint
        uses: golangci/golangci-lint-action@v3
        with:
          version: v1.54
          working-directory: ./app1

I've tried many different workarounds, but I'm having a hard time getting it to work in my repo with multiple projects without this feature. If there are any suggestions that would be greatly appreciated! I put my workflow file below for reference

jobs:
  test-app1:
    runs-on: ubuntu-latest

    defaults:
      run:
        working-directory: ./app1

    steps:
      - uses: actions/checkout@v3

      - name: Setup Go
        uses: actions/setup-go@v4
        with:
          go-version: '1.21'

      - name: Lint
        uses: golangci/golangci-lint-action@v3
        with:
          version: v1.54
          install-mode: "goinstall"
          args: --timeout=30m
          working-directory: ./app1

      - name: Run Tests
        run: make test

      - name: Check Test Coverage
        uses: vladopajic/go-test-coverage@v2
        with:
          config: app1/.coverage.yaml

  test-app2:
    runs-on: ubuntu-latest

    defaults:
      run:
        working-directory: ./app2

    steps:
      - uses: actions/checkout@v3

      - name: Setup Go
        uses: actions/setup-go@v4
        with:
          go-version: '1.21'

      - name: Lint
        uses: golangci/golangci-lint-action@v3
        with:
          version: v1.54
          install-mode: "goinstall"
          args: --timeout=30m
          working-directory: ./app2

      - name: Run Tests
        run: make test

      - name: Check Test Coverage
        uses: vladopajic/go-test-coverage@v2
        with:
          config: app2/.coverage.yaml

permissions:
  contents: read

I can configure the step to use the corresponding cover.out file, but since the step is not running in the directory that the tests were run in I get the following error:

failed to generate coverage statistics: could not find file [github.com/org/repo/app1/docs/docs.go]: can't find "docs.go": no required module provides package github.com/org/repo/app1/docs: go.mod file not found in current directory or any parent directory; see 'go help modules'
@pixo-wnobrien pixo-wnobrien changed the title Github Actions - set working directory in step feat: Github Actions - set working directory in step Feb 14, 2024
@vladopajic
Copy link
Owner

@pixo-wnobrien hey, sorry for late response. could you read this #55, and try two options that have been explained here

  1. use v2.8.1 version
  2. set local prefix for both apps (github.com/org/repo/app1 and github.com/org/repo/app2)

please let me know if this has resolved your issue.

@snicol
Copy link

snicol commented Apr 2, 2024

I'm having a similar issue, with a monorepo containing a single Go module nested a few directories inside. I think the issue is similar to in @pixo-wnobrien's case, github.com/org/repo/app1 and github.com/org/repo/app2 likely have their own separate go.mod files and aren't discoverable, neither is there a go.work file present.

Setting local-prefixes also doesn't seem to solve the issue in my case as the relative path to the module and the module name is not 1:1.

@vladopajic
Copy link
Owner

hello gents,

i have started to work on feature that will allow user to set source directory. this feature should allow use of this tool in monorepos. could you please test it out and give me insights?

to test it you can use 028b5bedcf5d71e9e59fcc5ca7219ec772197382 version.

in ci:

- name: check test coverage (app 1)
  uses: vladopajic/go-test-coverage@028b5bedcf5d71e9e59fcc5ca7219ec772197382
  with:
    config: ./.testcoverage_app1.yml
     source-dir: ./app1
     
- name: check test coverage (app 2)
  uses: vladopajic/go-test-coverage@028b5bedcf5d71e9e59fcc5ca7219ec772197382
  with:
    config: ./.testcoverage_app2.yml
     source-dir: ./app2

locally:

go install github.com/vladopajic/go-test-coverage/v2@028b5bedcf5d71e9e59fcc5ca7219ec772197382
go-test-coverage -config=./.testcoverage_app1.yml -source-dir=app1

@shettyh
Copy link

shettyh commented Apr 5, 2024

@vladopajic tried with the commit provided, it still doesnt work after providing source-dir also

go.mod file not found in current directory or any parent directory

@snicol
Copy link

snicol commented Apr 5, 2024

Same error here, have altered the full output to make it clearer (new lines my own):

failed to generate coverage statistics: could not find file [github.com/org/repo/subdir/module/package/example.go]: 
can't find file "github.com/org/repo/subdir/module/package/example.go": no required module provides package github.com/org/repo/subdir/module/package: 
go.mod file not found in current directory or any parent directory; see 'go help modules'

In the above, the go.mod lives in github.com/org/repo/subdir/module, and the coverage stats generation fails when analysing the cover output of a sub-package of the module (github.com/org/repo/subdir/module/package).

@vladopajic
Copy link
Owner

vladopajic commented Apr 9, 2024

hello gents,

not sure how do you actually want to setup you repositories. it would require me to see in the details your project setup to understand what is happening.

please feel free to this example https://github.com/vladopajic/monorepo-covrage where coverage check is configured to work with monorepo. would something like this work for you?

@krhubert
Copy link

@vladopajic The in monorepo-covrage lacks the github-action setup. Please try to set github actions in this repo for both projects and you will see the problem that issue describes.

jobs:
  backend-unit-test:
    runs-on: ubuntu-latest
    defaults:
      run:
        working-directory: proja
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-go@v4

      - name: run tests
        run: go test -v -cover -coverprofile=test.cover.out ./...

      - name: check test coverage
        # run go-test-coverage as a go command - this is a workaround I use today
        # to make sure go-test-coverage is executed inside proja dir
        run: go run github.com/vladopajic/go-test-coverage/v2 -c .testcoverage.yml

        # ideal solution would be to pass a working-directory and go-test-coverage
        # change the dir and execute the cover change there. 
        uses: vladopajic/go-test-coverage@v2
        with:
          config: .testcoverage.yml
          working-directory: proja

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

5 participants