Can't see Coverage in SonarQube and calculate .Net code Covergae using Github action(windows runner)

Hello Team,
I’m seeking your help here that I’m unable to see Coverage section in the SonarQube project and also not calculating .Net code Covergae using Github action(windows runner win2k19)

  • I’m using SonarQube Enterprise Edition Version 10.2.1

  • Integrated with Github (workflow action)

  • Building CI pipeline using github action and doing dotnet 4.8 Tests and Analyze Code Coverage

  • I’m using below github workflow action to achieve this use case,

    - name: Run Build BSCO.Service Tests and Analyze Code Coverage
      shell: powershell
      run: |
        &dotnet tool install --global dotnet-sonarscanner | echo true
        $prNumber=${{ github.event.pull_request.number }}          
        if([string]::IsNullOrEmpty($prNumber))
        {
          &dotnet sonarscanner begin -k:BSCO.Service -d:sonar.login=${{ secrets.SONAR_TOKEN }} -d:sonar.host.url=${{ secrets.SONAR_HOST_URL }} -d:sonar.verbose=true -v:${{ needs.generate-build-version.outputs.version-number }} -d:sonar.cs.vstest.reportsPaths=.\BSCOService.XUnit\TestResults\BSCO.Service.TestResults.xml -d:sonar.cs.vscoveragexml.reportsPaths=coverage.xml -d:sonar.branch.name=${{ github.ref_name }}
        }
        else
        {
          &dotnet sonarscanner begin -k:BSCO.Service -d:sonar.login=${{ secrets.SONAR_TOKEN }} -d:sonar.host.url=${{ secrets.SONAR_HOST_URL }} -d:sonar.verbose=true -v:${{ needs.generate-build-version.outputs.version-number }} -d:sonar.cs.vstest.reportsPaths=.\BSCOService.XUnit\TestResults\BSCO.Service.TestResults.xml -d:sonar.cs.vscoveragexml.reportsPaths=coverage.xml -d:sonar.pullrequest.key=${{ github.event.pull_request.number }} -d:sonar.pullrequest.branch=${{ github.head_ref }}  -d:sonar.pullrequest.base=${{ github.event.pull_request.base.ref }}
        }
        &dotnet publish .\BscoService\BscoService.csproj -p:VersionNumber=${{ needs.generate-build-version.outputs.version-number }} -p:Configuration=Release --no-restore
        &dotnet-coverage collect dotnet test .\BSCOService.XUnit\BSCOService.XUnit.csproj /p:VersionNumber=${{ needs.generate-build-version.outputs.version-number }} /p:Configuration=Release --no-build --verbosity normal --logger "trx;LogFileName=BSCO.Service.TestResults.xml" --collect:"Code Coverage" -f xml -o 'coverage.xml' -ll Verbose
        &echo "Merging Code Coverage files...."          
        &Get-ChildItem -Recurse -Filter *.coverage | ForEach-Object { dotnet-coverage merge -o coverage.xml -f xml $_.FullName }
        &echo "Merging Code Coverage files completed...."
        &Start-Sleep -s 30
        &dotnet sonarscanner end -d:sonar.login=${{ secrets.SONAR_TOKEN }}
      working-directory: .\BPOS.Net\Source\Infrastructure\BSCO.Service.App
    

PFA attached sonarqube debug logs
SonarQube-Debug-Logs.txt (4.4 MB)

Hi,

It looks like you generate multiple files and then combine them into coverage.xml. I think the paths in the reports get mangled at that point because here’s what I see in your logs:

2024-01-11T01:05:36.2104123Z 12:05:36.180 INFO: Sensor C# File Caching Sensor [csharp] (done) | time=265ms
2024-01-11T01:05:36.2104716Z 12:05:36.180 INFO: Sensor C# Tests Coverage Report Import [csharp]
2024-01-11T01:05:36.2105898Z 12:05:36.180 DEBUG: Analyzing coverage with wildcardPatternFileProvider with base dir 'C:\actions-runner-a\_work\storesystems\storesystems\BPOS.Net\Source\Infrastructure\BSCO.Service.App\.' and file separator '\'.
2024-01-11T01:05:36.2108127Z 12:05:36.180 DEBUG: Pattern matcher extracted prefix/absolute path 'C:\actions-runner-a\_work\storesystems\storesystems\BPOS.Net\Source\Infrastructure\BSCO.Service.App\.\coverage.xml' from the given pattern 'coverage.xml'.
2024-01-11T01:05:36.2109809Z 12:05:36.196 DEBUG: Pattern matcher returns a single file: 'C:\actions-runner-a\_work\storesystems\storesystems\BPOS.Net\Source\Infrastructure\BSCO.Service.App\.\coverage.xml'.
2024-01-11T01:05:36.2111128Z 12:05:36.196 DEBUG: The current user dir is 'C:\actions-runner-a\_work\storesystems\storesystems\BPOS.Net\Source\Infrastructure\BSCO.Service.App'.
2024-01-11T01:05:36.2112745Z 12:05:36.196 INFO: Parsing the Visual Studio coverage XML report C:\actions-runner-a\_work\storesystems\storesystems\BPOS.Net\Source\Infrastructure\BSCO.Service.App\.\coverage.xml

And then lots of lines like this:

2024-01-11T01:05:36.3037703Z 12:05:36.290 DEBUG: Found 0 indexed files for '/_/src/xunit.runner.visualstudio/Utility/AssemblyResolution/Microsoft.Extensions.DependencyModel/CompilationOptions.cs' (normalized to 'src/xunit.runner.visualstudio/Utility/AssemblyResolution/Microsoft.Extensions.DependencyModel/CompilationOptions.cs'). Will skip this coverage entry. Verify sonar.sources in .sonarqube\out\sonar-project.properties.

Ideally, you’re going to generate straight to coverage.xml, without the intermediate step of combining files, or you’re going to have to add a file-path-normalization step before the end step.

 
Ann

Thanks Ann for getting back with this suggestion.

Do we have any sample code snippet which I can refer with my use case please?

Hi,

The docs should help.

 
Ann