Code Coverage line number error

Template for a good new topic, formatted with Markdown:

  • ALM used (GitHub, Bitbucket Cloud, Azure DevOps)
    GitHub
  • CI system used (Bitbucket Cloud, Azure DevOps, Travis CI, Circle CI
    TeamCity
  • Scanner command used when applicable (private details masked)
    n/a
  • Languages of the repository
    C#, Typescript, TSQL
  • Only if the SonarCloud project is public, the URL
    n/a
    • And if you need help with pull request decoration, then the URL to the PR too
  • Error observed (wrap logs/code around with triple quotes ``` for proper formatting)
    I’m trying to import Code Coverage data & I’m getting an error that the line numbers don’t match the files.
    We use NCrunch to run the unit tests & it outputs a OpenCover xml file. The NCrunch team have confirmed the OpenCover file looks right (OpenCover report - Daily Usage Issues - NCrunch Forum), yet Sonar throws an error. The error isn’t limited to just this file, it can be whatever file Sonar decides to start with.

It feels like an off-by-one error.

INFO: Sensor C# Tests Coverage Report Import [csharp]
19:08:08   INFO: Parsing the OpenCover report E:\BuildAgent\work\727bac85dde6f437\NCrunchResults\OpenCover.xml
19:08:09   INFO: Adding this code coverage report to the cache for later reuse: E:\BuildAgent\work\727bac85dde6f437\NCrunchResults\OpenCover.xml
19:08:20   INFO: Time spent writing ucfgs 16349ms
19:08:25   INFO: ------------------------------------------------------------------------
19:08:25   INFO: EXECUTION FAILURE
19:08:25   INFO: ------------------------------------------------------------------------
19:08:25   INFO: Total time: 4:33.904s
19:08:25   INFO: Final Memory: 76M/296M
19:08:25   ERROR: Error during SonarScanner execution
19:08:25   INFO: ------------------------------------------------------------------------
19:08:25   java.lang.IllegalStateException: Line 23 is out of range in the file project/Validators/EmailTemplateModelValidator.cs (lines: 22)
19:08:25     at org.sonar.api.utils.Preconditions.checkState(Preconditions.java:61)
19:08:25     at org.sonar.api.batch.sensor.coverage.internal.DefaultCoverage.validateLine(DefaultCoverage.java:67)
19:08:25     at org.sonar.api.batch.sensor.coverage.internal.DefaultCoverage.lineHits(DefaultCoverage.java:55)
19:08:25     at org.sonar.plugins.dotnet.tests.CoverageReportImportSensor.analyzeCoverage(CoverageReportImportSensor.java:139)
19:08:25     at org.sonar.plugins.dotnet.tests.CoverageReportImportSensor.analyze(CoverageReportImportSensor.java:116)
19:08:25     at org.sonar.plugins.dotnet.tests.CoverageReportImportSensor.execute(CoverageReportImportSensor.java:84)
19:08:25     at org.sonar.scanner.sensor.AbstractSensorWrapper.analyse(AbstractSensorWrapper.java:62)
19:08:25     at org.sonar.scanner.sensor.ProjectSensorsExecutor.execute(ProjectSensorsExecutor.java:38)
19:08:25     at org.sonar.scanner.scan.ProjectScanContainer.doAfterStart(ProjectScanContainer.java:423)
19:08:25     at org.sonar.core.platform.ComponentContainer.startComponents(ComponentContainer.java:123)
19:08:25     at org.sonar.core.platform.ComponentContainer.execute(ComponentContainer.java:109)
19:08:25     at org.sonar.scanner.bootstrap.GlobalContainer.doAfterStart(GlobalContainer.java:130)
19:08:25     at org.sonar.core.platform.ComponentContainer.startComponents(ComponentContainer.java:123)
19:08:25     at org.sonar.core.platform.ComponentContainer.execute(ComponentContainer.java:109)
19:08:25     at org.sonar.batch.bootstrapper.Batch.doExecute(Batch.java:58)
19:08:25     at org.sonar.batch.bootstrapper.Batch.execute(Batch.java:52)
19:08:25     at org.sonarsource.scanner.api.internal.batch.BatchIsolatedLauncher.execute(BatchIsolatedLauncher.java:46)
19:08:25     at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
19:08:25     at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:78)
19:08:25     at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
19:08:25     at java.base/java.lang.reflect.Method.invoke(Method.java:567)
19:08:25     at org.sonarsource.scanner.api.internal.IsolatedLauncherProxy.invoke(IsolatedLauncherProxy.java:60)
19:08:25     at jdk.proxy1/jdk.proxy1.$Proxy0.execute(Unknown Source)
19:08:25     at org.sonarsource.scanner.api.EmbeddedScanner.doExecute(EmbeddedScanner.java:189)
19:08:25     at org.sonarsource.scanner.api.EmbeddedScanner.execute(EmbeddedScanner.java:138)
19:08:25     at org.sonarsource.scanner.cli.Main.execute(Main.java:112)
19:08:25     at org.sonarsource.scanner.cli.Main.execute(Main.java:75)
19:08:25     at org.sonarsource.scanner.cli.Main.main(Main.java:61)
19:08:25   ERROR:
19:08:25   The SonarScanner did not complete successfully
19:08:25   19:08:25.775  Post-processing failed. Exit code: 1
19:08:25   Process exited with code 0

  • Steps to reproduce
  • Potential workaround

Do not share screenshots of logs – share the text itself (bonus points for being well-formatted)!

Hi @simongh,

Thanks a lot for your feedback. Would it be possible to create a reproducer for us? If not, would it be possible to share the code file and the generated coverage?

Thanks,
Costin

let me see what I can do. I’m sure I can find a file without anything we can’t share in it

Hello @simongh

In addition, could you please share the debug logs with us?

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”)

Sorry it’s taken a while to update this thread.

I have a very simple project with a single method.
ClassLibrary1.zip (10.0 KB)

I haven’t set this project up for Sonar Analysis, but there error we get on a very similar project is:
This log file was run with verbose on. The file it mentions does have 25 lines

11:44:13 
  11:44:12.976 INFO: Parsing the OpenCover report E:\BuildAgent\work\598748740efd255a\NCrunchResults\OpenCover.xml
...
11:44:13 
  11:44:13.109 DEBUG: CoveredFile created: (ID '111', path 'E:\BuildAgent\work\598748740efd255a\singlesignon\src\SingleSignOn.Core\Validators\AddOrUpdateCommandValidator.cs', indexed as 'E:\BuildAgent\work\598748740efd255a\SingleSignOn\src\SingleSignOn.Core\Validators\AddOrUpdateCommandValidator.cs').
...
 11:44:24.295 ERROR: Error during SonarScanner execution
11:44:24   java.lang.IllegalStateException: Line 28 is out of range in the file src/SingleSignOn.Core/Validators/AddOrUpdateCommandValidator.cs (lines: 25)
11:44:24     at org.sonar.api.utils.Preconditions.checkState(Preconditions.java:61)
11:44:24     at org.sonar.api.batch.sensor.coverage.internal.DefaultCoverage.validateLine(DefaultCoverage.java:81)
11:44:24     at org.sonar.api.batch.sensor.coverage.internal.DefaultCoverage.lineHits(DefaultCoverage.java:69)
11:44:24     at org.sonar.plugins.dotnet.tests.CoverageReportImportSensor.analyzeCoverage(CoverageReportImportSensor.java:139)
11:44:24     at org.sonar.plugins.dotnet.tests.CoverageReportImportSensor.analyze(CoverageReportImportSensor.java:116)
11:44:24     at org.sonar.plugins.dotnet.tests.CoverageReportImportSensor.execute(CoverageReportImportSensor.java:84)
11:44:24     at org.sonar.scanner.sensor.AbstractSensorWrapper.analyse(AbstractSensorWrapper.java:64)
11:44:24     at org.sonar.scanner.sensor.ProjectSensorsExecutor.execute(ProjectSensorsExecutor.java:52)
11:44:24     at org.sonar.scanner.scan.SpringProjectScanContainer.doAfterStart(SpringProjectScanContainer.java:371)
11:44:24     at org.sonar.core.platform.SpringComponentContainer.startComponents(SpringComponentContainer.java:188)
11:44:24     at org.sonar.core.platform.SpringComponentContainer.execute(SpringComponentContainer.java:167)
11:44:24     at org.sonar.scanner.bootstrap.SpringGlobalContainer.doAfterStart(SpringGlobalContainer.java:137)
11:44:24     at org.sonar.core.platform.SpringComponentContainer.startComponents(SpringComponentContainer.java:188)
11:44:24     at org.sonar.core.platform.SpringComponentContainer.execute(SpringComponentContainer.java:167)
11:44:24     at org.sonar.batch.bootstrapper.Batch.doExecute(Batch.java:71)
11:44:24     at org.sonar.batch.bootstrapper.Batch.execute(Batch.java:65)
11:44:24     at org.sonarsource.scanner.api.internal.batch.BatchIsolatedLauncher.execute(BatchIsolatedLauncher.java:46)
11:44:24     at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
11:44:24     at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:78)
11:44:24     at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
11:44:24     at java.base/java.lang.reflect.Method.invoke(Method.java:567)
11:44:24     at org.sonarsource.scanner.api.internal.IsolatedLauncherProxy.invoke(IsolatedLauncherProxy.java:60)
11:44:24     at jdk.proxy1/jdk.proxy1.$Proxy0.execute(Unknown Source)
11:44:24     at org.sonarsource.scanner.api.EmbeddedScanner.doExecute(EmbeddedScanner.java:189)
11:44:24     at org.sonarsource.scanner.api.EmbeddedScanner.execute(EmbeddedScanner.java:138)
11:44:24     at org.sonarsource.scanner.cli.Main.execute(Main.java:126)
11:44:24     at org.sonarsource.scanner.cli.Main.execute(Main.java:81)
11:44:24     at org.sonarsource.scanner.cli.Main.main(Main.java:62)

If it helps, this is the fragment from the opencover file for the file mentioned:

        <Class>
          <Summary numSequencePoints="7" visitedSequencePoints="7" numBranchPoints="0" visitedBranchPoints="0" sequenceCoverage="100" branchCoverage="0" maxCyclomaticComplexity="0" minCyclomaticComplexity="0" maxCrapScore="0" minCrapScore="0" visitedClasses="1" numClasses="1" visitedMethods="1" numMethods="1" />
          <FullName>Gbg.SingleSignOn.Validators.AddOrUpdateCommandValidator</FullName>
          <Methods>
            <Method visited="true" cyclomaticComplexity="0" nPathComplexity="0" sequenceCoverage="0" branchCoverage="0" crapScore="0" isConstructor="true" isStatic="false" isGetter="false" isSetter="false">
              <Summary numSequencePoints="7" visitedSequencePoints="7" numBranchPoints="0" visitedBranchPoints="0" sequenceCoverage="100" branchCoverage="0" maxCyclomaticComplexity="0" minCyclomaticComplexity="0" maxCrapScore="0" minCrapScore="0" visitedClasses="0" numClasses="0" visitedMethods="1" numMethods="1" />
              <MetadataToken>100663331</MetadataToken>
              <Name>System.Void Gbg.SingleSignOn.Validators.AddOrUpdateCommandValidator::.ctor()</Name>
              <FileRef uid="56" />
              <SequencePoints>
                <SequencePoint vc="1" uspid="4389" ordinal="0" offset="0" sl="7" sc="3" el="7" ec="39" bec="0" bev="0" fileid="183" />
                <SequencePoint vc="1" uspid="4390" ordinal="1" offset="7" sl="8" sc="3" el="8" ec="4" bec="0" bev="0" fileid="183" />
                <SequencePoint vc="1" uspid="4391" ordinal="2" offset="8" sl="9" sc="4" el="11" ec="25" bec="0" bev="0" fileid="183" />
                <SequencePoint vc="1" uspid="4392" ordinal="3" offset="87" sl="13" sc="4" el="15" ec="25" bec="0" bev="0" fileid="183" />
                <SequencePoint vc="1" uspid="4393" ordinal="4" offset="163" sl="17" sc="4" el="19" ec="25" bec="0" bev="0" fileid="183" />
                <SequencePoint vc="1" uspid="4394" ordinal="5" offset="239" sl="21" sc="4" el="22" ec="21" bec="0" bev="0" fileid="183" />
                <SequencePoint vc="1" uspid="4395" ordinal="6" offset="314" sl="23" sc="3" el="23" ec="4" bec="0" bev="0" fileid="183" />
              </SequencePoints>
              <BranchPoints />
              <MethodPoint xsi:type="SequencePoint" vc="1" uspid="4389" ordinal="0" offset="0" sl="7" sc="3" el="7" ec="39" bec="0" bev="0" fileid="183" />
            </Method>
          </Methods>
        </Class>

and attached is the file
AddOrUpdateCommandValidator.zip (434 Bytes)

Hey @simongh ,

I tried running the end step locally, using the OpenCover.xml inside your zip.
It did not throw an exception.

Could you supply me with the following, so that I could reproduce it:

  • The verbose logs for this simple project, ClassLibrary1
  • What instructions you are using in the commandline

For reference, this is what I used:

dotnet sonarscanner begin /k:"whatever" /d:sonar.host.url="http://localhost:XXXX"  /d:sonar.login="<TOKEN>" /d:sonar.cs.opencover.reportsPaths="./OpenCover.xml" /d:"sonar.verbose=true"
dotnet build --no-incremental
// No coverage tool here, since I used your file
dotnet sonarscanner end /d:sonar.login="<TOKEN>"

Let me see what i can come up with.

It’s kinda tricky to configure, since I don’t have to the server, so I need to setup & configure a new pipeline & it get it approved.

Could Unicode byte markers cause the line count to be out? I think all our files are UTF8 with BOM.

I’ve managed to get this setup.

The sample project I supplied did work as well. However none of the other 4 projects configured in the same way seem to work. So there’s something about them that causes the Sonar Scanner to break.

I’ve got to a stage where I have managed to generate 2 opencover files, 1 works & 1 fails. These were from the same code, which was unchanged but different builds.

The opencover console xml file fails when sonar tries to read it. the console addin xml works. I’ve compared the 2 files & they are largely the same, but the id’s differ.

Is there anything you can tell as to why 1 fails & not the other?
ReportingService_NCrunchResults.zip (16.7 KB)

What is the specific failure?

Same error as above,

java.lang.IllegalStateException

It might partially be down to Ncrunch using generating a test run using release configuration, but the .net build step using Debug configuration.

There still seems to be something up though. The build server does a git clean -dxf to remove all untracked files. If that’s all it does, the sonar scan fails. If I do a full checkout it works, so it looks like there’s some temp files that don’t get removed that cause the sonar scan to fail. I’m still investigating & trying to reproduce the error.