SonarQube analysis with MSBuild: Line is out of range in file

风格不统一 提交于 2021-02-10 04:54:18

问题


So, we have a TeamCity project building a C# webapi project using .NET 4.5 which includes a SonarQube analysis.

At the end of the analysis, we receive the following exception:

[13:41:13][Step 14/16] ERROR: Error during SonarQube Scanner execution
[13:41:13][Step 14/16] java.lang.IllegalStateException: Line 65 is out of range in the file App_Start/UnityConfig.cs (lines: 64)

What did I already check:

  • UPDATE: Further analysis proves that the error is caused due to dotCover code coverage analysis
  • File Encoding: UTF-8 (verified with Notepad++, is UTF-8 without BOM), using CRLF line feeds (checked with hexdump and files end with 0x0D 0x0A)
  • Encoding used by SonarQube

    [16:17:37][Step 14/16] INFO: Default locale: "en_US", source code encoding: "windows-1252" (analysis is platform dependent)
    [16:17:37][Step 14/16] INFO: Process project properties
    [16:17:37][Step 14/16] bla bla
    [16:17:59][Step 14/16] INFO: Source encoding: UTF-8, default locale: en_US
    

Using the following stack:

  • SonarQube 6.4 (just updated)
  • SonarC# 5.11 (build 1761)
  • dotCover included in TeamCity 9.1.6
  • Microsoft (R) Build Engine version 14.0.24720.0
  • Java(TM) SE Runtime Environment (build 1.8.0_111-b14)

We do the following steps (related to SonarQube) in our build process:

  • start Analysis

    E:\SonarQube\sonar-scanner-msbuild-2.3.2\SonarQube.Scanner.MSBuild.exe begin /k:"CoBen" /n:"CoBen" /v:"%build.number%" /d:sonar.cs.nunit.reportsPaths="TestResult-*.xml" /d:sonar.cs.dotcover.reportsPaths="DotCover-*.html"
    
  • Build project

    MSBuild.exe /p:Configuration=Release;Targets=Rebuild
    
  • Run unit tests with dotCover

    C:\TeamCity\buildAgent\tools\dotCover\dotCover.exe analyse /TargetExecutable="%system.teamcity.build.workingDir%\packages\NUnit.ConsoleRunner.3.6.1\tools\nunit3-console.exe" /TargetArguments="--teamcity --agents=1 --result=%system.teamcity.build.workingDir%\TestResult-unit.xml;format=nunit2 --where \"cat != WebApiTests\" %system.teamcity.build.workingDir%\TestDlls.nunit" /Output="DotCover-unit.html" /ReportType="HTML"
    
  • Run Integration tests with dotCover

    C:\TeamCity\buildAgent\tools\dotCover\dotCover.exe analyse /TargetExecutable="%system.teamcity.build.workingDir%\packages\NUnit.ConsoleRunner.3.6.1\tools\nunit3-console.exe" /TargetArguments="--teamcity --agents=1 --result=%system.teamcity.build.workingDir%\TestResult-webapi.xml;format=nunit2 --where \"cat == WebApiTests\" %system.teamcity.build.workingDir%\TestDlls.nunit" /Output="DotCover-webapi.html" /ReportType="HTML"
    
  • End sonar analysis

    E:\SonarQube\sonar-scanner-msbuild-2.3.2\SonarQube.Scanner.MSBuild.exe end
    

The exact error:

