Error during SonarScanner execution: Line 34 is out of range in the file

I am using the SonarCloudAnalyze@1 task in a build pipeline in Azure DevOps, building a C# application. Everytime the pipeline runs, I get this error:

ERROR: Error during SonarScanner execution
##[error]java.lang.IllegalStateException: Line 34 is out of range in the file src/Core/Services/Core/ServiceCollectionExtensions.cs (lines: 18)
	at org.sonar.api.utils.Preconditions.checkState(Preconditions.java:61)
	at org.sonar.api.batch.sensor.coverage.internal.DefaultCoverage.validateLine(DefaultCoverage.java:67)
	at org.sonar.api.batch.sensor.coverage.internal.DefaultCoverage.lineHits(DefaultCoverage.java:55)
java.lang.IllegalStateException: Line 34 is out of range in the file src/Core/Services/Core/ServiceCollectionExtensions.cs (lines: 18)
	at org.sonar.api.utils.Preconditions.checkState(Preconditions.java:61)
	at org.sonar.api.batch.sensor.coverage.internal.DefaultCoverage.validateLine(DefaultCoverage.java:67)
	at org.sonar.api.batch.sensor.coverage.internal.DefaultCoverage.lineHits(DefaultCoverage.java:55)
##[error]at org.sonar.plugins.dotnet.tests.CoverageReportImportSensor.analyzeCoverage(CoverageReportImportSensor.java:139)
	at org.sonar.plugins.dotnet.tests.CoverageReportImportSensor.analyze(CoverageReportImportSensor.java:116)
	at org.sonar.plugins.dotnet.tests.CoverageReportImportSensor.execute(CoverageReportImportSensor.java:84)
	at org.sonar.scanner.sensor.AbstractSensorWrapper.analyse(AbstractSensorWrapper.java:62)
	at org.sonar.plugins.dotnet.tests.CoverageReportImportSensor.analyzeCoverage(CoverageReportImportSensor.java:139)
	at org.sonar.plugins.dotnet.tests.CoverageReportImportSensor.analyze(CoverageReportImportSensor.java:116)
	at org.sonar.plugins.dotnet.tests.CoverageReportImportSensor.execute(CoverageReportImportSensor.java:84)
...

It always fails with the same file (which is indeed 18 lines long). I’ve tried checking the encoding of the file, but that’s normal (UTF8-BOM). There are other classes/files with the same name in the application in different namespaces/folders: I wondered whether that could be confusing the scanner?

Hey there.

SonarQube relies on the coverage report being generated to reference lines that exist in the files it has analyzed.

If you look into the coverage report for these files src/Core/Services/Core/ServiceCollectionExtensions.cs do you see references to lines that don’t exist?

If so, it has to be addressed at that level.

We recently ran into the same issue with a C# project. The Visual Studio solution contained multple C# projects, which built Nuget packages.

The problem turned out to be one of the unit test projects included a PackageReference to one of the built Nuget packages. This meant that the code coverage generator was using the line numbers from the package, not the local project.

The fix was to replace the PackageReference with a local project reference instead.