c++jenkinssonarqubecode-coveragesonarqube-scan

Why is SonarQube ignoring coverage data by gcovr from C++ files?


I have a project that is structured like this:

- 3rdParty_Lib1
- 3rdParty_Lib2
- Project_Root
--- apps/
--- include/
------ project_headers/
------ boost_headers/
--- src/
------- module1/
------- module2/
------- module3/
------- 3p_stack/
---------- file1.hpp
---------- file1.cpp
------- file2.cpp
------- file3.cpp
------- file4.cpp
------- file5.cpp
--- tests/
------- module1/
------- module2/
------- module3/
------- file2test.cpp
------- file3test.cpp
------- file4test.cpp
------- file5test.cpp

Now I have generated gcovr coverage report, and the paths in the coverage report look like this:

src\file2.cpp
src\file3.cpp
src\file4.cpp

This is my sonar properties:

sonar.projectName=Project
sonar.projectVersion=0.1
sonar.sources=Project_Root/src/
sonar.exclusions=**/src/3p_stack/**
sonar.cfamily.build-wrapper-output=Project_Root/SonarQube/sonarqube_output
sonar.coverageReportPaths=Project_Root/artifacts/coverage.xml
sonar.sourceEncoding=UTF-8

This is what my log says:

00:33:00.881 INFO: Imported coverage data for 0 files
00:33:00.883 INFO: Coverage data ignored for 82 unknown files, including:
00:33:00.883 include/project_headers/file2.hpp
00:33:00.883 include/project_headers/file3.hpp
00:33:00.883 include/project_headers/file4.hpp

What alteration can I do to ensure that my coverage report is picked up by sonar? Kindly help!


Solution

  • So, I could not find a way for sonarqube to take up the relative paths.

    In the end, I wrote a simple python script to edit the paths to include the absolute path instead of them being relative.

    import xml.etree.ElementTree as ET
    import os
    def update_paths_in_xml(xml_file, prepend_path):
        # Parse the XML file
        tree = ET.parse(xml_file)
        root = tree.getroot()
        
        # Iterate over all 'file' elements and update the 'path' attribute
        for file_elem in root.findall(".//file"):
            old_path = file_elem.get('path')
            if old_path:
                new_path = prepend_path + old_path
                file_elem.set('path', new_path)
                print(f"Updated path: {old_path} -> {new_path}")
        
        # Write the updated XML back to the file
        tree.write(xml_file, encoding='UTF-8', xml_declaration=True)
    
    # Example usage
    xml_file = os.path.abspath('./coverage.xml')
    prepend_path = 'D:/slave/workspace/project/src/'
    
    update_paths_in_xml(xml_file, prepend_path)
    print("Paths updation completed.")
    
    

    Once the full paths started appearing in the coverage.xml file, sonarqube immediately started picking up everything.