Hi everyone,
we are using SonarCloud with automatic analysis and are overall happy with it.
Now we want to add coverage results for out GoLang projects but I cannot get it to work. I switched from automatic analysis to ci-based analysis and configured the sonarcloud-github-action (see below) but I cannot get the coverage results in the project.
This feature is only available for CI-based analysis. It is not available for automatic analysis.
Source: Test coverage & SonarCloud
Our Code structure
We have a mono repo with all our GoLang projects that looks like the following. During build/test cycle we generate the coverage.out files.
go-project-1/coverage.out
go-project-1/go.mod
...
go-project-2/coverage.out
go-project-2/go.mod
...
go-project-3/coverage.out
go-project-3/go.mod
...
This is the config for the GH action that we used.
- name: SonarCloud Scan
uses: SonarSource/sonarcloud-github-action@master
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
with:
args: >
-Dsonar.organization=${{ secrets.SONARCLOUD_ORGANIZATION }}
-Dsonar.projectKey=${{ secrets.SONARCLOUD_PROJECT_KEY }}
-Dsonar.sources=.
-Dsonar.tests=.
-Dsonar.test.inclusions=**/*.spec.ts,**/*.spec.tsx,**/*.test.ts,**/*.test.tsx
-Dsonar.go.coverage.reportPaths=go-project-1/coverage.out,go-project-2/coverage.out,go-project-3/coverage.out
In the GH action logs I can see that it is loading the coverage.out files but it is writing a warning that the files are not included in the project and that they are being ignored.
INFO: Load coverage report from '/github/workspace/go-project-1/coverage.out'
INFO: Load coverage report from '/github/workspace/go-project-2/coverage.out'
INFO: Load coverage report from '/github/workspace/go-project-3/coverage.out'
WARN: File 'github.com/<GITORG>/go-project-1/internal/routes/regions.go' is not included in the project, ignoring coverage
WARN: File 'github.com/<GITORG>/go-project-1/internal/routes/ping.go' is not included in the project, ignoring coverage
WARN: File 'github.com/<GITORG>/go-project-1/internal/routes/organizations.go' is not included in the project, ignoring coverage
...
This is what the coverage.out files look like
mode: atomic
github.com/dash0hq/go-project-1/internal/config/config.go:65.83,69.49 3 0
github.com/dash0hq/go-project-1/internal/config/config.go:69.49,72.3 2 0
github.com/dash0hq/go-project-1/internal/config/config.go:74.2,76.16 3 0
github.com/dash0hq/go-project-1/internal/config/config.go:76.16,78.3 1 0
github.com/dash0hq/go-project-1/internal/config/config.go:79.2,82.16 4 0
...
What we tried
1. Using projectBaseDir
The only thing that worked for us is creating a separate project in SonarCloud for each GoLang project, and running the GH action for each projectBaseDir.
uses: sonarsource/sonarcloud-github-action@master
with:
projectBaseDir: go-project-1
...
We don’t like that approach since this would create a lot of separate projects in SonarCloud. This would also make PR reviews harder.
2. global go.mod project
We also tried creating a global go.mod project in the root project with a Go file that imports all other go projects just for the analysis. We saw a similar solution somewhere online (can’t find the link anymore) but that did not work.
Has anyone figured out a way how to get this working?