SonarScanner SwiftUI Preview Macro

I have a project that is written in Swift and makes use of SwiftUI. For many of those SwiftUI views, we have Previews which use the SwiftUI Preview Macros.

This has worked just fine with SonarQube until today. As of this morning, SonarScans are failing on the last line of any file that has a SwiftUI #Preview macro in it. with an error like the following
ERROR: String is not parsed (file Client/Sources/MySwiftUIView.swift, line 127)

Has anyone else noticed this and is it a result of any updates or configuration changes?

+ echo 'Generating JUnit report'
Generating JUnit report
+ xchtmlreport -j /var/folders/7l/rszmbrmd6tv4pg52z6ql_5rc0000gn/T/XCUITestOutput667838978/Test-All.xcresult
Report successfully created at /var/folders/7l/rszmbrmd6tv4pg52z6ql_5rc0000gn/T/XCUITestOutput667838978/index.html
JUnit report successfully created at /var/folders/7l/rszmbrmd6tv4pg52z6ql_5rc0000gn/T/XCUITestOutput667838978/report.junit
+ echo 'Converting code coverage log to Sonar format'
Converting code coverage log to Sonar format
+ ./xccov-to-sonarqube-generic.sh /var/folders/7l/rszmbrmd6tv4pg52z6ql_5rc0000gn/T/XCUITestOutput667838978/Test-All.xcresult
+ echo 'Appending Sonar Properties'
Appending Sonar Properties
+ echo ''
+ echo sonar.host.url=[REDACTED]([REDACTED])
+ echo sonar.login=[REDACTED]
+ echo sonar.coverageReportPaths=./cov.xml
+ echo sonar.junit.reportPaths=./report.junit
+ echo sonar.cfamily.compile-commands=compile_commands.json
+ echo 'sonar.exclusions=**/Generated/*,**/Derived/Sources/*,**/Project.swift,**/Box2D/**/*,**/Mock/**/*,**/Ampli.swift,**/MockDataClient.swift'
+ [[ false = \t\r\u\e ]]
+ echo sonar.branch.name=hotfix/sonar-build-fixes
+ max_iteration=2
+ set +e
++ seq 1 2
+ for i in '$(seq 1 $max_iteration)'
+ sonar-scanner
INFO: Scanner configuration file: /opt/homebrew/Cellar/sonar-scanner/5.0.1.3006/libexec/conf/sonar-scanner.properties
INFO: Project root configuration file: /Users/vagrant/git/sonar-project.properties
INFO: SonarScanner 5.0.1.3006
INFO: Java 21.0.2 Homebrew (64-bit)
INFO: Mac OS X 14.1.2 aarch64
INFO: User cache: /Users/vagrant/.sonar/cache
INFO: Analyzing on SonarQube server 9.9.0.65466
INFO: Default locale: "en_US", source code encoding: "UTF-8" (analysis is platform dependent)
INFO: Load global settings
INFO: Load global settings (done) | time=350ms
INFO: Server id: 51804F84-AYdM6nLjPzIPvAlP20k9
INFO: User cache: /Users/vagrant/.sonar/cache
INFO: Load/download plugins
INFO: Load plugins index
INFO: Load plugins index (done) | time=94ms
INFO: Load/download plugins (done) | time=7706ms
INFO: Loaded core extensions: developer-scanner
INFO: Process project properties
INFO: Process project properties (done) | time=6ms
INFO: Execute project builders
INFO: Execute project builders (done) | time=1ms
INFO: Project key: iOS
INFO: Base dir: /Users/vagrant/git
INFO: Working dir: /Users/vagrant/git/.scannerwork
INFO: Load project settings for component key: 'iOS'
INFO: Load project settings for component key: 'ios' (done) | time=86ms
INFO: Load project branches
INFO: Load project branches (done) | time=84ms
INFO: Load branch configuration
INFO: Found manual configuration of branch/PR analysis. Skipping automatic configuration.
INFO: Load branch configuration (done) | time=1ms
INFO: Auto-configuring with CI 'Bitrise'
INFO: Load quality profiles
INFO: Load quality profiles (done) | time=125ms
INFO: Load active rules
INFO: Load active rules (done) | time=4490ms
INFO: Load analysis cache
INFO: Load analysis cache (404) | time=73ms
INFO: Branch name: hotfix/sonar-build-fixes
INFO: Load project repositories
INFO: Load project repositories (done) | time=106ms
INFO: Indexing files...
INFO: Project configuration:
INFO: Excluded sources: **/Generated/*, **/Derived/Sources/*, **/Project.swift, **/Box2D/**/*, **/Mock/**/*, **/Ampli.swift, **/MockDataClient.swift
INFO: Excluded sources for coverage: **/Generated/*, **/Derived/Sources/*, **/Project.swift
INFO: 1427 files indexed
INFO: 97 files ignored because of inclusion/exclusion patterns
INFO: 0 files ignored because of scm ignore settings
INFO: Quality profile for c: Sonar way
INFO: Quality profile for json: Sonar way
INFO: Quality profile for objc: Sonar way
INFO: Quality profile for swift: Sonar way
INFO: ------------- Run sensors on module iOS
INFO: Load metrics repository
INFO: Load metrics repository (done) | time=84ms
INFO: Sensor IaC CloudFormation Sensor [iac]
INFO: 0 source files to be analyzed
INFO: 0/0 source files have been analyzed
INFO: Sensor IaC CloudFormation Sensor [iac] (done) | time=98ms
INFO: Sensor IaC Kubernetes Sensor [iac]
INFO: 0 source files to be analyzed
INFO: 0/0 source files have been analyzed
INFO: Sensor IaC Kubernetes Sensor [iac] (done) | time=28ms
INFO: Sensor C# Project Type Information [csharp]
INFO: Sensor C# Project Type Information [csharp] (done) | time=1ms
INFO: Sensor C# Analysis Log [csharp]
INFO: Sensor C# Analysis Log [csharp] (done) | time=7ms
INFO: Sensor C# Properties [csharp]
INFO: Sensor C# Properties [csharp] (done) | time=0ms
INFO: Sensor HTML [web]
INFO: Sensor HTML [web] (done) | time=5ms
INFO: Sensor TextAndSecretsSensor [text]
INFO: 1406 source files to be analyzed
INFO: 1406/1406 source files have been analyzed
INFO: Sensor TextAndSecretsSensor [text] (done) | time=1854ms
INFO: Sensor VB.NET Project Type Information [vbnet]
INFO: Sensor VB.NET Project Type Information [vbnet] (done) | time=1ms
INFO: Sensor VB.NET Analysis Log [vbnet]
INFO: Sensor VB.NET Analysis Log [vbnet] (done) | time=6ms
INFO: Sensor VB.NET Properties [vbnet]
INFO: Sensor VB.NET Properties [vbnet] (done) | time=0ms
INFO: Sensor Swift Code Quality and Security [swift]
INFO: 904 source files to be analyzed
ERROR: String is not parsed (file Client/Sources/OneOfOurSwiftUIFilesWithPreview.swift, line 52)

