Sonar scanner fails to load Go test results

So I figured it out. The secret was in Cameron’s properties file, but it’s not clear what it is.

When I ran with just this (omitting projectKey and projectName):

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

I got code coverage, but not test results. So my assumption was that the default values for sonar.sources and sonar.tests was correct and the issue was in how tests were processed. This assumption turned out to be wrong (as they generally are).

I cloned the Sonar Slang repo at https://github.com/SonarSource/slang, added additional debugging output, and built and ran the sonar-go plugin locally. What it showed was that no test files were being picked up, even with sonar.test.inclusions being specified.

I added the sonar.tests=. line to the properties file and test results started being processed, but then code coverage wasn’t being reported. Finally I added sonar.sources=. and both code coverage and tests results showed up in Sonar Cloud.

The best I can figure is that sonar.sources was defaulting correctly, but sonar.tests wasn’t. When I added sonar.tests=. that then broke the default value for sonar.sources and both have to be supplied even if you’re just using the default values. Why is it like this? I’m not that good at Java, so I don’t know.

Here’s my final sonar-project.properties file:

sonar.projectKey=my-project-key
sonar.projectName=my-project-name
sonar.sources=.
sonar.exclusions=**/*_test.go,**/*_generated*.go,**/*_generated/**,**/vendor/**
sonar.tests=.
sonar.test.inclusions=**/*_test.go
sonar.test.exclusions=**/*_generated*.go,**/*_generated/**,**/vendor/**
sonar.go.tests.reportPaths=report.json
sonar.go.coverage.reportPaths=coverage.out

So Cameron had it right by including the sonar.sources and sonar.tests properties, but I was leaving them out thinking they would default correctly when they didn’t.

4 Likes