SonarQube - Fixing Sonar code coverage Zero percentage issue

Most common question has been asked on multiple channels multiple times. There is a hack to fix this quickly but you need to ensure that you run a sonar scanner from the project root where the sonar.properties files exist.

 

Take an example you are building a nodejs based application and your tests runes inside the docker containers or Kubernetes pods. You need to validate the following things -

  • The coverage folder exists on your root folder for sonar scanner to scan the code

  • Codebase also exists in the same directory structure as it was when you run the tests

  • The coverage file lcov.info exists at the right location (it is auto-generated so you don’t need to be a worry)

  • Now, If you are running the sonar from the host machine and your test runs from the container, there might be the path issue for the source file. E.g in your lcov.info file the file path shows something like /usr/app/resources/jsc/LogToLoggly.js and for sonar scanner, your host machine path should be there. So to do that you can simply have the absolution path removed

  • sed -i -- 's/\/usr\/app\///g' coverage/lcov.info

  • Now sonar scanner should be able to understand the folder structure well and generate the coverage percentage.

code-coverage-sonar

 

If you have any other issues related to sonar, please post logs in the comments. I am happy to help.