I am currently working on seeing if I can adjust the exclusions to avoid this issue.
Again, This was not a problem until last night.

Hey there.

We have some open tickets related to supporting Swift macros ( SONARSWIFT-550 and [SONARSWIFT-553] - Jira).

You can either ignore the error or exclude files that use this feature from analysis (analysis scope docs)

I tried the following, but did not have any success


Then I wrapped all the SwiftUI preview macros in comments indicating that the block should be ignored

// SONAR_IGNORE_START
#Preview { /* some preview */ }
// SONAR_IGNORE_END

However, this still results in the error. I suspect this is because there is not an issue that we are ignoring but rather an error with sonar parsing the file.

You’ll need to exclude the file entirely. The docs also cover this.

I have the same problem. Is there a plan when this will be fixed?

Is there anything going on on those tickets? With Swift 5.9 and many interesting Swift Macros this issue is a bummer. I’m assuming #Preview just got a backfire as this feature is available in Swift for years. With our SwiftUI heavy project this is a deal breaker to exclude all SwiftUI related code. What are the plans to fix this?

Hey @mmysliwiec

Swift is not a particular high-priority language for us (compared to our flagship languages), meaning it tends to get worked on only a few times a year. I am sure we will have a sprint targeting Swift before the end of the year (before the next SonarQube LTA), but that’s all I really estimate at this time.

I know it’s not good news, but I prefer to be fully transparent rather than just say “it’s coming eventually”.

OK, I understand. Will wait patiently and until then I’ll exclude problematic files.

1 Like

Hi Colin (and all),
One more question: is there a roadmap for this issue ([SONARSWIFT-553] - Jira)?
I’ve a customer interested in Enterprise Edition due to the availability of Swift language. In their code, the Preview macro is used a lot, but all lines in the source file that declare this macro are excluded from analysis! So it might be a blocking issue for purchase…
Thanks
a.

Sorry, is not true: source file lines are parsed…
Sorry for bothering.

Is it working for you with #Preview macro? Was there any update on the Sonar Swift plugin?