SwiftUI Previews broken on local Swift Package with dependencies

I have a project with two local packages

  • One client package with an interface and some models with dynamic type in the Package.Swift
  • One feature package with the UI and a dependency to the client package

When I try to preview a view that is not using any models or code from the client package, it loads just fine e.g. a view that is just a container to display things like a card

But when I tried to preview any other view that actually uses models from the client package, it just fails

the first few lines of the preview error display

LinkDylibError: Failed to build <filename>.swift
Linking failed: linker command failed with exit code 1 (use -v to see invocation)

ld: warning: search path '/Applications/Xcode-15.4.0.app/Contents/SharedFrameworks-iphonesimulator' not found
    Undefined symbols for architecture arm64:

Also, I'm using Xcode 15.4 and iOS 17 as the min version

Did you find the solution to this?

Are you able to try out your project using the new Xcode 16 beta?

I've been dealing with this problem for HOURS and your description has helped me come up with the 'solution'.

TLDR: (temporarily) change the dynamic package to static during development, then switch back to dynamic before deploying.

Obviously this only works if you control the client package.

I'm not expert, but here's my explanation:

Since the client package is dynamic, it's technically not supposed to be linked/accessed until runtime. And I don't think SwiftUI previews count as 'runtime'.

So, when the SwiftUI preview tries to access code from a dynamic library, it's not available.

This problem does NOT occur in an Xcode project because you have to embed the packages in the main target, which I guess solves the problem.

Unfortunately, there's no option to embed (even temporarily) in a swift package. So there's no way to allow the SwiftUI preview to gain access to the third party dynamic libraries.

When I commented out the dynamic declaration in the package.swift file (converting the dynamic library to static), the previews in my swift package that used the dynamic (now static) library started working. I'll likely write a script into my CI/CD to uncomment the line (and switch the package back to dynamic) before deploying so I don't forget.

I hope this helps. And shame on Apple for knowing about this problem for YEARS and ignoring it.

SwiftUI Previews broken on local Swift Package with dependencies
 
 
Q