How to use Sonarqube in Swift projects

Issue #216

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
1
2
3
4
5
6
7
8
9
10
11
12
# 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

a

Skip some tools

Modify run-sonar-swift.sh

1
2
3
4
5
6
7
8
9
vflag=""
nflag=""
unittests="on"
swiftlint="on"
tailor="off"
lizard="on"
oclint="off"
fauxpas="off"
sonarscanner=""

Git ignore

1
2
3
.scannerwork/
sonar-reports/
compile_commands.json

run-sonar-swift.sh

  • Update sonar-project.properties
1
2
3
4
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

1
2
3
4
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

1
2
3
4
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 =

1
-destination="$destinationSimulator"

instead of space ‘ ‘

1
-destination "$destinationSimulator"

Metric ‘files’ should not be computed by a Sensor

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
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

Try running

slather coverage –input-format profdata –cobertura-xml –output-directory sonar-reports –workspace MyApp.xcworkspace –scheme MyAppStaging MyApp.xcodeproj

1
2
3
4
5
6
7
8

👉 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)

1
2
3
4

👉Start sonar server

### LizardReportParser$SwiftFunction cannot be cast

Error during SonarQube Scanner execution
java.lang.ClassCastException: com.backelite.sonarqube.swift.complexity.LizardReportParser$SwiftFunction cannot be cast to org.sonar.api.batch.fs.internal.DefaultInputComponent

1
2

👉 Run [lizard](https://github.com/terryyin/lizard) manually

lizard –xml sonar-reports/lizard-report.xml
```

Comments