Swift Packages + Dependencies + SwiftUI = Fail to Preview

I'm trying to create a project that has the following structure:
  • Package Parser (a library and Resources)

  • Package Posts (a library with SwiftUI and Resources)

  • App itself

Isolated, both Packages build and Preview successfully. The App also runs fine.

When I add Package Parser to Package Posts as dependency to be able to use its Resources, I can build, test and run the App, but the Preview on Posts stop working.

The message from diagnostic is:
Code Block
ld: warning: directory not found for option '-F/Applications/Xcode-beta.app/Contents/SharedFrameworks-iphonesimulator'
Undefined symbols for architecture x86_64:
  "nominal type descriptor for Parser.XMLPost", referenced from:



Here are the packages definitions:
Code Block
let package = Package(
    name: "Parser",
    platforms: [.iOS(.v14)],
    products: [
        .library(name: "Parser",
                 targets: ["Parser"])
    ],
    targets: [
        .target(name: "Parser",
                resources: [.process("Resources")]),
        .testTarget(name: "ParserTests", dependencies: ["Parser"])
    ]
)

Code Block
let package = Package(
    name: "Posts",
    platforms: [.iOS(.v14)],
    products: [
        .library(name: "Posts", targets: ["Posts"])
    ],
    dependencies: [
        .package(path: "../Parser")
    ],
    targets: [
        .target(name: "Posts", dependencies: ["Parser"]),
        .testTarget(name: "PostsTests", dependencies: ["Posts"])
    ]
)


Is it possible to Preview a Swift Package that has a dependency? How?

Thanks and regards, Cassio
Post not yet marked as solved Up vote post of kasros Down vote post of kasros
2k views

Replies

Update:

Relevant snip of the view:
Code Block
public struct ListOfPosts: View {
    @State private var posts = [Post]()
    public var body: some View {
        return ScrollView {
            LazyVGrid(columns: columns, spacing: 20) {
                ForEach(posts, id: \.self) { item in
                    NormalView(title: item.title,
                               excert: item.excert,
                               thumbnail: item.thumbnail)
                }
            }
            .padding(.horizontal)
        }
    }
}
struct Posts_Previews: PreviewProvider {
    static var previews: some View {
        ListOfPosts(API().getPosts())
    }
}

After updating the Preview, this is the current error message:
Code Block
LoadingError: failed to load library at path "(...)/Products/Debug-iphonesimulator/PackageFrameworks/Posts.framework/Posts
Reason: image not found)




I also have this issue.

In a similar configuration I've seen the error "MessageSendingError: Connection interrupted" (filed FB7872575 with diagnostics) and "Failed to connect to <pid>" (filed FB7872591 with diagnostics).

I've had mixed results creating a random iOS app that depends on the package and doing previews on that. Currently it's the only workaround I'm aware of, and it's not terribly reliable.
Having the same problem. Unless we embed packages directly to the framework that has the code which is being previewed, then previews can't find the package dependencies. Super annoying!