Hi, I’m analyzing the Apex code for salesforce project using Azure Devops. I get results in SonarCloud But, the coverage code is always 0.
This is the configuration used :
Hi, I removed the project in SonarCloud, I created another one and I analyzed again the salesforce project using Azure Devops. the result is different. The coverage code is 19.8%
When I checked the analyzed files, I found only 28 files analyzed.
When I checked in the logs , I found 150 files to be analyzed.
this is the logs
INFO: Sensor Apex Sensor [sonarapex]
INFO: 150 source files to be analyzed
INFO: 150/150 source files have been analyzed
INFO: Sensor Apex Sensor [sonarapex] (done) | time=5319ms
INFO: Sensor Test coverage Sensor for Apex [sonarapex]
##[error]ERROR: Invalid coverage information on file: 'OcicatPortalRecapController'
java.lang.IllegalStateException: Line 2629 is out of range in the file force-app/main/default/classes/OcicatPortalRecapController.cls (lines: 2625)
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)
at com.A.A.D.E.A(Unknown Source)
at com.A.A.D.E.A(Unknown Source)
at com.A.A.D.E.execute(Unknown Source)
..........
WARN: File 'AssignAccountOwner' is present in coverage report but cannot be found in filesystem
WARN: File 'UserHierarchyUpdateSendEmail' is present in coverage report but cannot be found in filesystem
WARN: File 'UpdateAccountDetails' is present in coverage report but cannot be found in filesystem
WARN: File 'PopulateIncidentAccount' is present in coverage report but cannot be found in filesystem
WARN: File 'AddToModifyAllIncidentsGroup' is present in coverage report but cannot be found in filesystem
INFO: Sensor Test coverage Sensor for Apex [sonarapex] (done) | time=92ms
INFO: Sensor CSS Rules [javascript]
INFO: No CSS, PHP, HTML or VueJS files are found in the project. CSS analysis is skipped.
INFO: Sensor CSS Rules [javascript] (done) | time=2ms
This error message means that there is coverage being reported on line 2629 of a file, but as far as the scanner is aware, the file only has 2625 files.
Is the coverage report being freshly generated before the analysis, or is it possible an older coverage report is being used?
It looks like you’re performing a Pull Request Analysis, which means only changed files will be available on SonarCloud, although more files were analyzed at analysis time (and the results filtered later).
The coverage report is freshly generated, just before the analysis (using azure pipeline).
How can I modify the type “Pull Request Analysis” to get all analysis result ?
And taking a look at OcicatPortalRecapController.cls, how many lines does it actually have on the branch you are scanning?
You’ll need to make sure you’re building the main branch or a long-lived branch (which is determined by a regex, as noted in the docs) and building outside the context of a pull request in Azure DevOps.
Here is what I suggest: Once you have a full analysis running, if you still receive this specific error on coverage take a screenshot of where the file ends after browsing the Code tab, with the line numbers visible.
Now, all files have been analyzed. I got the same error for one file.
at org.sonar.scanner.bootstrap.GlobalContainer.doAfterStart(GlobalContainer.java:128)
at org.sonar.core.platform.ComponentContainer.startComponents(ComponentContainer.java:123)
at org.sonar.core.platform.ComponentContainer.execute(ComponentContainer.java:109)
at org.sonar.batch.bootstrapper.Batch.doExecute(Batch.java:58)
at org.sonar.batch.bootstrapper.Batch.execute(Batch.java:52)
at org.sonarsource.scanner.api.internal.batch.BatchIsolatedLauncher.execute(BatchIsolatedLauncher.java:46)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at org.sonarsource.scanner.api.internal.IsolatedLauncherProxy.invoke(IsolatedLauncherProxy.java:60)
at com.sun.proxy.$Proxy0.execute(Unknown Source)
at org.sonarsource.scanner.api.EmbeddedScanner.doExecute(EmbeddedScanner.java:189)
at org.sonarsource.scanner.api.EmbeddedScanner.execute(EmbeddedScanner.java:138)
at org.sonarsource.scanner.cli.Main.execute(Main.java:112)
at org.sonarsource.scanner.cli.Main.execute(Main.java:75)
at org.sonarsource.scanner.cli.Main.main(Main.java:61)
Also, when I executed Unit tests using sfdx cmd (sfdx force:apex:test:run …) the coverage code was 75%. But the value with sonarCloud is 11%
Have you any idea why values are not the same?
It looks like the logs you shared in your last post weren’t complete, can you make sure to include the full log message?
Are there non-Apex files (for example, Javascript) files being analyzed in your project?
The best way to debug issues like coverage numbers not matching is to narrow down to a specific example (a file, a line) where the coverage being reported by your coverage tool doesn’t match what is in your coverage report.