Code coverage was showing with 0% in sonarcloud but showing in Azure pipeline UI

Configured sonarcloud to existing Azure pipeline
Created pipeline with classic editor and added sonar task to it.

image : Ubuntu latest
Use .Net Core sdk 6.0.x
Prepare Analysis on sonarcloud
dotnet build 
dotnet test
Run code analysis
copying files to staging
publish artifact 
Build image using docker
push image to ACR
deploy on AKS

addition properties:
sonar.exclusions=**/obj/**,**/*.dll
sonar.cs.vstest.reportsPaths=$(Agent.TempDirectory)/*.trx
sonar.cs.opencover.reportsPaths=$(Agent.TempDirectory)/**/*.xml

dotnet test
Arguments:
 --collect "Code Coverage" ---> tried with --logger trx --collect:"XPlat Code Coverage"

checked Publish test result as well

On exection of pipeline
can see the code coverage result in Azure pipeline UI but not sonarcloud

on Dotnet test
can see the .trx, coverage.cobertura.xml and .coverage in $(Agent.TempDirectory)

on Run code analysis

INFO: Sensor C# Tests Coverage Report Import [csharp]

INFO: Parsing the OpenCover report /home/vsts/work/_temp/XXXXXXX-XXXXXXXX/packs/Microsoft.NETCore.App.Ref/6.0.19/ref/net6.0/System.Runtime.InteropServices.RuntimeInformation.xml

WARN: Could not import coverage report '/home/vsts/work/_temp/XXXXXXX/packs/Microsoft.NETCore.App.Ref/6.0.19/ref/net6.0/System.Runtime.InteropServices.RuntimeInformation.xml' because 'Missing root element <CoverageSession> in /home/vsts/work/_temp/XXXXXXXX/packs/Microsoft.NETCore.App.Ref/6.0.19/ref/net6.0/System.Runtime.InteropServices.RuntimeInformation.xml at line 2'. Troubleshooting guide: https://community.sonarsource.com/t/37151

INFO: Parsing the OpenCover report /home/vsts/work/_temp/XXXXXXXX/packs/Microsoft.AspNetCore.App.Ref/6.0.19/ref/net6.0/System.IO.Pipelines.xml

Please help me with what I am missing here to get the code coverage result in sonarcloud

Note: I am new to this sonarcloud

Hi,

Can you post the full analysis log?

Share the Scanner for .NET verbose logs

  • Add /d:"sonar.verbose=true" to the…
    • SonarScanner.MSBuild.exe or dotnet sonarscanner begin command to get more detailed logs
      • For example: SonarScanner.MSBuild.exe begin /k:"MyProject" /d:"sonar.verbose=true"
    • “SonarQubePrepare” or “SonarCloudPrepare” task’s extraProperties argument if you are using Azure DevOps
      • For example:
        - task: SonarCloudPrepare@1
            inputs:
              SonarCloud: 'sonarcloud'
              organization: 'foo'
              scannerMode: 'MSBuild'
              projectKey: 'foo_sonar-scanning-someconsoleapp'
              projectName: 'sonar-scanning-someconsoleapp'
              extraProperties: |
                sonar.verbose=true
        
  • The important logs are in the END step (i.e. SonarQubeAnalyze / SonarCloudAnalyze / “Run Code Analysis”)

Share the msbuild detailed logs

MsBuild.exe /t:Rebuild /v:d

or

dotnet build -v:d

 
Ann

Hi Ann Campbell,

Thanks for response and apology for delay in sending the logs.
Sharing with you full analysis log and please go through it and let me know if any inputs required.

logs_11.zip (5.4 MB)

FYI, we are using docker to build the image and then to ACR

Ravi Kumar

Hi Ravi,

For some reason, I can’t open that log.

 
Ann

Hello Ann,

It’s in Zip format, so can you please unzip it and then you open the file.
or you can download it from below link
https://download.winzip.com/gl/nkln/winzip27-downwz.exe

let me know if still facing issues while opening the log file

regards,
Ravi

Hi Ravi,

I did unzip it. Unfortunately my text editor wouldn’t open the unzipped file.

 
Ann

Hi Ann,

Can you try to open on Notepad++ tool and let me know if it works or not.

regards,
Ravi Kumar

Hi Ravi,

I tried it in Geany, and it wouldn’t open.

 
Ann

Hi Ann,

you were unable to open because of huge data so requesting you to download notepad++ tool and then install and try to open it.

link to download the notepad++
https://notepad-plus-plus.org/downloads/v8.5.6/

regards,
Ravi Kumar

Hi Ravi,

Your file is 368.4Mb. I’ve opened 1Gb files with Geany.

Please try with a fresh version of the log.

 
Ann

Hi Ann,

I have downloaded and installed Geany and try to open the txt file but showing unable to see the file.

Whereas I can able to open the same file in Notepad++ without any issue.
So can you please try from your end with Notepad++ tool.

