onmyway133 / blog

🍁 What you don't know is what you haven't learned
https://onmyway133.com/
MIT License
672 stars 33 forks source link

How to use Sonarqube in Swift projects #216

Open onmyway133 opened 5 years ago

onmyway133 commented 5 years ago

Install Sonarqube

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

Install Sonar scanner

https://docs.sonarqube.org/display/SCAN/Analyzing+with+SonarQube+Scanner

# 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

Install swift plugin

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

a

Skip some tools

Modify run-sonar-swift.sh

vflag=""
nflag=""
unittests="on"
swiftlint="on"
tailor="off"
lizard="on"
oclint="off"
fauxpas="off"
sonarscanner=""

Git ignore

.scannerwork/
sonar-reports/
compile_commands.json

run-sonar-swift.sh

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

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

Try running

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

πŸ‘‰ 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)

πŸ‘‰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

πŸ‘‰ Run lizard manually

lizard --xml sonar-reports/lizard-report.xml
paicolman commented 5 years ago

Thanks man for all the tipps, was lost editing properties... Unfortunately, the hack for the "Metric 'files' should not be computed by a Sensor" did not work... commented out the line in both files, compiled with maven but get the same issue.

sandeepbol commented 5 years ago

I am getting LizardReportParser$SwiftFunction cannot be cast error. What do you mean run lizard manually? Even if we run it manually, next sonarqube scan fails with the same error and no reports get uploaded to the server.

sandeepbol commented 5 years ago

@onmyway133 You may want to look at (and add in your guide) https://github.com/Backelite/sonar-swift/issues/225#issuecomment-497445343 for "LizardReportParser$SwiftFunction cannot be cast" error.

nascimentorafael commented 5 years ago

I tried out theMetric 'files' should not be computed by a Sensor workaround, but I'm getting a different error now:

ERROR: Error during SonarQube Scanner execution
java.lang.UnsupportedOperationException: Can not add the same measure twice on Source/XXXX/XXXX.swift: DefaultMeasure[component=Source/XXXX/XXXX.swift,metric=Metric[id=<null>,key=ncloc,description=Non commenting lines of code,type=INT,direction=-1,domain=Size,name=Lines of Code,qualitative=false,userManaged=false,enabled=true,worstValue=<null>,bestValue=<null>,optimizedBestValue=false,hidden=false,deleteHistoricalData=false,decimalScale=<null>],value=6,fromCore=false]
    at org.sonar.scanner.sensor.DefaultSensorStorage.saveMeasure(DefaultSensorStorage.java:172)
    at org.sonar.scanner.sensor.DefaultSensorStorage.store(DefaultSensorStorage.java:132)
    at org.sonar.api.batch.sensor.measure.internal.DefaultMeasure.doSave(DefaultMeasure.java:95)
    at org.sonar.api.batch.sensor.internal.DefaultStorable.save(DefaultStorable.java:45)
    at com.backelite.sonarqube.commons.MeasureUtil.saveMeasure(MeasureUtil.java:34)
    at com.backelite.sonarqube.swift.SwiftSquidSensor.saveMeasures(SwiftSquidSensor.java:86)
    at com.backelite.sonarqube.swift.SwiftSquidSensor.save(SwiftSquidSensor.java:79)
    at com.backelite.sonarqube.swift.SwiftSquidSensor.execute(SwiftSquidSensor.java:140)
    at org.sonar.scanner.sensor.AbstractSensorWrapper.analyse(AbstractSensorWrapper.java:48)
    at org.sonar.scanner.sensor.ModuleSensorsExecutor.execute(ModuleSensorsExecutor.java:85)
    at org.sonar.scanner.sensor.ModuleSensorsExecutor.lambda$execute$1(ModuleSensorsExecutor.java:59)
    at org.sonar.scanner.sensor.ModuleSensorsExecutor.withModuleStrategy(ModuleSensorsExecutor.java:77)
    at org.sonar.scanner.sensor.ModuleSensorsExecutor.execute(ModuleSensorsExecutor.java:59)
    at org.sonar.scanner.scan.ModuleScanContainer.doAfterStart(ModuleScanContainer.java:82)
    at org.sonar.core.platform.ComponentContainer.startComponents(ComponentContainer.java:136)
    at org.sonar.core.platform.ComponentContainer.execute(ComponentContainer.java:122)
    at org.sonar.scanner.scan.ProjectScanContainer.scan(ProjectScanContainer.java:400)
    at org.sonar.scanner.scan.ProjectScanContainer.scanRecursively(ProjectScanContainer.java:395)
    at org.sonar.scanner.scan.ProjectScanContainer.doAfterStart(ProjectScanContainer.java:358)
    at org.sonar.core.platform.ComponentContainer.startComponents(ComponentContainer.java:136)
    at org.sonar.core.platform.ComponentContainer.execute(ComponentContainer.java:122)
    at org.sonar.scanner.bootstrap.GlobalContainer.doAfterStart(GlobalContainer.java:141)
    at org.sonar.core.platform.ComponentContainer.startComponents(ComponentContainer.java:136)
    at org.sonar.core.platform.ComponentContainer.execute(ComponentContainer.java:122)
    at org.sonar.batch.bootstrapper.Batch.doExecute(Batch.java:73)
    at org.sonar.batch.bootstrapper.Batch.execute(Batch.java:67)
    at org.sonarsource.scanner.api.internal.batch.BatchIsolatedLauncher.execute(BatchIsolatedLauncher.java:46)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.base/java.lang.reflect.Method.invoke(Unknown Source)
    at org.sonarsource.scanner.api.internal.IsolatedLauncherProxy.invoke(IsolatedLauncherProxy.java:60)
    at com.sun.proxy.$Proxy0.execute(Unknown Source)
    at org.sonarsource.scanner.api.EmbeddedScanner.doExecute(EmbeddedScanner.java:185)
    at org.sonarsource.scanner.api.EmbeddedScanner.execute(EmbeddedScanner.java:137)
    at org.sonarsource.scanner.cli.Main.execute(Main.java:112)
    at org.sonarsource.scanner.cli.Main.execute(Main.java:75)
    at org.sonarsource.scanner.cli.Main.main(Main.java:61)

