Why does the code coverage import via .trx file (links to a .coverage file) not work?

We are using SonarQube 8.9.1 (and SonarScanner 5.3.1) and run it on our own server. We are trying to get the code coverage for our .Net project imported.
There are many sources on the internet stating that SonarQube/SonarScanner is able to handle .trx files and get the coverage information.

But we just can’t get it working so that SonarQube shows coverage information.

Our repro 1 (we specify the path of the .trx file):

dotnet sonarscanner begin /k:"Jam-Analyzers" /d:sonar.host.url="http://server:9000"  /d:sonar.login="------------------------" /d:sonar.cs.vstest.reportsPaths="D:\Projects\Jam.Analyzers\Jam.Analyzers.Test\TestResults\*.trx"

dotnet build

dotnet test --logger:"trx" --collect "Code Coverage"

dotnet sonarscanner end /d:sonar.login="------------------------"

Result with repro 1: At Overview/Overall Code, the number of unit tests is 40 (correct), but the code coverage is 0.0% (wrong)
The output (it found the trx file and parsed it, but it seems to ignore the .coverage file)

INFO: Parsing the Visual Studio Test Results file ‘D:\Projects\Jam.Analyzers\Jam.Analyzers.Test\TestResults\user_NB-JAM158_2021-09-17_09_47_02.trx’.
INFO: Sensor C# Unit Test Results Import [csharp] (done) | time=56ms
INFO: Sensor Zero Coverage Sensor
INFO: Sensor Zero Coverage Sensor (done) | time=11ms
INFO: CPD Executor 1 file had no CPD blocks
INFO: CPD Executor Calculating CPD for 2 files
INFO: CPD Executor CPD calculation finished (done) | time=13ms
INFO: Analysis report generated in 51ms, dir size=199 KB
INFO: Analysis report compressed in 163ms, zip size=38 KB
INFO: Analysis report uploaded in 137ms
INFO: ANALYSIS SUCCESSFUL, you can browse http://server:9000/dashboard?id=Jam-Analyzers
INFO: Note that you will be able to access the updated dashboard once the server has processed the submitted analysis report
INFO: More about the report processing at http://server:9000/api/ce/task?id=AXvy8G47_uZxQKEMQhLd
INFO: Analysis total time: 4.554 s
INFO: ------------------------------------------------------------------------
INFO: EXECUTION SUCCESS


Our repro 2 (we don’t specify the path of the .trx file):

dotnet sonarscanner begin /k:"Jam-Analyzers" /d:sonar.host.url="http://server:9000"  /d:sonar.login="------------------------"

dotnet build

dotnet test --logger:"trx" --collect "Code Coverage"

dotnet sonarscanner end /d:sonar.login="------------------------"

Result with repro 2: At Overview/Overall Code, the number of unit tests is 0 (incorrect) and the code coverage is 0.0% (wrong)

The last lines of the output (it does not mention the .trx file or the .coverage file)

INFO: Importing results from 5 proto files in ‘D:\Projects\Jam.Analyzers.sonarqube\out\4\output-cs’
INFO: Importing results from 5 proto files in ‘D:\Projects\Jam.Analyzers.sonarqube\out\1\output-cs’
INFO: Importing 4 Roslyn reports
INFO: Found 4 MSBuild C# projects: 2 MAIN projects. 1 TEST project. 1 with no MAIN nor TEST files.
INFO: Sensor C# [csharp] (done) | time=330ms
INFO: Sensor Zero Coverage Sensor
INFO: Sensor Zero Coverage Sensor (done) | time=10ms
INFO: CPD Executor 1 file had no CPD blocks
INFO: CPD Executor Calculating CPD for 2 files
INFO: CPD Executor CPD calculation finished (done) | time=14ms
INFO: Analysis report generated in 51ms, dir size=199 KB
INFO: Analysis report compressed in 77ms, zip size=38 KB
INFO: Analysis report uploaded in 128ms
INFO: ANALYSIS SUCCESSFUL, you can browse http://server:9000/dashboard?id=Jam-Analyzers
INFO: Note that you will be able to access the updated dashboard once the server has processed the submitted analysis report
INFO: More about the report processing at http://server:9000/api/ce/task?id=AXvy9IFl_uZxQKEMQhLe
INFO: Analysis total time: 4.383 s
INFO: ------------------------------------------------------------------------
INFO: EXECUTION SUCCESS

The .trx file contains this element that references the .coverage file. So we’d expect SonarScanner to find and handle it and get the code coverage on SonarQube:

<ResultSummary outcome="Completed">
  <Counters total="40" executed="40" passed="40" failed="0" error="0" timeout="0" aborted="0" inconclusive="0" passedButRunAborted="0" notRunnable="0" notExecuted="0" disconnected="0" warning="0" completed="0" inProgress="0" pending="0" />
  <CollectorDataEntries>
    <Collector agentName="NB-JAM158" uri="datacollector://microsoft/CodeCoverage/2.0" collectorDisplayName="Code Coverage">
      <UriAttachments>
        <UriAttachment>
          <A href="NB-JAM158\user_NB-JAM158_2021-09-17.08_45_58.coverage"></A>
        </UriAttachment>
      </UriAttachments>
    </Collector>
  </CollectorDataEntries>
</ResultSummary>

Are we missing something? Are we doing it wrong? There is just no log-output telling us what’s wrong.

Hello @jam

Could you please take a look at the coverage troubleshooting guide? Seems to me that you did not set the path to the coveragexml.

Hi,

Our situation: The coverage analysis works for us. But we heard that there is a simpler way.

Currently, we convert the coverage report ourselves from *.coverage to *.xml using CodeCoverage.exe.

We wanted to simplify this and heard that it’s already enough to specify the .trx file and the SonarScanner is then able to handle it (converting it automatically to xml).

Hi @jam,

you can find here the documentation for the auto-discovery for the trx test and coverage files.

Unfortunately, at this moment this works only with the .NET 4.6 flavor of the scanner.

Hmm just stumbled upon the ‘NET 4.6 only’ comment… I totally misread the documentation (mybad…)

Is this something that is on your (shortterm) roadmap to implement for .NET5/.NET6 ? Or should I invest the time to convert these files myself ?

Freddy