SonarCloud coverage for Go modules

My Go project repository with several modules is structured like this:

project/
├─ dist/
│  ├─ mod_a-coverage.out
│  ├─ mod_a-report.json
│  ├─ mod_b-coverage.out
│  ├─ mod_b-report.json
├─ mod_a/
│  ├─ go.mod
│  ├─ ...
├─ mod_b/
│  ├─ go.mod
│  ├─ ...
├─ sonar-project.properties

My Go project’s sonar-project.properties looks like this:

sonar.projectKey=<project>
sonar.organization=<org>

sonar.sources=.
sonar.tests=.
sonar.test.inclusions=**/*_test.go
sonar.test.exclusions=**/vendor/**

# =====================================================
#   Properties specific to Go
# =====================================================

sonar.go.tests.reportPaths=dist/report.json
sonar.go.coverage.reportPaths=dist/coverage.out

When I push commit to a PR, I get following logs in Github Actions:

INFO: Load project repositories (done) | time=947ms
INFO: SCM collecting changed files in the branch
WARN: Could not find ref: develop in refs/heads, refs/remotes/upstream or refs/remotes/origin
INFO: SCM collecting changed files in the branch (done) | time=10ms
INFO: Indexing files...
INFO: Project configuration:
INFO:   Excluded sources: **/build-wrapper-dump.json, **/*_test.go
INFO:   Included tests: **/*_test.go
INFO:   Excluded tests: **/vendor/**
INFO: 48 files indexed
INFO: Quality profile for go: Sonar way
INFO: ------------- Run sensors on module <project>
INFO: Load metrics repository
INFO: Load metrics repository (done) | time=760ms
INFO: Sensor cache enabled
INFO: Load sensor cache
INFO: Load sensor cache (41 KB) | time=1360ms
INFO: Sensor Code Quality and Security for Go [go]
INFO: Sensor Code Quality and Security for Go is restricted to changed files only
INFO: 0 source files to be analyzed
INFO: 0/0 source files have been analyzed
INFO: Sensor Code Quality and Security for Go [go] (done) | time=31ms
INFO: Sensor Go Unit Test Report [go]
INFO: Sensor Go Unit Test Report [go] (done) | time=184ms
INFO: Sensor Go Cover sensor for Go coverage [go]
INFO: Load coverage report from '/github/workspace/dist/coverage.out'
WARN: File 'github.com/<org>/<project>/mod_a/b/c/x.go' is not included in the project, ignoring coverage
WARN: File 'github.com/<org>/<project>/mod_a/b/c/y.go' is not included in the project, ignoring coverage
...

I came across this thread and understand the the problem is relate to this comment, specifically to GOPATH and GOROOT settings.

The thing is that we don’t use GOPATH in our project since it is optional since Go 1.11. We use Go modules instead. What SonarCloud identifies as github.com/<org>/<project>/mod_a/b/c/x.go is actually a file located in the mod_a/b/c/x.go(relative to the repository root) where mod_a is a module root and mod_a/go.mod contains the Go module definition: module github.com/<org>/<project>/mod_a

I also saw this ticket but I don’t think it applies in my case since the Go module is not at the project root.

How can I configure SonarCloud to work with Go modules in a monorepo setup where there are multiple Go modules in a single Github repository?

Hello @Ivan_Ribakov ,

Unfortunately, the go coverage sensor does not support mono-repositories. We are aware of the issue and there is a ticket for it already: SONARSLANG-642

We will try to cover this case during our next sprint on the Go analyzer, but it is not yet scheduled.

Cheers,
Michael

Are there any known workarounds?

What if I pointed to the module root with sonar.projectBaseDir and placed coverage reports in the module root as well? Would that work?