Sonar go not able to import code coverage report

When running sonar to detect for my go project issues, is not able to detect any code coverage

      sonar.language: go
      sonar.sources: .
      sonar.tests: .
      sonar.test.inclusions: "**/*_test.go"
      sonar.sources.inclusions: "**/**.go"
      sonar.go.coverage.reportPaths: coverage.out
      sonar.exclusions: 'useful/**,bat/**, deploy/**, jenkins*/**, my-chart/**, **/*.yaml,*.sh'
      sonar.projectKey: icds-auth-management
      sonar.projectName: icds-auth-management
      sonar.verbose: true

sonarIssue.txt (171.4 KB)

Hello @Mauricio_Gomez and welcome to our Community!

You have some issue with your coverage file, check the log output you provided:

05:18:21.156 INFO: Sensor Go Cover sensor for Go coverage [go]
05:18:21.157 ERROR: Coverage report can't be loaded, report file not found, ignoring this file coverage.out.
05:18:21.158 INFO: Sensor Go Cover sensor for Go coverage [go] (done) | time=2ms

Our scanner could not find the coverage.out file.

1 Like

ok thanks

Yes, i figure it out i needed to run a bash script before doing this:

function runTests {

    if [ -f coverage.out ]; then
      rm coverage.out

    for d in $(GOOS=$GOOS GOARCH=$GOARCH CGO_ENABLED=0 go list ./...); do
       if [ "$d" != 'symantec/ams/unused' ] && [ "$d" != 'symantec/ams/unused/amqp' ]; then
         GOOS=$GOOS GOARCH=$GOARCH CGO_ENABLED=0 go test -coverprofile=profile.out $d
         if [ -f profile.out ]; then
           cat profile.out >> coveragePre.out
           rm profile.out
   echo "mode: set" > coverage.out
   sed  '/^mode/d' coveragePre.out  >> coverage.out
   rm coveragePre.out

quite important as well the mode: set, it only needed one time right ?

Hello @Mauricio_Gomez

Iā€™m unsure to understand what your script is about and what you are trying to do exactly. The typical use-case is to run go test -coverprofile=coverage.out and to import this report as it is.

We do not define/control what is in this report, we take what the coverage tool provides us. So to answer to your question (if I got it correctly): yes, we expect the first line of the report to contain mode: set.

The scripts kind of doing similar to go test just that is doing one by one with whatever the go list reports (which tests packages are out there ) then I create one temporal file to that particular test. Look that I filter out some packages that looks to be unable to compile on this project hence the filter out

At the very end I create the coverage file needed. Look that I need to seed to add only one line to set mode since if it did before one test by one test, each test was adding its own set mode test line and when exported the coverage file was unable to read this weird format

The variable there mention as GOOS or GOARCH not required.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.