How to use Sonarqube in Swift projects
Tags: swift, ios, sonar
Published: 2019-04-29 11:24:11

---
title: How to use Sonarqube in Swift projects
published: true
description: 
tags: swift,ios,sonar
---

## Install Sonarqube
https://docs.sonarqube.org/latest/setup/get-started-2-minutes/

- Download Sonarqube for macOS https://www.sonarqube.org/downloads/
- Put it in `~/sonarqube`
- Run localhost server `~/sonarqube/bin/macosx-universal-64/sonar.sh console`
- Login `http://localhost:9000` with `admin/admin`
- Create new project

## Install Sonar scanner
https://docs.sonarqube.org/display/SCAN/Analyzing+with+SonarQube+Scanner
- Download for macOS 64 bit
- Put it in `~/sonarscanner`
- export `PATH=$PATH:/Users/khoa/sonarscanner/bin`
- Go to project, create `sonar-project.properties`

```
# must be unique in a given SonarQube instance
sonar.projectKey=my-app
# this is the name and version displayed in the SonarQube UI. Was mandatory prior to SonarQube 6.1.
sonar.projectName=My App
sonar.projectVersion=1.0
 
# Path is relative to the sonar-project.properties file. Replace "\" by "/" on Windows.
# This property is optional if sonar.modules is set. 
sonar.sources=.
 
# Encoding of the source code. Default is default system encoding
#sonar.sourceEncoding=UTF-8
```

- Run `sonar-scanner`

## Install swift plugin
https://github.com/Backelite/sonar-swift

- Download jar file https://github.com/Backelite/sonar-swift/releases
- Follow https://docs.sonarqube.org/latest/setup/install-plugin/
- Put jar file into `~/sonarqube/extensions/plugins`
- Restart server
- Copy run-sonar-swift.sh https://github.com/Backelite/sonar-swift/blob/develop/sonar-swift-plugin/src/main/shell/run-sonar-swift.sh to project folder
- Go to Quality Profile http://localhost:9000/profiles?language=swift

<img width="979" alt="a" src="https://user-images.githubusercontent.com/2284279/56885533-ed320000-6a6c-11e9-8550-7b24487a0a45.png">

## run-sonar-swift.sh

- Update sonar-project.properties

```
sonar.swift.appScheme=MyApp Staging
sonar.swift.project=MyApp.xcodeproj
sonar.swift.workspace=MyApp.xcworkspace
sonar.swift.simulator=platform=iOS Simulator,name=iPhone Xʀ
```

- Run `chmod +x run-sonar-swift.sh`
- Run `./run-sonar-swift.sh`

## Troubleshooting

### failed with error code: 64 https://github.com/Backelite/sonar-swift/issues/222
When run `./run-sonar-swift.sh`

```
xcodebuild: error: ''MyApp.xcodeproj'' does not exist.
2019-04-29 12:10:17.486 defaults[4134:569992]
Domain CFBundleShortVersionString does not exist
.Extracting Xcode project informationxcodebuild: error: option 'Destination' requires at least one parameter of the form 'key=value'
```

👉 Remove quotes in `sonar-project.properties`
👉 Modify run-sonar-swift.sh, add these before `Check for mandatory parameters` section

Surround by double quotes

```
projectFile="\"$projectFile\""
workspaceFile="\"$workspaceFile\""
appScheme="\"$appScheme\""
destinationSimulator="\"$destinationSimulator\""
```

😢 Does not work
👉 Need to create a scheme name without space

### Error code 65

👉Specify team in Xcode project

### destination

Need double quotes

`destinationSimulator="\"$destinationSimulator\""`

Use equal sign `=` 

```
-destination="$destinationSimulator"
```

instead of space ' '

```
-destination "$destinationSimulator"
```

### Metric 'files' should not be computed by a Sensor

When run `sonar-scanner` https://github.com/Backelite/sonar-swift/issues/212

```
11:01:14.406 INFO: Sensor JaCoCo XML Report Importer [jacoco]
11:01:14.409 DEBUG: No reports found
11:01:14.409 INFO: Sensor JaCoCo XML Report Importer [jacoco] (done) | time=3ms
11:01:14.409 INFO: Sensor SwiftLint [backelitesonarswiftplugin]
11:01:14.417 INFO: Sensor SwiftLint [backelitesonarswiftplugin] (done) | time=8ms
11:01:14.417 INFO: Sensor Tailor [backelitesonarswiftplugin]
11:01:14.418 INFO: Sensor Tailor [backelitesonarswiftplugin] (done) | time=1ms
11:01:14.418 INFO: Sensor OCLint [backelitesonarswiftplugin]
11:01:14.419 INFO: Sensor OCLint [backelitesonarswiftplugin] (done) | time=1ms
11:01:14.419 INFO: Sensor FauxPas [backelitesonarswiftplugin]
11:01:14.419 INFO: Sensor FauxPas [backelitesonarswiftplugin] (done) | time=0ms
11:01:14.419 INFO: Sensor Swift Squid [backelitesonarswiftplugin]
11:01:14.526 INFO: ------------------------------------------------------------------------
11:01:14.526 INFO: EXECUTION FAILURE
11:01:14.526 INFO: ------------------------------------------------------------------------
11:01:14.527 INFO: Total time: 6.180s
11:01:14.603 INFO: Final Memory: 25M/566M
11:01:14.603 INFO: ------------------------------------------------------------------------
11:01:14.603 ERROR: Error during SonarQube Scanner execution
java.lang.UnsupportedOperationException: Metric 'files' should not be computed by a Sensor
	at org.sonar.scanner.sensor.DefaultSensorStorage.saveMeasure(DefaultSensorStorage.java:168)
``` 

👉Install maven https://maven.apache.org/download.cgi
Edit `ObjectiveCSquidSensor.java` and `SwiftSquidSensor`, remove line with `CoreMetrics.FILES`
Run `export PATH=$PATH:/Users/khoa/apache-maven/bin`
Run `./build-and-deploy.sh`
Or `~/apache-maven/bin/mvn clean install`

🎉 Built jar is in `sonar-swift-plugin/target/backelite-sonar-swift-plugin-0.4.4.jar`, copy back to `extensions/plugins`


### How to enable SwiftLint as default profile 🤔

👉 Need to close current Sonar tab and restart server

### Testing failed: unable to attach DB

Modify `run-sonar-swift.sh` to add `-UseModernBuildSystem=NO` to `buildCmd+=(-destination`

### slather No coverage directory found

👉 Enable coverage option in scheme -> Test

![](https://github.com/SlatherOrg/slather/raw/master/README_Images/test_scheme.png)

Optional: declare `.slather.yml` file https://github.com/SlatherOrg/slather

### Unable to execute SonarQube

```
14:53:23.251 ERROR: Error during SonarQube Scanner execution
org.sonarsource.scanner.api.internal.ScannerException: Unable to execute SonarQube
	at org.sonarsource.scanner.api.internal.IsolatedLauncherFactory.lambda$createLauncher$0(IsolatedLauncherFactory.java:85)
```

👉Start sonar server

---

❤️ Support my app ❤️ 

- [Push Hero - pure Swift native macOS application to test push notifications](https://www.producthunt.com/posts/push-hero-2)
- [Quick Access - Organise files in the Mac menu bar](https://www.producthunt.com/posts/quick-access)
- [Frame recorder - Recorder gif and video with frame](https://www.producthunt.com/posts/frame-recorder)
- [Other apps](https://onmyway133.github.io/projects/)

❤️❤️😇😍🤘❤️❤️