SwiftUI Preview - Undefined symbols for architecture arm64

Starting on Xcode 15.3 and 15.4 my SwiftUI Previews stopped working with the following error:

== PREVIEW UPDATE ERROR:

    LinkDylibError: Failed to build ContentView.swift
    
    Linking failed: linker command failed with exit code 1 (use -v to see invocation)
    
    ld: warning: search path '/Applications/Xcode.app/Contents/SharedFrameworks-iphonesimulator' not found
    Undefined symbols for architecture arm64:
      "static MyLibrary.DisplayUtil.getText() -> Swift.String", referenced from:
          closure #1 () -> SwiftUI.Text in (extension in Demo_Broken_Preview_PreviewReplacement_ContentView_1):Demo_Broken_Preview.ContentView.(__preview__body in _1BA320C8FB5388C953E1E463345C3D72).getter : some in ContentView.1.preview-thunk.o
      "type metadata accessor for MyLibrary.DisplayUtil", referenced from:
          closure #1 () -> SwiftUI.Text in (extension in Demo_Broken_Preview_PreviewReplacement_ContentView_1):Demo_Broken_Preview.ContentView.(__preview__body in _1BA320C8FB5388C953E1E463345C3D72).getter : some in ContentView.1.preview-thunk.o
    ld: symbol(s) not found for architecture arm64
    clang: error: linker command failed with exit code 1 (use -v to see invocation)

After much trial and error, I narrowed it down to my SwiftUI views using properties or functions inside components in Swift Packages.

This is a simple example of my Swift Package:

public class DisplayUtil {
    public func getText() -> String {
        return "Text"
    }
    
    public static func getText() -> String {
        return "Text"
    }
} 

And my SwiftUI View

import SwiftUI
import MyLibrary

struct ContentView: View {
    var body: some View {
        VStack {
            Text(DisplayUtil.getText())
        }
        .padding()
    }
}

#Preview {
    ContentView()
}

The same code works fine on Xcode 15.2

Link to the sample project:

https://www.icloud.com/iclouddrive/0c00AD0esi2PwqqiRggokfwGw#Demo%5FBroken%5FPreview

Is anybody else having a similar issue?

Answered by Developer Tools Engineer in 787811022

Hi, thanks for providing an example project.

This is a known issue. For now you can try the following workaround. If you set the product type in your package to dynamic then previews should work for you. In the example you provided you would do this as follows:

products: [
    // Products define the executables and libraries a package produces, making them visible to other packages.
    .library(
        name: "MyLibrary",
        type: .dynamic,
        targets: ["MyLibrary"]),
],
Accepted Answer

Hi, thanks for providing an example project.

This is a known issue. For now you can try the following workaround. If you set the product type in your package to dynamic then previews should work for you. In the example you provided you would do this as follows:

products: [
    // Products define the executables and libraries a package produces, making them visible to other packages.
    .library(
        name: "MyLibrary",
        type: .dynamic,
        targets: ["MyLibrary"]),
],
SwiftUI Preview - Undefined symbols for architecture arm64
 
 
Q