[23:25:19][Step 14/16] INFO: Parsing the dotCover report E:\TeamCity\buildAgent\work\5728a1531387749f\.\DotCover-webapi\src\996.html
[23:25:19][Step 14/16] INFO: Parsing the dotCover report E:\TeamCity\buildAgent\work\5728a1531387749f\.\DotCover-webapi\src\997.html
[23:25:19][Step 14/16] INFO: Parsing the dotCover report E:\TeamCity\buildAgent\work\5728a1531387749f\.\DotCover-webapi\src\998.html
[23:25:19][Step 14/16] INFO: Parsing the dotCover report E:\TeamCity\buildAgent\work\5728a1531387749f\.\DotCover-webapi\src\999.html
[23:25:19][Step 14/16] INFO: Adding this code coverage report to the cache for later reuse: E:\TeamCity\buildAgent\work\5728a1531387749f\.\DotCover-webapi.html
[23:25:19][Step 14/16] INFO: ------------------------------------------------------------------------
[23:25:19][Step 14/16] INFO: EXECUTION FAILURE
[23:25:19][Step 14/16] INFO: ------------------------------------------------------------------------
[23:25:19][Step 14/16] INFO: Total time: 12:17.339s
[23:25:20][Step 14/16] ERROR: Error during SonarQube Scanner execution
[23:25:20][Step 14/16] INFO: Final Memory: 52M/392M
[23:25:20][Step 14/16] INFO: ------------------------------------------------------------------------
[23:25:20][Step 14/16] java.lang.IllegalStateException: Line 65 is out of range in the file App_Start/UnityConfig.cs (lines: 64)
[23:25:20][Step 14/16]  at org.sonar.api.internal.google.common.base.Preconditions.checkState(Preconditions.java:197)
[23:25:20][Step 14/16]  at org.sonar.api.batch.sensor.coverage.internal.DefaultCoverage.validateLine(DefaultCoverage.java:90)
[23:25:20][Step 14/16]  at org.sonar.api.batch.sensor.coverage.internal.DefaultCoverage.lineHits(DefaultCoverage.java:78)
[23:25:20][Step 14/16]  at org.sonar.plugins.dotnet.tests.CoverageReportImportSensor.analyze(CoverageReportImportSensor.java:94)
[23:25:20][Step 14/16]  at org.sonar.plugins.dotnet.tests.CoverageReportImportSensor.execute(CoverageReportImportSensor.java:69)
[23:25:20][Step 14/16]  at org.sonar.scanner.sensor.SensorWrapper.analyse(SensorWrapper.java:53)
[23:25:20][Step 14/16]  at org.sonar.scanner.phases.SensorsExecutor.executeSensor(SensorsExecutor.java:57)
[23:25:20][Step 14/16]  at org.sonar.scanner.phases.SensorsExecutor.execute(SensorsExecutor.java:49)
[23:25:20][Step 14/16]  at org.sonar.scanner.phases.AbstractPhaseExecutor.execute(AbstractPhaseExecutor.java:78)
[23:25:20][Step 14/16]  at org.sonar.scanner.scan.ModuleScanContainer.doAfterStart(ModuleScanContainer.java:175)
[23:25:20][Step 14/16]  at org.sonar.core.platform.ComponentContainer.startComponents(ComponentContainer.java:143)
[23:25:20][Step 14/16]  at org.sonar.core.platform.ComponentContainer.execute(ComponentContainer.java:128)
[23:25:20][Step 14/16]  at org.sonar.scanner.scan.ProjectScanContainer.scan(ProjectScanContainer.java:262)
[23:25:20][Step 14/16]  at org.sonar.scanner.scan.ProjectScanContainer.scanRecursively(ProjectScanContainer.java:257)
[23:25:20][Step 14/16]  at org.sonar.scanner.scan.ProjectScanContainer.scanRecursively(ProjectScanContainer.java:255)
[23:25:20][Step 14/16]  at org.sonar.scanner.scan.ProjectScanContainer.doAfterStart(ProjectScanContainer.java:247)
[23:25:20][Step 14/16]  at org.sonar.core.platform.ComponentContainer.startComponents(ComponentContainer.java:143)
[23:25:20][Step 14/16]  at org.sonar.core.platform.ComponentContainer.execute(ComponentContainer.java:128)
[23:25:20][Step 14/16]  at org.sonar.scanner.task.ScanTask.execute(ScanTask.java:47)
[23:25:20][Step 14/16]  at org.sonar.scanner.task.TaskContainer.doAfterStart(TaskContainer.java:86)
[23:25:20][Step 14/16]  at org.sonar.core.platform.ComponentContainer.startComponents(ComponentContainer.java:143)
[23:25:20][Step 14/16]  at org.sonar.core.platform.ComponentContainer.execute(ComponentContainer.java:128)
[23:25:20][Step 14/16]  at org.sonar.scanner.bootstrap.GlobalContainer.executeTask(GlobalContainer.java:118)
[23:25:20][Step 14/16]  at org.sonar.batch.bootstrapper.Batch.executeTask(Batch.java:117)
[23:25:20][Step 14/16]  at org.sonarsource.scanner.api.internal.batch.BatchIsolatedLauncher.execute(BatchIsolatedLauncher.java:63)
[23:25:20][Step 14/16]  at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
[23:25:20][Step 14/16]  at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
[23:25:20][Step 14/16]  at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
[23:25:20][Step 14/16]  at java.lang.reflect.Method.invoke(Method.java:498)
[23:25:20][Step 14/16]  at org.sonarsource.scanner.api.internal.IsolatedLauncherProxy.invoke(IsolatedLauncherProxy.java:60)
[23:25:20][Step 14/16]  at com.sun.proxy.$Proxy0.execute(Unknown Source)
[23:25:20][Step 14/16]  at org.sonarsource.scanner.api.EmbeddedScanner.doExecute(EmbeddedScanner.java:233)
[23:25:20][Step 14/16]  at org.sonarsource.scanner.api.EmbeddedScanner.runAnalysis(EmbeddedScanner.java:151)
[23:25:20][Step 14/16]  at org.sonarsource.scanner.cli.Main.runAnalysis(Main.java:123)
[23:25:20][Step 14/16]  at org.sonarsource.scanner.cli.Main.execute(Main.java:77)
[23:25:20][Step 14/16]  at org.sonarsource.scanner.cli.Main.main(Main.java:61)
[23:25:20][Step 14/16] ERROR: 
[23:25:20][Step 14/16] ERROR: Re-run SonarQube Scanner using the -X switch to enable full debug logging.
[23:25:21][Step 14/16] The SonarQube Scanner did not complete successfully

Any idea's what else we could do?


回答1:


Solved! I switched from dotCover to OpenCover and now all is running well.




回答2:


This error normally happens when you had changed a file in your program. In your case, previously your file (App_Start/UnityConfig.cs) had the line 65 and now it doesn't have that line anymore as the stdout said to you: (lines: 64).

As explained in this tutorial, running dotnet sonarscanner command (with dotnet-sonarscanner tool already installed), after dotnet build and dotnet test --settings your-settings.xmlcommands, a folder called TestResults will be created and under it will be created another folder with a random guid name with all your coverage files (depending on your coverage xml settings), something like this:

TestResults
    51481fee-256a-483f-b473-d4e741228025
        coverage
        coverage.xml
        coverage.info
        coverage.json
        coverage.opencover.xml

If your TestResults folder has more than one guid folder, like that:

TestResults
    51481fee-256a-483f-b473-d4e741228025
    8341f0a9-ae41-4c28-a651-5a94931add2f
    b6c0b109-72c4-415e-934f-230e6438c03f
    953c1a4f-b98b-4fb0-bd55-7616e9464257
    0597c293-c904-4a1f-ba13-5b494edeefcf

let only the last folder created deleting all of the other ones. The only folder that is important to sonar is the last one, the other ones are garbage. See the documentation for more information.



来源:https://stackoverflow.com/questions/44331973/sonarqube-analysis-with-msbuild-line-is-out-of-range-in-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!