Any ideas what this could be?

INFO: sonar-scanner-cli-4.0.0.1744-macosx INFO: 12.0.2, x86_64: "Java SE 12.0.2" INFO: Mac OS 10.14.5 INFO: sonarqube-developer-7.9.1

shuaibimran commented 5 years ago

when these fixes will be merge into repository? anyone have any idea.

harshita29 commented 4 years ago

Hi I am facing an issues as

15:30:48.346 INFO: ------------------------------------------------------------------------ 15:30:48.346 INFO: EXECUTION FAILURE 15:30:48.346 INFO: ------------------------------------------------------------------------ 15:30:48.346 INFO: Total time: 7.601s 15:30:48.382 INFO: Final Memory: 21M/74M 15:30:48.382 INFO: ------------------------------------------------------------------------ 15:30:48.382 ERROR: Error during SonarQube Scanner execution java.lang.UnsupportedOperationException: Can not add the same measure twice on MyStocksPOC/AssetListViewController/AssestListTableViewCells/MyStocks_AssestListTableViewCell.m: DefaultMeasure[component=MyStocksPOC/AssetListViewController/AssestListTableViewCells/MyStocks_AssestListTableViewCell.m,metric=Metric[id=,key=ncloc,description=Non commenting lines of code,type=INT,direction=-1,domain=Size,name=Lines of Code,qualitative=false,userManaged=false,enabled=true,worstValue=,bestValue=,optimizedBestValue=false,hidden=false,deleteHistoricalData=false,decimalScale=],value=3,fromCore=false,storage=org.sonar.scanner.sensor.DefaultSensorStorage@5834fd3f,saved=false] at org.sonar.scanner.sensor.DefaultSensorStorage.saveMeasure(DefaultSensorStorage.java:177) at org.sonar.scanner.sensor.DefaultSensorStorage.store(DefaultSensorStorage.java:137) at org.sonar.api.batch.sensor.measure.internal.DefaultMeasure.doSave(DefaultMeasure.java:96) at org.sonar.api.batch.sensor.internal.DefaultStorable.save(DefaultStorable.java:45) at com.sonar.cpp.plugin.LexicalAnalyzer.measure(LexicalAnalyzer.java:140) at com.sonar.cpp.plugin.LexicalAnalyzer.analyze(LexicalAnalyzer.java:69) at com.sonar.cpp.plugin.LexicalAnalyzer.analyze(LexicalAnalyzer.java:59) at com.sonar.cpp.plugin.CFamilySensor.execute(CFamilySensor.java:191) at org.sonar.scanner.sensor.AbstractSensorWrapper.analyse(AbstractSensorWrapper.java:48) at org.sonar.scanner.sensor.ModuleSensorsExecutor.execute(ModuleSensorsExecutor.java:85) at org.sonar.scanner.sensor.ModuleSensorsExecutor.execute(ModuleSensorsExecutor.java:62) at org.sonar.scanner.scan.ModuleScanContainer.doAfterStart(ModuleScanContainer.java:82) at org.sonar.core.platform.ComponentContainer.startComponents(ComponentContainer.java:136) at org.sonar.core.platform.ComponentContainer.execute(ComponentContainer.java:122) at org.sonar.scanner.scan.ProjectScanContainer.scan(ProjectScanContainer.java:387) at org.sonar.scanner.scan.ProjectScanContainer.scanRecursively(ProjectScanContainer.java:383) at org.sonar.scanner.scan.ProjectScanContainer.doAfterStart(ProjectScanContainer.java:346) at org.sonar.core.platform.ComponentContainer.startComponents(ComponentContainer.java:136) at org.sonar.core.platform.ComponentContainer.execute(ComponentContainer.java:122) at org.sonar.scanner.bootstrap.GlobalContainer.doAfterStart(GlobalContainer.java:141) at org.sonar.core.platform.ComponentContainer.startComponents(ComponentContainer.java:136) at org.sonar.core.platform.ComponentContainer.execute(ComponentContainer.java:122) at org.sonar.batch.bootstrapper.Batch.doExecute(Batch.java:72) at org.sonar.batch.bootstrapper.Batch.execute(Batch.java:66) at org.sonarsource.scanner.api.internal.batch.BatchIsolatedLauncher.execute(BatchIsolatedLauncher.java:46) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at java.base/java.lang.reflect.Method.invoke(Unknown Source) at org.sonarsource.scanner.api.internal.IsolatedLauncherProxy.invoke(IsolatedLauncherProxy.java:60) at com.sun.proxy.$Proxy0.execute(Unknown Source) at org.sonarsource.scanner.api.EmbeddedScanner.doExecute(EmbeddedScanner.java:189) at org.sonarsource.scanner.api.EmbeddedScanner.execute(EmbeddedScanner.java:138) at org.sonarsource.scanner.cli.Main.execute(Main.java:112) at org.sonarsource.scanner.cli.Main.execute(Main.java:75) at org.sonarsource.scanner.cli.Main.main(Main.java:61)

Can Anyone please help me regarding this?