Sonarqube 8.9.2 installation - scan initiated with Sonarqube gradle plugin 3.3 and gradle 6.8.3 (which seems to use jacoco 0.8.6.
Jacoco reports are created correctly (we import them without issues inside Jenkins). But we incur into the following error when Sonarqube attempts to import one of the Jacoco reports (sorry, had to anonymize the actual path/name of the file):
Sensor JaCoCo XML Report Importer [jacoco]
Importing 1 report(s). Turn your logs in debug mode in order to see the exhaustive list.
Cannot import coverage information for file '<project>/src/test/scala/<path>/<filename>.scala', coverage data is invalid. Error: {}
java.lang.IllegalStateException: Line 67 is out of range in the file <project>/src/test/scala/<path>/<filename>.scala (lines: 66)
at org.sonar.api.utils.Preconditions.checkState(Preconditions.java:61)
at org.sonar.api.batch.sensor.coverage.internal.DefaultCoverage.validateLine(DefaultCoverage.java:93)
....
I believe I know the issue. As you can see it’s attempting to set the coverage for a test file; the main issue is probably that we have two files with the exact same name and in the same package, one inside the src/main directory and one inside the src/test directory. Jacoco correctly creates the report for the file inside src/main, but then the sonarqube importing associates the wrong file with the entry in the report.
Is there a “best way” to associate coverage imported data with files into only once of the sources folders, without having sonarqube skip checking the test files?
It seems odd to me that analysis would attempt to import coverage for a file in a test directory. Since it’s inside your src directory it looks like your test directory has been misidentified(?) as containing source, rather than test files.
Hi Ann, the rest seems to work fine. The gradle sourcesets in that project are just the default ones so I didn’t touch the default sonarqube properties in regards to any path. Could it be it’s a limitation of the Jacoco scanner itself? I noticed how there is no option to specify an actual folder to it (unless I missed some documentation page). Could it be that sonar or the gradle scanner are passing to the jacoco plugin the whole project folder for searching the files mentioned in the Jacoco report? Indeed I see that here sonar-scanner-gradle/SonarPropertyComputer.java at b7bb6f119504b43d778e7e9bf58c2834027389fb · SonarSource/sonar-scanner-gradle · GitHub you compute the different paths for the java sources, but for jacoco it seems the only computed paths are for the xml reports.
We’re both guessing at this point. Could you run a new analysis with -D sonar.scanner.dumpToFile=[path to file] on the command line & then share the (redacted as necessary) contents here? Specifically, I’m interested in seeing the calculated values of sonar.sources and sonar.tests, but the rest may be interesting too.