Go files are not included in the coverage

Hi All,

I have coverage.out and report.json file in the project root directory, I made sure of it.

This is how my sonar-project properties look like

sonar.projectKey=<company name_github repo name>
sonar.organization=<company name>
# This is the name and version displayed in the SonarCloud UI.
sonar.projectName=<github repo name> 
sonar.projectVersion=1.0
# Mono repo setting
sonar.project.monorepo.enabled=true
sonar.sources=.
sonar.exclusions=**/*_test.go
sonar.tests=.
sonar.test.inclusions=**/*_test.go
# Code Coverage path
sonar.go.coverage.reportPaths=**/coverage.out
sonar.go.tests.reportPaths=test-data-creation/report.json
# Encoding of the source code. Default is default system encoding
sonar.sourceEncoding=UTF-8

my github workflow CI build.yml

name: SonarCloud CI
on:
  push:
    branches:
      - master
  pull_request:
    types: [opened, synchronize, reopened]
jobs:
  sonarcloud:
    name: SonarCloud
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
        with:
          fetch-depth: 0  # Shallow clones should be disabled for a better relevancy of analysis
      - name: SonarCloud Scan
        uses: SonarSource/sonarcloud-github-action@master
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}  # Needed to get PR information, if any
          SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}

build run log

INFO: Sensor Code Quality and Security for Go [go]

