Xcode Previews doesn't work when used in a package that imports other packages.

I keep getting a Xcode Previews error in Xcode 12 from a package that imports other packages. As soon as I remove the dependency I'm able to see the SwiftUI preview. I'm only able to use previews in packages that have no dependencies

The error I get is: "LoadingError: failed to load library at path...Library not loaded " then it point to the dependency that it could not load in the current package.

How can I access Xcode Previews from a package that depends on other packages?
Answered by Developer Tools Engineer in 622246022
Hi,

Sorry to hear you are having problems. We have a received a feedback tracking this same issue, and we are actively investigating it. So far no known workaround.
Steps to reproduce this error:
  1. Git clone TestPackage1 & TestPackage2 at the same level in a directory

  2. Open TestPackage2 and go to Sources/TestPackage2/SwiftUIView2.swift and try to start an Xcode preview

  3. You get the error I mentioned above

  4. Remove the TestPackage1 dependency in TestPackage2/Package.swift

change line 33 from:
Code Block swift
dependencies: ["TestPackage1"]
to:
Code Block swift
dependencies: []

5. Repeat step 2 and this time Xcode preview builds with no error
I think this should be fixable by updating the "frameworks search path", build setting. But haven't been able to figure out exactly what to add.
Accepted Answer
Hi,

Sorry to hear you are having problems. We have a received a feedback tracking this same issue, and we are actively investigating it. So far no known workaround.
We are seeing the same issue here. Our library's SwiftUI preview stopped working as soon as we included another library. :(
Still experiencing this issue
Hi Apple,

Thank you so much for your continued great work on SPM and SwiftUI.

I am still experiencing this issue. -- as of Xcode 12.3 (12C33).

Can you please provide us with an update?


Still experiencing the same problem with Xcode 12.4 with macOS Big Sur v11.2.1 and iPad Pro iOS 14.3.
An update on this would be really nice!
I have had the same problem with displaying previews in Swift packages.
My project architecture is: UIComponents (local package) -> AppPackage (local package) -> App
What I did was to add .dynamic parameter to .library declaration in both packages.
Now I can see previews for all SwiftUI files, both in UIComponents and AppPackage.
Code Block swift
products: [
.library(
name: "UIComponents",
type: .dynamic, /* added parameter */
targets: ["UIComponents"])
],

Code Block swift
products: [
.library(
name: "AppPackage",
type: .dynamic, /* added parameter */
targets: ["AppPackage"])
],
dependencies: [
.package(path: "../UIComponents")
],


I'm still having the same issue. All my packages are .dynamics but I'm with the same problem:

LoadingError: failed to load library at path...Library not loaded

I've opened a bug report (FB9067192).
Xcode 12.4 (12D4e)
I have the same issue. None of my views inside my framework project are showing on Swift UI Previews.
I'm using UIKit and trying to use the Swift UI Preview.

This needs an updated answer. The last official answer was almost a year ago saying there is no work-around and is being "actively investigated". What is the timeline for this to be solved? Utterly ridiculous.

My package's min iOS Version was iOS 12. All my SwiftUI views are obviously only available in iOS 13+. When developing I set the min iOS Version to iOS 13 and it works. Not very pleasant but at least I can use previews while developing now.

I continue to see issues with this. We need this fixed ASAP

In my case, swift package generate-xcodeproj and opening the .xcodeproj file instead of Package.swift made it possible to access the Xcode preview.

Just ran across this on a project that is being refactored to have "better" code structure. This inability to preview views within a package that depend upon another package is pretty surprising, given the push toward Packages, the age of this post, and the number of upvotes here.

In my experience, it's not that it couldn't find the package—adding the full path to the package ("/my/specific/dir/PackageName" instead of "../PackageName") did not work.

The project builds and runs fine for simulators and the automated tests run and pass, but it does not build for previews.

I sometimes see crashes, and sometimes the inline xcode source code error "could not find module '' for target 'x86_64-apple-ios-simulator'; found: arm64, arm64-apple-ios-simulator".

Those details seem interesting (not available for x86 when running simulator)—is this what others are seeing? I've tried setting various architectures—my Mac, my Mac (catalyst), iOS simulator running iOS15, etc.

For now, my workaround may be to not have packages that depend upon each other, which partly defeats the purpose. And I may file a bug report, but I may find existing and upvote.

Xcode Previews doesn't work when used in a package that imports other packages.
 
 
Q