DEV Community

Khoa Pham
Khoa Pham

Posted on • Updated on

How to use Sonarqube in Swift projects

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


Enter fullscreen mode Exit fullscreen mode
  • Run sonar-scanner

Install swift plugin

https://github.com/Backelite/sonar-swift

a

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ʀ


Enter fullscreen mode Exit fullscreen mode
  • 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'


Enter fullscreen mode Exit fullscreen mode

👉 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\""


Enter fullscreen mode Exit fullscreen mode

😢 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"


Enter fullscreen mode Exit fullscreen mode

instead of space ' '



-destination "$destinationSimulator"


Enter fullscreen mode Exit fullscreen mode

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)


Enter fullscreen mode Exit fullscreen mode

👉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

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)


Enter fullscreen mode Exit fullscreen mode

👉Start sonar server


❤️ Support my app ❤️

❤️❤️😇😍🤘❤️❤️

Top comments (1)

Collapse
 
mercury77 profile image
mercury77 • Edited

Hi

I've read the guide , but maybe I've done some mistakes ,can you hepl me on this ?

Creating directory sonar-reports/
Building & extracting Xcode project information

  • xcodebuild clean build-for-testing -workspace MyApp.xcworkspace -scheme MyApp '-destination="platform=iOS Simulator,name=iPhone 8,OS=11.4"' -destination-timeout 360 COMPILER_INDEX_STORE_ENABLE=NO
    2020-03-19 15:47:08.898 xcodebuild[13418:30032] Error writing xctestrun file: Error Domain=NSCocoaErrorDomain Code=4 "The folder “MyApp_iphoneos13.2-arm64e.xctestrun” doesn’t exist." UserInfo={NSFilePath=/var/root/Library/Developer/Xcode/DerivedData/MyApp-fjbdzxyjiiznisbynvxkrvwqxovi/Build/Products/MyApp_iphoneos13.2-arm64e.xctestrun, NSUserStringVariant=Folder, NSUnderlyingError=0x7fc9599b9dc0 {Error Domain=NSPOSIXErrorDomain Code=2 "No such file or directory"}}
    ** TEST BUILD FAILED **

  • returnValue=65

  • set +x
    ERROR - Command 'xcodebuild clean build-for-testing -workspace MyApp.xcworkspace -scheme MyApp -destination="platform=iOS Simulator,name=iPhone 8,OS=11.4" -destination-timeout 360 COMPILER_INDEX_STORE_ENABLE=NO' failed with error code: 65