[76](https://github.com/<company_name>/<repo_name>/actions/runs/9102625700/job/25022683224?pr=543#step:4:77)INFO: Sensor Code Quality and Security for Go is restricted to changed files only

[77](https://github.com/<company_name>/<repo_name>/actions/runs/9102625700/job/25022683224?pr=543#step:4:78)INFO: 0 source files to be analyzed

[78](https://github.com/<company_name>/<repo_name>/actions/runs/9102625700/job/25022683224?pr=543#step:4:79)INFO: 0/0 source files have been analyzed

[79](https://github.com/<company_name>/<repo_name>/actions/runs/9102625700/job/25022683224?pr=543#step:4:80)INFO: Sensor Code Quality and Security for Go [go] (done) | time=42ms

[80](https://github.com/<company_name>/<repo_name>/actions/runs/9102625700/job/25022683224?pr=543#step:4:81)INFO: Sensor Go Unit Test Report [go]

[81](https://github.com/<company_name>/<repo_name>/actions/runs/9102625700/job/25022683224?pr=543#step:4:82)INFO: Sensor Go Unit Test Report [go] (done) | time=54ms

[82](https://github.com/<company_name>/<repo_name>/actions/runs/9102625700/job/25022683224?pr=543#step:4:83)INFO: Sensor Go Cover sensor for Go coverage [go]

[83](https://github.com/<company_name>/<repo_name>/actions/runs/9102625700/job/25022683224?pr=543#step:4:84)INFO: Load coverage report from '/github/workspace/test-data-creation/coverage.out'

[84](https://github.com/<company_name>/<repo_name>/actions/runs/9102625700/job/25022683224?pr=543#step:4:85)WARN: File 'github.com/<company_name>/<repo_name>/test-data-creation/internal/config/config.go' is not included in the project, ignoring coverage

[85](https://github.com/<company_name>/<repo_name>/actions/runs/9102625700/job/25022683224?pr=543#step:4:86)WARN: File 'github.com/<company_name>/<repo_name>/test-data-creation/internal/profanity/profanity.go' is not included in the project, ignoring coverage

[86](https://github.com/<company_name>/<repo_name>/actions/runs/9102625700/job/25022683224?pr=543#step:4:87)WARN: File 'github.com/<company_name>/<repo_name>/test-data-creation/internal/generator/fake_generator.go' is not included in the project, ignoring coverage

[87](https://github.com/<company_name>/<repo_name>/actions/runs/9102625700/job/25022683224?pr=543#step:4:88)WARN: File 'github.com/<company_name>/<repo_name>/test-data-creation/internal/jsonreader/jsonreader.go' is not included in the project, ignoring coverage

[88](https://github.com/<company_name>/<repo_name>/actions/runs/9102625700/job/25022683224?pr=543#step:4:89)WARN: File 'github.com/<company_name>/<repo_name>/test-data-creation/cmd/test-data-creation/main.go' is not included in the project, ignoring coverage

[89](https://github.com/<company_name>/<repo_name>/actions/runs/9102625700/job/25022683224?pr=543#step:4:90)WARN: File 'github.com/<company_name>/<repo_name>/test-data-creation/internal/generator/age_generator.go' is not included in the project, ignoring coverage

[90](https://github.com/<company_name>/<repo_name>/actions/runs/9102625700/job/25022683224?pr=543#step:4:91)WARN: File 'github.com/<company_name>/<repo_name>/test-data-creation/internal/common/common.go' is not included in the project, ignoring coverage

[91](https://github.com/<company_name>/<repo_name>/actions/runs/9102625700/job/25022683224?pr=543#step:4:92)INFO: Sensor Go Cover sensor for Go coverage [go] (done) | time=29ms

[92](https://github.com/<company_name>/<repo_name>/actions/runs/9102625700/job/25022683224?pr=543#step:4:93)INFO: Sensor IaC Docker Sensor [iac]

[93](https://github.com/<company_name>/<repo_name>/actions/runs/9102625700/job/25022683224?pr=543#step:4:94)INFO: Sensor IaC Docker Sensor is restricted to changed files only

[94](https://github.com/<company_name>/<repo_name>/actions/runs/9102625700/job/25022683224?pr=543#step:4:95)INFO: 0 source files to be analyzed

[95](https://github.com/<company_name>/<repo_name>/actions/runs/9102625700/job/25022683224?pr=543#step:4:96)INFO: 0/0 source files have been analyzed

[96](https://github.com/<company_name>/<repo_name>/actions/runs/9102625700/job/25022683224?pr=543#step:4:97)INFO: Sensor IaC Docker Sensor [iac] (done) | time=107ms

[97](https://github.com/<company_name>/<repo_name>/actions/runs/9102625700/job/25022683224?pr=543#step:4:98)INFO: Sensor TextAndSecretsSensor [text]

[98](https://github.com/<company_name>/<repo_name>/actions/runs/9102625700/job/25022683224?pr=543#step:4:99)INFO: Sensor TextAndSecretsSensor is restricted to changed files only

[99](https://github.com/<company_name>/<repo_name>/actions/runs/9102625700/job/25022683224?pr=543#step:4:100)INFO: Available processors: 2

[100](https://github.com/<company_name>/<repo_name>/actions/runs/9102625700/job/25022683224?pr=543#step:4:101)INFO: Using 2 threads for analysis.

[101](https://github.com/<company_name>/<repo_name>/actions/runs/9102625700/job/25022683224?pr=543#step:4:102)INFO: 4 source files to be analyzed

[102](https://github.com/<company_name>/<repo_name>/actions/runs/9102625700/job/25022683224?pr=543#step:4:103)INFO: 4/4 source files have been analyzed

[103](https://github.com/<company_name>/<repo_name>/actions/runs/9102625700/job/25022683224?pr=543#step:4:104)INFO: Sensor TextAndSecretsSensor [text] (done) | time=598ms

Someone please take a look and let me know what is the mistake I am doing

Hi,

Welcome to the community!

First, I’ll be honest and say that it’s not clear what format you’re supposed to be passing in for Go coverage reports, and the docs aren’t helpful on the matter. (I’ve raised that internally.) So let’s assume that a coverage.out is the right type of file to use.

Then I look at your pipeline (thanks for providing that proactively!) and what I don’t see happening between checkout and analysis is the generation of that report. Or am I misreading it somehow?

 
Ann

Hi Ann,

Thank you for replying
Here is the command to generate the coverage.out using below command.

go test ./… -coverprofile=coverage.out -covermode=count -json > report.json`

I was assuming that coverage file and test report file should be in a certain location as pre generated. and sonar github action can read that.

Please correct me, if my assumption is wrong. And what is the best way to do the process.

Santanu

Hi Santanu,

That GH Actions snippet is provided as a minimal example. Between checkout and analysis you need to insert the creation of your coverage report.

 
HTH,
Ann

Hi Ann,

is there any github example file I can follow?

Santanu

Hi Santanu,

Generating your coverage report is a bit out of scope for us.

 
Ann

Hi Ann,

I know how to generate the coverage out report with github action.

steps:
    - name: Checkout code
      uses: actions/checkout@v4

    - name: Set up Go
      uses: actions/setup-go@v5
      with:
        go-version: '1.22'
    - run: go version

    - name: Install dependencies
      run: go mod download

    - name: Run tests and generate coverage profile
      run: |
        go test -v ./... -coverprofile=coverage.out -covermode=count -json > report.json;

I am asking once I do that how to pass it to sonar to read the file?

Santanu

Hi Santanu,

You’ve already got that part covered with the properties file in your initial post.

 
Ann

Hi Ann,
Thank you!