Code Coverage Not working with .NET Applications using .coverage file

Hi all,

so im trying to make the code coverage working in my report.

I’m using azure devops on premise and therefore i have activated the ‘Enable Coverage’ option in the MSTest task (im using VS Entreprise)

So I do see that i have a .Coverage file being properly generated:

In my Test task run:

Attachments:
D:\agents\xxx_work\231\s\TestResults\5d23d3e6-3a57-42b5-a702-792ab09ac2dc\2024-11-15.16_21_07.coverage
Test Run Successful.

The file is indeed present and i have verified that it does contains code coverage report and percentages.

Then in the Run Code Analysis Sonar qube task, the .coverage file does seem picked up and analyzed:

Calling the TFS Processor executable...

Fetching code coverage report information from TFS...

Attempting to locate a test results (.trx) file...

Looking for TRX files in: D:\agents\xxx\_work\231\TestResults, D:\agents\xxx\_work\231\s\TestResults

The following test results files were found: D:\agents\PC393-WS10-D\_work\231\s\TestResults\2024-11-15_16_20_45.trx

Absolute path to coverage file: D:\agents\xxx\_work\231\s\TestResults\2024-11-15_16_20_45\In\xxx\2024-11-15.16_21_07.coverage

The following code coverage attachments were found from the trx files: D:\agents\xxx\_work\231\s\TestResults\2024-11-15_16_20_45\In\xxx\2024-11-15.16_21_07.coverage

Not using the fallback mechanism to detect binary coverage files.

Cxxonverting coverage file 'D:\agents\x\_work\231\s\TestResults\2024-11-15_16_20_45\In\xxx\2024-11-15.16_21_07.coverage' to 'D:\agents\xxx\_work\231\s\TestResults\2024-11-15_16_20_45\In\x\2024-11-15.16_21_07.coveragexml'.

Coverage report conversion completed successfully.

The TFS Processor has finished

Calling the SonarScanner CLI...

But then later there seems to be some issue locating the file it just converted??

INFO: ------------- Run sensors on module App.Desktop

INFO: Sensor HTML [web]

INFO: Sensor HTML [web] (done) | time=0ms

INFO: Sensor XML Sensor [xml]

INFO: 1 source file to be analyzed

INFO: 1/1 source file has been analyzed

INFO: Sensor XML Sensor [xml] (done) | time=15ms

INFO: Sensor JaCoCo XML Report Importer [jacoco]

INFO: 'sonar.coverage.jacoco.xmlReportPaths' is not defined. Using default locations: target/site/jacoco/jacoco.xml,target/site/jacoco-it/jacoco.xml,build/reports/jacoco/test/jacocoTestReport.xml

INFO: No report imported, no coverage information will be imported by JaCoCo XML Report Importer

INFO: Sensor JaCoCo XML Report Importer [jacoco] (done) | time=0ms

INFO: Sensor IaC CloudFormation Sensor [iac]

INFO: 0 source files to be analyzed

INFO: 0/0 source files have been analyzed

INFO: Sensor IaC CloudFormation Sensor [iac] (done) | time=16ms

INFO: Sensor IaC AzureResourceManager Sensor [iac]

INFO: 0 source files to be analyzed

Im not sure if i need to stepup something special but it was my understanding that this should be done automatically if we were using the VS Entreprise code coverage feature?

Thank you,

Eric

Hi Eric,

Welcome to the community!

This is a red herring. The JaCoCo sensor runs every time, looking for reports, even though it’s completely irrelevant for C# projects.

Which of the supported .NET coverage tools are you using?

 
Ann

Hi!

Im using Visual Studio Code Coverage (using the Entreprise license).

I noticed this JaCoco sensor indeed, are you saying that it is overwritting the .NET sensor which actually found a report and replaced it with and empty report (since the jacoco found nothing?)

I do have a UI test project which contains some JavaScripts. Maybe this is why it tries to find a java code coverage report.

I’ll try to exclude the UI test project from the build and re-run it to see if it changes anything.

However I dont have JS in all my projects and does that doesnt have any JS scripts are behaving like this too.

Hi,

No. That’s not at all what I’m saying. What I’m saying is that the JaCoCo logging is entirely irrelevant. You can completely ignore it.

The docs should help.

 
Ann

Hi Ann,

thank you for the prompt reply.

I did follow this documentation.

In there it states that using Visual Studio Code Coverage that it should automatically generate the XML.

From what i can see in the log it seems to be detected and working properly. Not too sure why it doesnt show in the report though…

Calling the TFS Processor executable...

Fetching code coverage report information from TFS...

Attempting to locate a test results (.trx) file...

Looking for TRX files in: D:\agents\xxx\_work\231\TestResults, D:\agents\xxx\_work\231\s\TestResults

The following test results files were found: D:\agents\PC393-WS10-D\_work\231\s\TestResults\2024-11-15_16_20_45.trx

Absolute path to coverage file: D:\agents\xxx\_work\231\s\TestResults\2024-11-15_16_20_45\In\xxx\2024-11-15.16_21_07.coverage

The following code coverage attachments were found from the trx files: D:\agents\xxx\_work\231\s\TestResults\2024-11-15_16_20_45\In\xxx\2024-11-15.16_21_07.coverage

Not using the fallback mechanism to detect binary coverage files.

Cxxonverting coverage file 'D:\agents\x\_work\231\s\TestResults\2024-11-15_16_20_45\In\xxx\2024-11-15.16_21_07.coverage' to 'D:\agents\xxx\_work\231\s\TestResults\2024-11-15_16_20_45\In\x\2024-11-15.16_21_07.coveragexml'.

Coverage report conversion completed successfully.

