SonarCloud metrics(coverage, code smells, vulnerabilities,..) on Pull Requests covers no lines

Tech stack :

  • Bitbucket Cloud
  • GCP - Cloud Build
  • sonar.branch.name=$BRANCH_NAME and sonar.branch.target=master
  • Java - Sprint Boot

the SonarCloud coverage reports generated on our pull requests have 0 lines covered when we follow the report towards its details page. I’ve noticed that none of the short-lived branches had any lines that were covered. Even if we introduce vulnerabilities or code smells on purpose, neither branch or PR analysis are picking this up.

Hey there!

Just to be clear about what you mean — do you mean that on the analyses of short-lived branches and pull requests, no files appear in the Code tab on SonarCloud?

Hi,

Thank you for the response!
Yes, that is correct and that’s probably also the reason why vulnerabilities, code smells, code coverage and so on are not displayed and picked up on branch and PR analysis.

Hey there.

I think it’s time to look in the scanner logs (preferably at DEBUG level, by adding -X to the scanner command or setting the analysis parameter sonar.verbose=true to see if anything is off. Here are the kind of logs you should be looking for on a branch/PR analysis.

# Check if there are any errors collecting changed files 
INFO: SCM collecting changed files in the branch
....
INFO: SCM collecting changed files in the branch (done) | time=324ms
DEBUG: SCM reported 1 file changed in the branch

# Make sure all files you expect to be analyzed are being analyzed and aren't ignored
INFO: 15 files indexed
INFO: 0 files ignored because of inclusion/exclusion patterns
INFO: 0 files ignored because of scm ignore settings

# Make sure the git repository is being detected
INFO: SCM Publisher SCM provider for this project is: git
INFO: SCM Publisher 1 source file to be analyzed

# Check if there are any errors collecting changed lines 
DEBUG: SCM reported changed lines for 1 file in the branch
INFO: SCM writing changed lines (done) | time=59ms

Checking for any WARN messages in the logs would also be a good step.

These are the logs corresponding to you’re proposal. The only one i could not find was the one about the SCM Publisher

Step #1 - "build_application_and_run_tests": [INFO] SCM collecting changed files in the branch
Step #1 - "build_application_and_run_tests": [INFO] SCM collecting changed files in the branch (done) | time=193ms
Step #1 - "build_application_and_run_tests": [INFO] Indexing files...
Step #1 - "build_application_and_run_tests": [INFO] Project configuration:
Step #1 - "build_application_and_run_tests": [INFO]   Excluded sources: **/build-wrapper-dump.json
Step #1 - "build_application_and_run_tests": [INFO] 3 files indexed
Step #1 - "build_application_and_run_tests": [INFO] 0 files ignored because of inclusion/exclusion patterns
Step #1 - "build_application_and_run_tests": [INFO] 0 files ignored because of scm ignore settings

Step #1 - "build_application_and_run_tests": [INFO] SCM writing changed lines
Step #1 - "build_application_and_run_tests": [INFO] SCM writing changed lines (done) | time=3ms

No WARN messages found in the logs

Hey there.

It doesn’t look like you went up to DEBUG level with your logs – I would go back and do so and look for the lines like this:

DEBUG: SCM reported 1 file changed in the branch
DEBUG: SCM reported changed lines for 1 file in the branch

I would also check the 3 files that are indexed, and making sure that all the files you expect to see there are there.

My bad! Now i did the run with the debug switch enabled. It looks like it won’t pick up any of the files.

Step #1 - "build_application_and_run_tests": [DEBUG] 10:47:13.409 SCM reported changed lines for 0 files in the branch
Step #1 - "build_application_and_run_tests": [INFO] 10:47:13.409 SCM writing changed lines (done) | time=44ms

Step #1 - "build_application_and_run_tests": [INFO] 10:47:08.240 3 files indexed
Step #1 - "build_application_and_run_tests": [DEBUG] 10:47:08.240 file=/workspace/.git/... , lastModified changed from 2022-01-17 10:47:08.233012901 to 2022-01-17 10:47:08.237013256
Step #1 - "build_application_and_run_tests": [INFO] 10:47:08.241 0 files ignored because of inclusion/exclusion patterns
Step #1 - "build_application_and_run_tests": [DEBUG] 10:47:08.241 file=/workspace/.git/. create new FileSnapshot: lastRead=2022-01-17 10:47:08.241449596, lastModified=2022-01-17 10:47:08.237013256, size=1

Hey @LarsLefebvre

Sorry for the late reply.

It shows 3 files have been indexed… total. Do you expect your project to only have 3 files?

Hi @Colin

I’ve been digging for the last couple of days and it’s starting to look a lot like the problem here. If i trigger an analysis locally it reports the short lived branch. Whenever i trigger it from cloudbuild (with the same parameters) it does nothing.

@LarsLefebvre I sent you a DM