Sonarcloud code coverage: multilanguage support

How to capture code coverage for multiple languages within a single sonar scan?

I am able to get it to work for one language or the other, but when i combine them sonar starts reporting no code coverage on the newly added code.

heres an example of what i used when i tried into make this change. this example correctly reported code cov for the python code changes in my PR but after merging it stopped reporting java code coverage correctly.

    stage('SonarScan') {
            when {
                expression { gitEventSource == 'pull_request' }
                expression { env.CHANGE_TARGET == 'main' }
            }
            environment {
                ORGANIZATION = "drivewealth"
            }
            steps {
                script {
                    withCredentials([gitUsernamePassword(credentialsId: 'xxx')]) {
                        withSonarQubeEnv('xxx') {
                            sh "git fetch origin"
                            sh "git fetch origin main:master"

                            def changeUrl = env.GIT_URL.split("/")
                            def org = changeUrl[3]
                            def repo = changeUrl[4].substring(0, changeUrl[4].length() - 4)
                            //language=bash
                            sh """mvn -B sonar:sonar \
                                -Dsonar.projectKey=xxx \
                                -Dsonar.pullrequest.provider=GitHub \
                                -Dsonar.java.binaries=target \
                                -Dsonar.organization=$ORGANIZATION \
                                -Dsonar.pullrequest.github.repository=${org}/${repo} \
                                -Dsonar.pullrequest.key=${env.CHANGE_ID} \
                                -Dsonar.pullrequest.branch=${env.CHANGE_BRANCH} \
                                -Dsonar.sources=. \
                                -Dsonar.inclusions=src/drivewealth**,src/main/java \
                                -Dsonar.coverage.jacoco.xmlReportPaths=${REPO_PATH}/common.drivewealth.com/drivewealth_jacoco_report_aggregate/target/site/jacoco-aggregate/jacoco.xml \
                                -Dsonar.python.coverage.reportPaths=${REPO_PATH}/backoffice.drivewealth.com/drivewealth_backoffice_instrument_recon/target/coverage.xml \
                                -Dsonar.pullrequest.base=${env.CHANGE_TARGET}
                            """

                        }
                    }
                }
            }

=== extra stuff below ===

i can include logs of what sonar sees when attempting the scan if that helps. I’m hoping this use case is fairly common and i’ve made a basic mistake with my combination of sources,inclusions,jacoco.xml.reportPath and python.coverage.reportPaths

some extra details on the module src structure of my python and java modules and why the params are the way are above:

python module src can be found at its root with some_module/src/some_module/*.py

drivewealth_backoffice_instrument_recon git:main ❯ pwd                                                                                                                                                   ✭
/Users/cflynn/IdeaProjects/code.drivewealth.com/backoffice.drivewealth.com/drivewealth_backoffice_instrument_recon
drivewealth_backoffice_instrument_recon git:main ❯ tree -L 1 src                                                                                                                                         ✭
src
├── drivewealth_backoffice_instrument_recon
├── drivewealth_backoffice_instrument_recon.egg-info
├── dto
├── main
└── service

6 directories, 0 files

java modules are standard at some_module/src/main/java

This should work out of the box.

What do the logs say when you pass multiple coverage reports? Do both sensors indicate success? Do the sensor logs look different as compared to the runs where you only provide one?

Hey!

When I run the sonar scan with the above parameters, the python modules code gets correctly reported. The java module(s) do not.

When I look into the sensor logs for the java module, this is what I see:

[INFO] 16:37:23.532 Indexing files of module '${project.name}'
[INFO] 16:37:23.533   Base dir: /var/lib/****/workspace/Agave/code.drivewealth.com/PR-792/2/code.drivewealth.com/posttrade.drivewealth.com/drivewealth_posttrade_order_handler
[INFO] 16:37:23.533   Source paths: .
[INFO] 16:37:23.533   Test paths: src/test/java
[INFO] 16:37:23.533   Included sources: src/drivewealth**, src/main/java
[INFO] 16:37:23.533   Excluded sources: **/build-wrapper-dump.json
...
[INFO] 16:37:34.328 ------------- Run sensors on module ${project.name}
[INFO] 16:37:34.411 Sensor JavaSensor [java]
[INFO] 16:37:34.411 Configured Java source version (sonar.java.source): 17, preview features enabled (sonar.java.enablePreview): false
[INFO] 16:37:34.413 The Java analyzer is running in a context where unchanged files can be skipped. Full analysis is performed for changed files, optimized analysis for unchanged files.
[INFO] 16:37:34.414 Server-side caching is enabled. The Java analyzer was able to leverage cached data from previous analyses for 1 out of 2 files. These files will not be parsed.
[INFO] 16:37:34.414 No "Main" source files to scan.
[INFO] 16:37:34.414 Restoring SonarArchitecture IndexIR data generated from previous module.
[INFO] 16:37:34.443 Writing SonarArchitecture IR data to directory "/var/lib/****/workspace/Agave/code.drivewealth.com/PR-792/2/code.drivewealth.com/target/sonar/architecture-ir/java"
[INFO] 16:37:34.465 Using ECJ batch to parse 1 Test java source files with batch size 210 KB.
[INFO] 16:37:34.691 Starting batch processing.
[INFO] 16:37:35.208 100% analyzed
[INFO] 16:37:35.208 Batch processing: Done.
[INFO] 16:37:35.209 Did not optimize analysis for any files, performed a full analysis for all 1 files.
[INFO] 16:37:35.209 No "Generated" source files to scan.
[INFO] 16:37:35.209 Sensor JavaSensor [java] (done) | time=798ms
[INFO] 16:37:35.209 Sensor JaCoCo XML Report Importer [jacoco]
[INFO] 16:37:35.209 Importing 1 report(s). Turn your logs in debug mode in order to see the exhaustive list.
[INFO] 16:37:35.250 Sensor JaCoCo XML Report Importer [jacoco] (done) | time=41ms
[INFO] 16:37:35.250 Sensor Java Config Sensor [iac]
[INFO] 16:37:35.261 0 source files to be analyzed
[INFO] 16:37:35.261 0/0 source files have been analyzed
[INFO] 16:37:35.261 Sensor Java Config Sensor [iac] (done) | time=11ms
[INFO] 16:37:35.261 Sensor ThymeLeaf template sensor [securityjavafrontend]
[INFO] 16:37:35.261 Sensor ThymeLeaf template sensor [securityjavafrontend] (done) | time=0ms
[INFO] 16:37:35.261 Sensor SurefireSensor [java]
[INFO] 16:37:35.261 parsing [/var/lib/****/workspace/Agave/code.drivewealth.com/PR-792/2/code.drivewealth.com/posttrade.drivewealth.com/drivewealth_posttrade_order_handler/target/surefire-reports]
[INFO] 16:37:35.266 Sensor SurefireSensor [java] (done) | time=5ms
[INFO] 16:37:35.267 Sensor IaC Docker Sensor [iac]
[INFO] 16:37:35.267 Sensor IaC Docker Sensor is restricted to changed files only
[INFO] 16:37:35.270 0 source files to be analyzed
[INFO] 16:37:35.270 0/0 source files have been analyzed
[INFO] 16:37:35.271 Sensor IaC Docker Sensor [iac] (done) | time=4ms
[INFO] 16:37:35.271 Sensor Serverless configuration file sensor [security]
[INFO] 16:37:35.271 0 Serverless function entries were found in the project
[INFO] 16:37:35.271 0 Serverless function handlers were kept as entrypoints
[INFO] 16:37:35.271 Sensor Serverless configuration file sensor [security] (done) | time=0ms
[INFO] 16:37:35.271 Sensor AWS SAM template file sensor [security]
[INFO] 16:37:35.271 Sensor AWS SAM template file sensor [security] (done) | time=0ms
[INFO] 16:37:35.271 Sensor AWS SAM Inline template file sensor [security]
[INFO] 16:37:35.271 Sensor AWS SAM Inline template file sensor [security] (done) | time=0ms

now, compare that to the logs of my prior setup, where java srcs are captured correctly:

[INFO] 16:56:43.943 Indexing files of module '${project.name}'
[INFO] 16:56:43.943   Base dir: /var/lib/****/workspace/Agave/code.drivewealth.com/PR-792/3/code.drivewealth.com/posttrade.drivewealth.com/drivewealth_posttrade_order_handler
[INFO] 16:56:43.943   Source paths: pom.xml, src/main/java
[INFO] 16:56:43.943   Test paths: src/test/java
[INFO] 16:56:43.943   Excluded sources: **/build-wrapper-dump.json
...
[INFO] 16:57:07.686 ------------- Run sensors on module ${project.name}
[INFO] 16:57:07.733 Sensor JavaSensor [java]
[INFO] 16:57:07.733 Configured Java source version (sonar.java.source): 17, preview features enabled (sonar.java.enablePreview): false
[INFO] 16:57:07.735 The Java analyzer is running in a context where unchanged files can be skipped. Full analysis is performed for changed files, optimized analysis for unchanged files.
[INFO] 16:57:07.736 Restoring SonarArchitecture IndexIR data generated from previous module.
[INFO] 16:57:07.764 Server-side caching is enabled. The Java analyzer was able to leverage cached data from previous analyses for 1 out of 4 files. These files will not be parsed.
[INFO] 16:57:07.765 Using ECJ batch to parse 2 Main java source files with batch size 210 KB.
[INFO] 16:57:07.765 Starting batch processing.
[INFO] 16:57:08.019 100% analyzed
[INFO] 16:57:08.019 Batch processing: Done.
[INFO] 16:57:08.020 Optimized analysis for 1 of 2 files.
[INFO] 16:57:08.020 Writing SonarArchitecture IR data to directory "/var/lib/****/workspace/Agave/code.drivewealth.com/PR-792/3/code.drivewealth.com/target/sonar/architecture-ir/java"
[INFO] 16:57:08.043 Using ECJ batch to parse 1 Test java source files with batch size 210 KB.
[INFO] 16:57:08.044 Starting batch processing.
[INFO] 16:57:08.080 100% analyzed
[INFO] 16:57:08.080 Batch processing: Done.
[INFO] 16:57:08.081 Did not optimize analysis for any files, performed a full analysis for all 1 files.
[INFO] 16:57:08.081 No "Generated" source files to scan.
[INFO] 16:57:08.081 Sensor JavaSensor [java] (done) | time=348ms
[INFO] 16:57:08.081 Sensor JaCoCo XML Report Importer [jacoco]
[INFO] 16:57:08.081 Importing 1 report(s). Turn your logs in debug mode in order to see the exhaustive list.
[INFO] 16:57:08.122 Sensor JaCoCo XML Report Importer [jacoco] (done) | time=41ms
[INFO] 16:57:08.122 Sensor Java Config Sensor [iac]
[INFO] 16:57:08.125 0 source files to be analyzed
[INFO] 16:57:08.125 0/0 source files have been analyzed
[INFO] 16:57:08.125 Sensor Java Config Sensor [iac] (done) | time=3ms
[INFO] 16:57:08.125 Sensor ThymeLeaf template sensor [securityjavafrontend]
[INFO] 16:57:08.125 Sensor ThymeLeaf template sensor [securityjavafrontend] (done) | time=0ms
[INFO] 16:57:08.125 Sensor SurefireSensor [java]
[INFO] 16:57:08.125 parsing [/var/lib/****/workspace/Agave/code.drivewealth.com/PR-792/3/code.drivewealth.com/posttrade.drivewealth.com/drivewealth_posttrade_order_handler/target/surefire-reports]
[INFO] 16:57:08.130 Sensor SurefireSensor [java] (done) | time=5ms
[INFO] 16:57:08.130 Sensor XML Sensor [xml]
[INFO] 16:57:08.130 Sensor XML Sensor is restricted to changed files only
[INFO] 16:57:08.130 Sensor XML Sensor [xml] (done) | time=0ms
[INFO] 16:57:08.130 Sensor IaC Docker Sensor [iac]
[INFO] 16:57:08.130 Sensor IaC Docker Sensor is restricted to changed files only
[INFO] 16:57:08.135 0 source files to be analyzed
[INFO] 16:57:08.135 0/0 source files have been analyzed
[INFO] 16:57:08.135 Sensor IaC Docker Sensor [iac] (done) | time=5ms
[INFO] 16:57:08.135 Sensor Serverless configuration file sensor [security]
[INFO] 16:57:08.135 0 Serverless function entries were found in the project
[INFO] 16:57:08.135 0 Serverless function handlers were kept as entrypoints
[INFO] 16:57:08.135 Sensor Serverless configuration file sensor [security] (done) | time=0ms
[INFO] 16:57:08.135 Sensor AWS SAM template file sensor [security]
[INFO] 16:57:08.135 Sensor AWS SAM template file sensor [security] (done) | time=0ms
[INFO] 16:57:08.135 Sensor AWS SAM Inline template file sensor [security]
[INFO] 16:57:08.135 Sensor AWS SAM Inline template file sensor [security] (done) | time=0ms

and the working cmd for java src:

sh """mvn -B sonar:sonar \
    -Dsonar.projectKey=xxx \
    -Dsonar.pullrequest.provider=GitHub \
    -Dsonar.java.binaries=target \
    -Dsonar.organization=$ORGANIZATION \
    -Dsonar.pullrequest.github.repository=${org}/${repo} \
    -Dsonar.pullrequest.key=${env.CHANGE_ID} \
    -Dsonar.pullrequest.branch=${env.CHANGE_BRANCH} \
    -Dsonar.coverage.jacoco.xmlReportPaths=${REPO_PATH}/common.drivewealth.com/drivewealth_jacoco_report_aggregate/target/site/jacoco-aggregate/jacoco.xml \
    -Dsonar.pullrequest.base=${env.CHANGE_TARGET}
 ""

I found a fix. ( using wildcard for src/main/java/** )

 -Dsonar.sources=. \
-Dsonar.inclusions=src/drivewealth**,src/main/java/** \

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