The TFS Processor has finished

Calling the SonarScanner CLI...

I can confirm that the .coverage does have a non-0% code coverage.

Hi,

Can you provide a full, debug log, please?

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

 
Thx,
Ann

SonaQubeCodeCoverageLog.txt (84.0 KB)
Here is the redacted Sonar Run Code Analysis log

Hi,

To verify, this is primarily a .NET project? And are you building between the begin and end steps?

Because I see this in the log:

2024-11-19T14:45:45.4838292Z 09:45:45.477 DEBUG: Plugins not loaded because they are optional: [csharp, flex, go, web, javasymbolicexecution, java, javascript, kotlin, php, python, ruby, sonarscala, vbnet]
2024-11-19T14:45:45.5051285Z 09:45:45.493 DEBUG: Plugins loaded:
2024-11-19T14:45:45.5051899Z 09:45:45.493 DEBUG:   * Clean as You Code 2.4.0.2018 (cayc)
2024-11-19T14:45:45.5052396Z 09:45:45.493 DEBUG:   * XML Code Quality and Security 2.10.0.4108 (xml)
2024-11-19T14:45:45.5053688Z 09:45:45.493 DEBUG:   * JaCoCo 1.3.0.1538 (jacoco)
2024-11-19T14:45:45.5054099Z 09:45:45.493 DEBUG:   * IaC Code Quality and Security 1.36.0.12431 (iac)
2024-11-19T14:45:45.5055239Z 09:45:45.493 DEBUG:   * Text Code Quality and Security 2.16.0.4008 (text)
2024-11-19T14:45:45.5055479Z 09:45:45.493 DEBUG:   * TFVC 2.4.0 (scmtfvc)

This indicates that no C# files are analyzed. And no code → no coverage.

 
Ann

Hi again,

Before closing the log file, I scrolled just a little further and found this:

2024-11-19T14:45:49.2398177Z 09:45:49.232 DEBUG: Detected languages: [web, xml, json, vbnet]
2024-11-19T14:45:49.2400459Z 09:45:49.232 INFO: Load/download plugins
2024-11-19T14:45:49.2464660Z 09:45:49.232 INFO: Load/download plugins (done) | time=0ms
2024-11-19T14:45:49.2468223Z 09:45:49.232 DEBUG: Optional language-specific plugins not loaded: [csharp, flex, go, javasymbolicexecution, java, kotlin, php, python, ruby, sonarscala]
2024-11-19T14:45:49.2523165Z 09:45:49.248 DEBUG: Plugins loaded:
2024-11-19T14:45:49.2523883Z 09:45:49.248 DEBUG:   * VB.NET Code Quality and Security 9.32.0.97167 (vbnet)
2024-11-19T14:45:49.2524163Z 09:45:49.248 DEBUG:   * HTML Code Quality and Security 3.16.0.5274 (web)
2024-11-19T14:45:49.2524448Z 09:45:49.248 DEBUG:   * JavaScript/TypeScript/CSS Code Quality and Security 10.16.0.27621 (javascript)

So, okay. We do have code. But… It looks like the project is read as a test project:

> 2024-11-19T14:45:50.2864659Z 09:45:50.279 INFO: Sensor VB.NET Project Type Information [vbnet]
> 2024-11-19T14:45:50.2882334Z 09:45:50.279 DEBUG: Adding file type information (has MAIN 'false', has TEST 'true') for project 'MyApp.UnitTests' (project key 'DevMain-MyApp:DevMain-MyApp:C95CAC70-AE95-424B-A9E9-0227CAA51CC2', base dir 'D:\SomePath\318\s\Src\MyApp.UnitTests'). For debug info, see ProjectInfo.xml in 'D:\SomePath\318\.sonarqube\out\2'.
> 2024-11-19T14:45:50.2882846Z 09:45:50.279 INFO: Sensor VB.NET Project Type Information [vbnet] (done) | time=0ms
> 2024-11-19T14:45:50.2883005Z 09:45:50.279 INFO: Sensor VB.NET Analysis Log [vbnet]
> ```

And then we've got some generated code:
> ```
> 2024-11-19T14:45:50.2907454Z 09:45:50.279 DEBUG: Project 'DevMain-MyApp:DevMain-MyApp:C95CAC70-AE95-424B-A9E9-0227CAA51CC2': Analyzer working directory 'D:\SomePath\318\.sonarqube\out\2\output-vbnet' contains 6 .pb file(s)
> 2024-11-19T14:45:50.2928796Z 09:45:50.279 INFO: Roslyn version: 4.10.0.0
> 2024-11-19T14:45:50.2929248Z 09:45:50.279 INFO: Language version: VisualBasic16_9
> 2024-11-19T14:45:50.2929665Z 09:45:50.279 INFO: Concurrent execution: enabled
> 2024-11-19T14:45:50.2930775Z 09:45:50.279 DEBUG: File 'D:\SomePath\318\s\Src\MyApp.UnitTests\obj\Release\net8.0-windows\.NETCoreApp,Version=v8.0.AssemblyAttributes.vb' was recognized as generated
> 2024-11-19T14:45:50.2932152Z 09:45:50.279 DEBUG: File 'C:\Users\SomeUser\.nuget\packages\microsoft.net.test.sdk\17.8.0\build\netcoreapp3.1\Microsoft.NET.Test.Sdk.Program.vb' was recognized as generated
> ***Sensitive data REMOVED***
> 2024-11-19T14:45:50.2937062Z 09:45:50.279 INFO: Sensor VB.NET Analysis Log [vbnet] (done) | time=0ms
> ```

So I think we're back to having no "code" (only tests and generated files).

 
Ann