Sonar Analysis of swift project failing

We are running an Azure DevOps YAML pipeline to gather code coverage on an iOS swift project. The SonarQubeAnalyze task consistently fails with the error

The only way to get an accurate analysis of C/C++/Objective-C files
 is by using the SonarSourcebuild-wrapper and setting the property
 "sonar.cfamily.build-wrapper-output" or by using Clang Compilation Database
 and setting the property "sonar.cfamily.compile-commands". None of these two
 options were specified.

Our configuration is

  • macOS 14.7.2 Azure hosted agent
  • Xcode 15.4
  • SonarQube Server: Server version: 9.9.6.92038
  • SonarQubePrepare@7 task
  • SonarQubeAnalyze@7 task
  • Xcode@5task

We have followed this document Coverage & Test Data Generate Reports for Swift
We have tried using the SonarQube generic coverage format xccov-to-sonarqube-generic.sh
and also xcpretty to generate a compilation database file compile_commands.json
Both methods fail the same way.

The buildwrapper method only works on macOS before version 13 and macOS 13.7.2 is the lowest available on Azure hosted agents.

Attaching the files

  • compile_commands.json
  • sonarqube-generic-coverage.xml had to upload with txt extension
  • soanrqubeanalysetasklog.txt

Hi,

The analysis is failing because it detects C / C++ or Objective-C code that it is unable to analyze.

You say you’re trying to analyze a Swift project, but you’ve provided a compile_commands.json file. Can you clarify the languages in your project that you want to analyze versus the full list of languages in your project?

To easily exclude CFamily languages, you can add the following properties to your analysis parameters:

sonar.c.file.suffixes=-
sonar.cpp.file.suffixes=-
sonar.objc.file.suffixes=-

 
HTH,
Ann

Hi Ann,

I will run as you suggest. Do these three properties go under extraProperties in the SonarQubePrepare task?

I will run the scripts/xccov-to-sonarqube-generic.sh to generate the sonarqube-generic-coverage.xml file and reference that file in the sonar.coverageReportPaths property

Hi,

Yes, add them as extraProperties.

 
HTH,
Ann

Hi Ann,

It worked. Two things had to be changed

  1. Add the extra properties you mentioned
  2. Have them properly formatted in the task
  3. run the xccov-to-sonarqube-generic.sh script

Now that it is running leads me to ask a second question.
In the project there are three different Test schemes.
How do we use the output from three different runs of the xcode task?
Is there a way to combine the three files into one for the analysis?

Thanks,
Kirk

Another thought.
Could we do the following.

Run SonarQubePrepare
Run xcode test test target 1
Run xccov-to-sonarqube-generic.sh
Run SonarQubeAnalyze
Run SonarQubePublish

Run xcode test test target 2
Run xccov-to-sonarqube-generic.sh
Run SonarQubeAnalyze
Run SonarQubePublish

Run xcode test test target 3
Run xccov-to-sonarqube-generic.sh
Run SonarQubeAnalyze
Run SonarQubePublish

Hi,

Analysis is not additive. If you do what you’ve outlined, what you’ll end up with is the results of test target 3. Unless, that is, you use 3 different, unique sonar.projectKey values for your three different runs. Then you’ll have 3 different projects in SonarQube, each with the results of one test target.

 
HTH,
Ann

Hi Ann,

That is what I was afraid the result would be.

Do you know if there is any way to combine the generated sonarqube-generic-coverage.xml files into one for analysis?

If not I will have to get the developers to create a test scheme for overall coverage.

Thanks,
Kirk

Hi Kirk,

I’m not aware of any scripts for this but it doesn’t seem like it should be hard…?

 
Ann

Hi Ann,

It looks like it works if you list all files in the sonar.coverageReportPaths property.

sonar.coverageReportPaths=$(Build.SourcesDirectory)/sonarfiles/sonarqube-hubtests-coverage.xml,$(Build.SourcesDirectory)/sonarfiles/sonarqube-pickingtests-coverage.xml,$(Build.SourcesDirectory)/sonarfiles/sonarqube-systemtests-coverage.xml

1 Like