Xcode 14 try to build all SPM when building swiftUI preview

HI, I have an issue displaying SwiftUI previews with Xcode 14.

My project has iOS and an watchOS Target. It also includes a lot of SPM, most of them only used by the PhoneApp. The project build successfully for both iOS and watchOS.

I can see that when building the watch target, only SPM that are included in this target are build for for watchOS

The issue is when I try to build a swiftUI preview of the watch target. In this case I can see on the build for Preview log that Xcode try to build ALL spm defined in the Xcode project, including the one that are not used by the watch Target, As a lot if spm are for iOS only, and doesn't build for watchOS, the build for preview fails, and I'm unable to preview my swiftUI views on the AppeWatch.

Note that my project is working fine on Xcode 13.

This issue is easy to reproduce in any Xcode projects containing a iOS and watchOS target by adding a SPM that is only for iOS.

Any workaround to fix this issue ?

Answered by cylox in 749832022

This problem still exists in Xcode 14.3 (14E222b)

To eliminate any ambiguity or artifacts from my existing project, I verified this in but in the simplest case, using Xcode 15.2:

  • I created a simple project in Xcode: New Project, creating a watchOS app with a new companion iOS app.
  • I verified previews work for the ContentViews for both the watchOS and iOS targets.
  • Then File -> Add Package Dependencies... I added a package - in this case AWSiOSSDKV2 from https://github.com/aws-amplify/aws-sdk-ios-spm - and added it only to the iOS target. Verified it was added correctly in project settings, in the "Frameworks, Libraries, and Embedded Content" section for both iOS and watchOS targets
  • Previews no longer work for the watchOS target

I may have found a workaround, detailed here

Confirmed to fix FirebaseAnalytics installed via SPM, now my watchOS previews work. Not sure why this isn't default, only annoying thing is having to switch schemes for the relevant file, but not a huge breaker.

Xcode 14 try to build all SPM when building swiftUI preview
 
 
Q