SonarCloud Showing 0% Code Coverage for Swift [Xcode 13]

I have been trying this for a couple of days to get the code coverage on the sonar cloud, but the sonar cloud is not able to detect the test cases and shows 0% code coverage.

Specification: Xcode version: 13.2.1, Mac OS X 12.2.1

So, far I have tried as follow

  1. Followed this link sonar-scanning-examples/swift-coverage at master · SonarSource/sonar-scanning-examples · GitHub.
    1. Build the Project
    2. Creat code coverage report.
      but in 3rd step importing the code coverage report I am getting the following error:
INFO: Scanner configuration file: /usr/local/Cellar/sonar-scanner/4.6.2.2472_1/libexec/conf/sonar-scanner.properties
INFO: Project root configuration file: NONE
INFO: SonarScanner 4.6.2.2472
INFO: Java 11.0.12 Homebrew (64-bit)
INFO: Mac OS X 12.2.1 x86_64
INFO: User cache: /Users/admin/.sonar/cache
ERROR: SonarQube server [http://localhost:9000] can not be reached
INFO: ------------------------------------------------------------------------
INFO: EXECUTION FAILURE
INFO: ------------------------------------------------------------------------
INFO: Total time: 0.769s
INFO: Final Memory: 3M/17M
INFO: ------------------------------------------------------------------------
ERROR: Error during SonarScanner execution

Apart from this, I have tried giving code coverage path in Adminstrater> Language> Code Coverage Report.

but nothing is working for me, anyone can please look into this issue.

Thank You.

Hey there.

It looks like the error you’re receiving doesn’t have anything to do with code coverage – but that your scan is running without being pointed to -Dsonar.host.url=https://sonarcloud.io and probably without the required sonar.organization and sonar.projectKey configuration.

Presumably, you have an analysis somewhere that is already successfully analyzing (and where 0% code coverage is being reported). When you’re running your regular project (not the example), where are you running it? In a particular CI (GitHub actions, Bitbucket Pipelines, etc.)

1 Like

Hi Colin,

We are using the Bitbucket pipeline and I have added the above keys in my .yml pipeline file.

find command below.

  • export CODE_COVERAGE=./sonarqube-generic-coverage.xml
    - sonar-scanner -Dsonar.host.url=https://sonarcloud.io -Dsonar.projectKey=projKey -Dsonar.organization=OrgKey -Dsonar.sources=. -Dsonar.cfamily.build-wrapper-output.bypass=true -Dsonar.coverageReportPaths=$CODE_COVERAGE

but still, sonar cloud is showing 0.0% Code Coverage.

Can you post your full bitbucket-pipelines.yml file?

please find the same below:

image: gcc:9.1.0

clone:
  depth: full              # SonarCloud scanner needs the full history to assign issues properly

definitions:
  caches:
    sonar: ~/.sonar/cache  # Caching SonarCloud artifacts will speed up your build
  steps:
    - step: &build-test-sonarcloud
        name: Build, test and analyze on SonarCloud
        caches:
          - sonar
        services:
          - docker
        script:
          - curl -v https://api.bitbucket.org/2.0/repositories/$BITBUCKET_WORKSPACE/$BITBUCKET_REPO_SLUG/pullrequests/$BITBUCKET_PR_ID/comments -d access_token=$REPOSITORY_OAUTH_ACCESS_TOKEN
          - export SONAR_SCANNER_VERSION=4.4.0.2170
          - export SONAR_SCANNER_HOME=$HOME/.sonar/sonar-scanner-$SONAR_SCANNER_VERSION-linux
          - curl --create-dirs -sSLo $HOME/.sonar/sonar-scanner.zip https://binaries.sonarsource.com/Distribution/sonar-scanner-cli/sonar-scanner-cli-$SONAR_SCANNER_VERSION-linux.zip
          - unzip -o $HOME/.sonar/sonar-scanner.zip -d $HOME/.sonar/
          - export PATH=$SONAR_SCANNER_HOME/bin:$PATH
          - export COMPILE_DATABASE=./compile_commands.json
          - export CODE_COVERAGE=./sonarqube-generic-coverage.xml
          - sonar-scanner -Dsonar.host.url=https://sonarcloud.io -Dsonar.projectKey=proj-key -Dsonar.organization=organization-key -Dsonar.sources=. -Dsonar.cfamily.build-wrapper-output.bypass=true -Dsonar.coverageReportPaths=$CODE_COVERAGE

pipelines:                 # More info here: https://confluence.atlassian.com/bitbucket/configure-bitbucket-pipelines-yml-792298910.html
  branches:
    qa:
      - step: *build-test-sonarcloud
  pull-requests:
    '**':
      - step: *build-test-sonarcloud

any update??? @Colin

Hey there.

Please do not bump threads – and certainly not when your last update was mid-day on a Friday. While we are happy to provide free support to our users, please be respectful.

The bitbucket-pipelines.yml file doesn’t actually show how you produce the code coverage report (which to be clear should be generated each time, and not stored in your repository).

The code coverage should also be produced on the same machine where analysis takes place (so running analysis on a linux gcc container when coverage was produced on a macOS machine won’t work).

@Akhil_Verma Coming back to this, I am guessing you are trying to do it this way because of some limitation on Bitbucket regarding macOS runners? Where are you usually building this code?

Hi @Colin

I am building my code on my local device(MacBook).

After successfully testing my code on Xcode the process we are following is as follows:

  1. I am building the project on my local machine with
    xcodebuild -project swift-coverage-example.xcodeproj/ -scheme swift-coverage-example -derivedDataPath Build/ -enableCodeCoverage YES clean build test CODE_SIGN_IDENTITY="" CODE_SIGNING_REQUIRED=NO

  2. Creating a code coverage report using:
    bash xccov-to-sonarqube-generic.sh Build/Logs/Test/*.xcresult/ > sonarqube-generic-coverage.xml

and the script I am using xccov-to-sonarqube-generic.sh you can find here
https://github.com/SonarSource/sonar-scanning-examples/blob/master/swift-coverage/swift-coverage-example/xccov-to-sonarqube-generic.sh

** This command will generate a generic code coverage report.

  1. Then committing the changes to BitBucket.

PFA bitbucket-pipeline.yml file and generic code coverage file,

sonarqube-generic-coverage.xml.zip (555 Bytes)

bitbucket-pipelines.yml.zip (1.5 KB)