Does SonarCloud support Kotlin Multiplatform?

SonarCloud doesn’t detect my Kotlin common code in a multiplatform project.

Is it something SonarCloud does not support yet, or am I doing something wrong?

    subprojects {
        sonarqube {
            properties {
                property("sonar.kotlin.detekt.reportPaths", "build/reports/detekt/detekt.xml")
                property("sonar.sources", files("src/commonMain/kotlin", "src/jvmMain/kotlin")) // I tried few alternatives, like use listOf and sourceSets.flatMap { it.allSources.srcDirs }
                property("sonar.test", files("src/commonTest/kotlin", "src/jvmTest/kotlin"))
            }
        }
    }
4 Likes

What do you mean by “a multiplatform project”?

What is this piece of configuration you added?
Can you show logs of analysis?

What do you mean by “a multiplatform project”?

What is this piece of configuration you added?

I’m not sure to understand the question.

The code snippet from my original post is nothing more than the sonarQube configuration for the probject.

Or you are talking about something else?

Can you show logs of analysis?

Where can I find them?

What is this piece of configuration you added?

I mean this code snippet you’ve put in the first message, to which file does it belong?

Can you show logs of analysis?

These are logs printed when running sonar-scanner

As I said, it is the sonar configuration.

If the fact that it is configured from gradle disturbs you, here is a simpler equivalent:

sonar.kotlin.detekt.reportPaths=build/reports/detekt/detekt.xml
sonar.sources=src/commonMain/kotlin,src/jvmMain/kotlin
sonar.test=src/commonTest/kotlin,src/jvmTest/kotlin

These are logs printed when running sonar-scanner

I don’t recall seeing any special logs.

But since I understood that sonarQube do not really support Kotlin, I stopped using it.

And right now I’m not willing to spend time to setup sonar again, just to reproduce this problem.

If you, however, manage to set up sonar for a kotlin multiplatform project, with code coverage properly reported, please share it.

I have the same issue: I have android project (gradle), with many modules. I have few regular java modules, few android library modules, and one Kotlin multiplatform module. In Java and Android lib modules I have mixed Java and Kotlin classes.

I managed to setup SonarQube and Jacoco for Java and Android modules (I have one shared configuration in top level build.gradle file). But I can’t manage how to upload to sonarqube my Kotlin multiplatform module.

I tried to modify sonarqube configuration in build.gradle in that Kotlin Multiplatform module, but without any success :confused:

sonarqube {
    properties {
        property("sonar.sources", ["./src/commonMain/kotlin", "./src/iosMain/kotlin", "./src/jvmMain/kotlin"])
        property("sonar.tests",  ["./src/commonTest/kotlin", "./src/iosTest/kotlin", "./src/jvmTest/kotlin"])
    }
}

I’m using SonarQube 7.9.2 LTS

I tried many ways of defining sonar properties for that Kotlin Multiplatform module, without any success :confused:
When I ran ./gradlew sonarqube --info I can see following logs:

  1. For a regular java module with Android and Kotlin:
------------- Run sensors on module MY_JAVA_MODULE
Sensor JavaSquidSensor [java]
Configured Java source version (sonar.java.source): 8
JavaClasspath initialization
JavaClasspath initialization (done) | time=2ms
JavaTestClasspath initialization
JavaTestClasspath initialization (done) | time=61ms
Java Main Files AST scan
1003 source files to be analyzed

404/1003 files analyzed, current file: SOME_CLASS.java
689/1003 files analyzed, current file: SOME_CLASS.java
794/1003 files analyzed, current file: SOME_CLASS.java
1003/1003 source files have been analyzed

> Task :sonarqube
Java Main Files AST scan (done) | time=38737ms
Java Test Files AST scan
250 source files to be analyzed

250/250 source files have been analyzed

> Task :sonarqube
Java Test Files AST scan (done) | time=6597ms
Sensor JavaSquidSensor [java] (done) | time=45427ms
Sensor JaCoCo XML Report Importer [jacoco]
Sensor JaCoCo XML Report Importer [jacoco] (done) | time=413ms
Sensor Kotlin Sensor [kotlin]
26 source files to be analyzed
Sensor Kotlin Sensor [kotlin] (done) | time=272ms

26/26 source files have been analyzed
13/13 source files have been analyzed
2/2 source files have been analyzed

As you can see, sonarqube ran many different sensors on that module, including Java and Kotlin sensors.

  1. Logs for Kotlin Multiplatform module:
------------- Run sensors on module MY_KOTLIN_MULTIPLATFORM_MODULE
Sensor JaCoCo XML Report Importer [jacoco]
Report doesn't exist: 'SOME_XML_FILE'
Sensor JaCoCo XML Report Importer [jacoco] (done) | time=1ms
Sensor JavaXmlSensor [java]
Sensor JavaXmlSensor [java] (done) | time=0ms

As you can see, sonarqube didn’t launch Kotlin sensor on this module, only xml and jacoco.

How can I enforce Sonarqube to run analysis on this module? It just enters the module, and ignore all sources (I tried all combinations of sonar.sources, sonar.java.source, sonar.java.binaries).

Maybe problem is that Sonarqube scanner don’t look at files inside sources dir but only on applied plugins, and it can’t find java-library or com.android.library plugins, so it skip the analysis?
Because my module have only these plugins:

apply plugin: 'kotlin-multiplatform'
apply plugin: 'kotlinx-serialization'

You might be correct sonar-scanner-gradle/SonarQubePlugin.java at 1e71faf88cecc90acf638b968b59926f1ee3cb4c · SonarSource/sonar-scanner-gradle · GitHub

Whats the current state of this issue? Is this part of the roadmap due increased importance of KMM projects.

I’ve tried several configurations:
My module structure:
/module/src/commonMain
/module/src/commonTest
/module/src/androidMain
/module/src/androidTest

Plugin configuration:
property(“sonar.sources”, buildutils.SonarQubeUtils.sourcePaths)
property(“sonar.tests”, buildutils.SonarQubeUtils.testPaths)
property(“sonar.junit.reportPaths”, “$buildDir/test-results/testDebugUnitTest”)

object SonarQubeUtils {

  val sourcePaths = listOf(
    "src/commonMain/kotlin",
    "src/androidMain/kotlin",
  ).joinToString(separator = ",")

  val testPaths = listOf(
    "src/commonTest/kotlin",
    "src/androidTest/kotlin",
  ).joinToString(separator = ",")
}

then running gradle sonarqube:
Resource not found: package.TrackingTest under the directory /XX while reading test reports. Please, make sure your “sonar.junit.reportPaths” property is configured properly

Whats the problem?
junit.reportPaths targets /test-results/testDebugUnitTest/TEST-package.TrackingTest.xml but the error message reports test class not found. Above I defined sonar.test with commonTest where the plugin should find the class (src/commonTest/kotlin/package/TrackingTest.kt)

SonarQube plugin version: 3.2.0

Hello @nilsjr,

Thank you for the message. Unfortunately, currently we’re not supporting Kotlin multiplatform properly. This issue was not in priority for some time, but currently we’re considering to implement the fix. I can’t tell you about the timing, but there is a ticket for it, so you can monitor the progress of this issue.

https://jira.sonarsource.com/browse/SONARKT-23

Regards,
Margarita

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.