SonarKotlin code coverage

jacoco
kotlin
coverage

(Grégory Desmaziers) #1

Hi,

Sonar doesn’t display coverage information after analysis of my Kotlin project with the new SonarKotlin (official) plugin (I was working with another non-official Kotlin plugin).

Jacoco configuration :
property “sonar.jacoco.reportPaths”, “${project.buildDir}/jacoco/testDebugUnitTest.exec”

Version used:
sonar gradle plugin 2.6.2
SonarCloud

Is there another way to use the result of jacoco analysis ?


(Alexandre Gigleux) #4

Hello Grégory,

With the new SonarKotlin, it is not yet possible to import JaCoCo reports. We are working on it and this time the idea is to support the XML format instead of the binary one. See https://jira.sonarsource.com/browse/MMF-1362 for more details.

Regards


Jacoco coverage not shown neither with exec nor with xml
(Grégory Desmaziers) #5

Hi Alexandre,

I see that the ticket is currently in Test. Do you have a plan released date for the support of Jacoco reports in SonarSource ?

Thanks


(Jaka Putra Lesmana) #6

Hi Alexandre,

I already tried to send Kotlin Code Coverage (https://jira.sonarsource.com/browse/MMF-1362), but it didnt show up on Sonarqube, it shows 0% coverage. I already set the xml.enabled to true and make sure the xml file is there.

This is how it looks on my project:
property “sonar.coverage.jacoco.xmlReportPaths”, “build/reports/jacoco/jacocoTestReport/jacocoTestReport.xml”

There is no problem with Java coverage. I use Kotlin + Java in my project.

Did I do anything wrong with the script? Any help is appreciated.
I use sonarqube Version 7.1 (build 11001) on docker

Thank you.


(Robert Stoll) #7

jacoco reports are still not picked up with sonar.coverage.jacoco.xmlReportPaths as explained here:
https://docs.sonarqube.org/display/PLUG/Kotlin+Coverage+Results+Import

is this feature not yet available on SonarCloud?

Edit Tt tries to pick it up but seems to have problems due to line-number mismatches which is quite common in kotlin due to inline functions and such :frowning:


(Nico De Wet) #8

Excuse my ignorance but is this something that needs to be rolled out to sonarcloud.io?

I can’t tell what happens to MMF-1362 in terms of higher level processes.

On my side I’m using the 2.6.2 of the org.sonarqube Gradle plugin to try to send the XML report to sonarcloud.io and my gradle.properties has: systemProp.sonar.coverage.jacoco.xmlReportPaths=build/reports/jacoco/test/jacocoTestReport.xml


(Manuel Ortiz) #9

I am using that exactly same configuration, and my project is working showing coverage. But I am running on some issues, because it is not grabbing the coverage of all my classes. There some classes showing the right coverage, but some others are showing 0%. I think there are some issues in this plugin.

property “sonar.coverage.jacoco.xmlReportPaths”, “build/reports/jacoco/jacocoTestReport/jacocoTestReport.xml”

Also Please note that in the documentation says that you need to have the JaCoCo plugin on your server instance. After I install this plugin, I started getting the coverage. Before, with the sonar detekt plugin, I didn’t need to have the jacoco one installed.

My project is Android + gradle + kotlin.


(Jaka Putra Lesmana) #10

Thank you Manuel! That’s right, I need to install jacoco plugin on the server. My coverage is now showing. And as far as I checked, I have no issue yet regarding the 0% coverage for some files. However, I’ll keep checking and will pop out here if I find something.

Regards


(Alexandre Gigleux) #11

Hello,

I confirm SonarJaCoCo 1.0.0 is installed on SonarCloud.io (https://sonarcloud.io/api/plugins/installed) and I’m waiting for the 1.0.1 to be installed very soon to handle the case described in https://jira.sonarsource.com/browse/JACOCO-3

Alex


(Manuel Ortiz) #13

Hi Alexandre,

I am using the SonarJaCoCo version 1.0.1 and SonarKotlin 1.2 in my own sonar installation, and I am getting errors with some classes, where the coverage can not be retrieved. So for that specific class I am getting 0% coverage, but with others I can see the right one.

This is an example of one error I am getting:

Cannot import coverage information for file 'src/main/java/com/SomeClass.kt', coverage data is invalid. Error: {}
java.lang.IllegalStateException: Line 76 is out of range in the file src/main/java/com/SomeClass.kt (lines: 75)
    at org.sonar.api.internal.google.common.base.Preconditions.checkState(Preconditions.java:197)
    at org.sonar.api.batch.sensor.coverage.internal.DefaultCoverage.validateLine(DefaultCoverage.java:93)
    ...

It seems to be the same error described in https://jira.sonarsource.com/browse/JACOCO-3. How should I proceed? Do I need to create a ticket? I am new here, so it would be great you could help me.

Thanks!


(Alexandre Gigleux) #14

Hello Manuel,

This message “Cannot import coverage information for file” is exactly the fix we did this week to prevent the full code scan to stop because the JaCoCo XML file is containing invalid data.
This is now expected to have it on a couple of files and have 0% of coverage for these files in SonarQube.

Once https://github.com/jacoco/jacoco/issues/763 will be fixed on JaCoCo side, I do expect these messages to disappear.

Regards


(Manuel Ortiz) #15

Oh, I see. Then this is a jacoco issue. Nothing to do with Sonar.

Thank you very much for your clarification!


(Alexandre Gigleux) #16

FYI, SonarJaCoCo 1.0.1 and SonarKotlin 1.2.1 are now installed on SonarCloud.io


(Nico De Wet) #17

Does anyone have a working sample project with gradle + kotlin + sonarcloud.io?

I’m at the point where I’m more than willing to part with cash just for a working configuration.

I have an apparently broken configuration here: https://gitlab.com/nicodewet/cashbook


(Evgeny Mandrikov) #18

If after execution of gradle test jacocoTestReport you’ll open build/reports/jacoco/test/html/index.html, then you can notice that content of source files is not shown, whereas normally it is - see for example https://www.jacoco.org/jacoco/trunk/coverage/org.jacoco.core/org.jacoco.core/index.source.html

Somewhat similar happens with SonarQube: build/reports/jacoco/test/jacocoTestReport.xml contains

...
<package name="com/thorgil/cashbook/dataproviders/">
  ...
  <sourcefile name="BusinessTransactionDataProvider.kt">
...

so SonarQube JaCoCo Plugin tries to find file whose path ends by com/thorgil/cashbook/dataproviders/BusinessTransactionDataProvider.kt - relevant code is

however path to BusinessTransactionDataProvider.kt is src/main/kotlin/dataproviders/BusinessTransactionDataProvider.kt.

After moving this file to src/main/kotlin/com/thorgil/cashbook/dataproviders/BusinessTransactionDataProvider.kt and others to corresponding locations, you’ll see their sources in HTML report generated by JaCoCo and their coverage in SonarCloud.io :


(Ivan Pusic) #19

Currently I get low coverage percentage (30%) because half of my files are throwing errors when uploading to sonarcloud.

Gettling errors like “Cannot import coverage information for file XXX. coverage data is invalid. Error: {}”
“java.lang.IllegalStateException: Line 22 is out of range in the file XXX (lines: 21)”.

I am using gradle + kotlin + spring boot.

Did anybody solved this?