link to download the notepad++
https://notepad-plus-plus.org/downloads/v8.5.6/

if you face any issues while installing Notepad++ then try with Notepad++ portable

regards,
Ravi Kumar

Ravi, I’m on Linux, with no plans to install Wine so I can run Notepad++.

 
Ann

Hi Ann,

resending the zip file(splitted it into two txt files) and I have tried opening in Geany and its working.

Let me know if you still facing issue while opening the txt file

sonar_logs.zip (5.4 MB)

Hi Ann,

Under analysis part:

sonar.exclusions=**/obj/**,**/*.dll
sonar.exclusions=**/ControlTower/**
sonar.cs.opencover.reportsPaths=$(Agent.TempDirectory)/**/*.coverage
sonar.cs.vstest.reportsPaths=$(Agent.TempDirectory)/*.trx
#dotnet-coverage collect "dotnet test" -f xml -o "coverage.xml"
#sonar.verbose=true

I could see these logs under run code analysis

Sensor C# File Caching Sensor [csharp] (done) | time=22ms

INFO: Sensor C# Tests Coverage Report Import [csharp]

INFO: Parsing the OpenCover report /home/vsts/work/_temp/ff2e771c-af27-4a98-9e0d-2c6b73eceacf/vsts_fv-az709-302_2023-08-24.09_36_36.coverage

WARN: Unable to get next XML event while parsing file '/home/vsts/work/_temp/ff2e771c-af27-4a98-9e0d-2c6b73eceacf/vsts_fv-az709-302_2023-08-24.09_36_36.coverage'

WARN: Could not import coverage report '/home/vsts/work/_temp/ff2e771c-af27-4a98-9e0d-2c6b73eceacf/vsts_fv-az709-302_2023-08-24.09_36_36.coverage' because 'Error while parsing the XML file: /home/vsts/work/_temp/ff2e771c-af27-4a98-9e0d-2c6b73eceacf/vsts_fv-az709-302_2023-08-24.09_36_36.coverage'. Troubleshooting guide: https://community.sonarsource.com/t/37151

INFO: Parsing the OpenCover report /home/vsts/work/_temp/_fv-az709-302_2023-08-24_09_36_40/In/fv-az709-302/vsts_fv-az709-302_2023-08-24.09_36_36.coverage

WARN: Unable to get next XML event while parsing file '/home/vsts/work/_temp/_fv-az709-302_2023-08-24_09_36_40/In/fv-az709-302/vsts_fv-az709-302_2023-08-24.09_36_36.coverage'

WARN: Could not import coverage report '/home/vsts/work/_temp/_fv-az709-302_2023-08-24_09_36_40/In/fv-az709-302/vsts_fv-az709-302_2023-08-24.09_36_36.coverage' because 'Error while parsing the XML file: /home/vsts/work/_temp/_fv-az709-302_2023-08-24_09_36_40/In/fv-az709-302/vsts_fv-az709-302_2023-08-24.09_36_36.coverage'. Troubleshooting guide: https://community.sonarsource.com/t/37151

INFO: Sensor C# Tests Coverage Report Import [csharp] (done) | time=126ms

INFO: Sensor C# Unit Test Results Import [csharp]

INFO: Parsing the Visual Studio Test Results file '/home/vsts/work/_temp/_fv-az709-302_2023-08-24_09_36_40.trx'.

INFO: Sensor C# Unit Test Results Import [csharp] (done) | time=62ms

INFO: Sensor Zero Coverage Sensor

INFO: Sensor Zero Coverage Sensor (done) | time=46ms

INFO: SCM Publisher SCM provider for this project is: git

INFO: SCM Publisher 224 source files to be analyzed

Hi,

I still can’t open your first file. I suspect there are some weird/bad characters in it.

So I went back to your OP

This isn’t actually a valid property

Per the docs (emphasis mine)

The remarks for properties that support wildcards will mention this fact. If wildcards are not noted for a given property, then they are not supported for that property.

Here’s what it says for that property:

sonar.cs.opencover.reportsPaths

Path to the OpenCover or Coverlet coverage report.

Thus, wildcards are not supported. You should provide exact paths.

 
Ann

Hi Ann,

I have tried with Windows as Agent with below task:
Use .Net Core sdk 6.0.x
Prepare analysis on SonarCloud
* sonar.exclusions=/obj/,**/*.dll

  • sonar.exclusions=**/ControlTower/**
    
  • sonar.cs.opencover.reportsPaths=$(Agent.TempDirectory)/**/*.coverage
    
  • sonar.cs.vstest.reportsPaths=$(Agent.TempDirectory)/*.trx
    

dotnet build
dotnet test
Run code analysis

And it got executed successfully without any issue and can see the code coverage result both on Azure devops UI and sonarcloud
and if I change Agent to Ubuntu and execute the pipeline then on execution I can see the code coverage result on Azure devops UI but not on Sonarcloud

On dotnet test task,

Code coverage can be collected by adding --collect "Code coverage" option to the command line arguments. This is currently only available on the Windows platform.

That means code coverage wont show result on sonarcloud for Ubuntu Agent ?

Please suggest or guide to add any missing properties for Ubuntu Agent ?

Hi,

I was wrong. sonar.cs.vstest.reportsPaths is a valid test execution parameter. I was looking for it on the coverage list.

But the docs still indicate that wildcards aren’t valid for sonar.cs.opencover.reportsPaths. Perhaps that’s an oversight. Perhaps you got lucky.

I note that both your paths rely on variable interpolation. I’m not sure $(var) is valid on Linux.

 
HTH,
Ann

Hi Ann,

FYI, I can see the code coverage result on azure devops UI.

And JFI…

under dotnet test task:

Under Run code Analysis ---- below logs shows when I executed from Agent Ubuntu

And below logs shows when I execute from Windows as Agent

differ is I don’t see the CodeCoverage.exe when I executed it from Agent Ubuntu where it will convert .coverage to .xml which readable by sonarcloud

So can you please suggest me with the valid parameters that publish the code coverage result to sonarcloud for Ubuntu Agent.

hi Ann,

can you please suggest me with the valid parameters that publish the code coverage result to sonarcloud for Ubuntu Agent.

regards,
Ravi Kumar

Hi Ann,

I apologies on that wild card on properties and I have changed it accordingly.

sonar.cs.opencover.reportsPaths=$(Build.SourcesDirectory)/coveragereport/.xml*
sonar.cs.vstest.reportsPaths=$(Agent.TempDirectory)/.trx*

under dotnet test :
Argument as –collect:“XPlat Code Coverage”

used reportgenerator :
dotnet tool install -g dotnet-reportgenerator-globaltool

reportgenerator -reports:$(Agent.TempDirectory)//coverage.cobertura.xml -targetdir:$(Build.SourcesDirectory)/coveragereport -reporttypes:‘HtmlInline_AzurePipelines;Cobertura’*

And under publish code coverage :
$(Build.SourcesDirectory)/coveragereport/Cobertura.xml

after executing the pipeline shows the result for each task :
under dotnet test result :
Starting test execution, please wait…
A total of 1 test files matched the specified pattern.
Results File: /home/vsts/work/_temp/_fv-az162-636_2023-09-06_12_52_05.trx

Attachments:

  • /home/vsts/work/_temp/06f71caa-7b01-4820-ab00-49f8caa2c320/coverage.cobertura.xml*

under publish code coverage result :
2023-09-06T12:52:24: Arguments

2023-09-06T12:52:24: -reports:/home/vsts/work/1/s/coveragereport/Cobertura.xml

2023-09-06T12:52:24: -targetdir:/home/vsts/work/_temp/cchtml

2023-09-06T12:52:24: -reporttypes:HtmlInline_AzurePipelines

2023-09-06T12:52:25: Writing report file ‘/home/vsts/work/_temp/cchtml/index.html’

2023-09-06T12:52:25: Report generation took 1.1 seconds

Generated code coverage html report: /home/vsts/work/_temp/cchtml

##[warning]Ignoring coverage report directory with Html content as we are auto-generating Html content

Reading code coverage summary from ‘/home/vsts/work/1/s/coveragereport/Cobertura.xml’

Async Command Start: Publish code coverage

Publishing coverage summary data to TFS server.

Lines- 4116 of 6206 covered.

Branches- 355 of 920 covered.

Modifying Cobertura Index file

Publishing code coverage files to TFS server.

Uploading 176 files

Max dedup parallelism: 192

Building file tree

Uploaded 0 out of 28,946,415 bytes.

Uploaded 28,946,415 out of 28,946,415 bytes.

Associating files

Total files: 176 ---- Associated files: 0 (0%)

File upload succeed.

Published ‘/home/vsts/work/_temp/cchtml’ as artifact ‘Code Coverage Report_2874’

Async Command End: Publish code coverage

Finishing: Publish code coverage from /home/vsts/work/1/s/coveragereport/Cobertura.xml

Under run code analysis result :
INFO: Parsing the OpenCover report /home/vsts/work/1/s/coveragereport/Cobertura.xml

WARN: Could not import coverage report ‘/home/vsts/work/1/s/coveragereport/Cobertura.xml’ because ‘Missing root element in /home/vsts/work/1/s/coveragereport/Cobertura.xml at line 3’. Troubleshooting guide: [Coverage] Troubleshooting guide for .NET code coverage import

INFO: Sensor C# Tests Coverage Report Import [csharp] (done) | time=64ms

INFO: Sensor C# Unit Test Results Import [csharp]

INFO: Parsing the Visual Studio Test Results file ‘/home/vsts/work/_temp/_fv-az162-636_2023-09-06_12_52_05.trx’.

Please go through it and suggest me as still unable to see the code coverage result on sonarcloud