Xcode Previews don't work with Package that includes a .binaryTarget

I am working on rebuilding our large app's dependency graph to use Swift Package Manager, replacing CocoaPods. I have it nearly done and am quite happy with the progress so far. However, I've run into a blocker with Xcode's SwiftUI Previews not working, seemingly due to several .binaryTarget dependencies.

I've boiled this down to a simple reproducible case, with this Package.swift:

Code Block
let package = Package(
name: "MyLibrary",
platforms: [.iOS(.v13)],
products: [.library(name: "MyLibrary", targets: ["MyLibrary"]),],
dependencies: [],
targets: [
.target(name: "MyLibrary", dependencies: ["NewRelic"]),
.binaryTarget(name: "NewRelic",
url: "https://download.newrelic.com/ios_agent/NewRelic_XCFramework_Agent_7.3.0.zip",
checksum: "daaaff7897246e4baddb1b8131a79268de3b6889a48182b4fbdabe5b926d08f4"),
]
)

Here I am using NewRelic as a common 3rd party dependency that vends an .xcframework.zip-- but this is not specific to NewRelic and can be reproduced with many (any?) similar examples.

This package builds a library successfully and works as expected when included in an app.

But when I try to use SwiftUI previews on a trivial view, e.g.:

Code Block
struct MyLibraryView: View {
var body: some View {
VStack {
Text(verbatim: "Hello World, I'm MyLibraryView")
}
}
}
struct MyLibrary_Previews: PreviewProvider {
static var previews: some View {
return MyLibraryView()
}
}


I end up with this Unexpected duplicate tasks build failure, due to the build system seemingly trying to process the XCFramework twice:

Code Block
note: Using new build system
note: Building targets in parallel
note: Planning build
note: Using build description from memory
note: Using build description 'e8ea061fcc823688d24edc3f230a0c7c'
note: Build preparation complete
note: Target dependency graph (3 targets)
MyLibrary in MyLibrary
MyLibrary in MyLibrary, depends on:
MyLibrary in MyLibrary (explicit)
MyLibrary in MyLibrary, depends on:
MyLibrary in MyLibrary (explicit)
error: Unexpected duplicate tasks:
1) Command: ProcessXCFramework /Users/mthole/Library/Developer/Xcode/DerivedData/DuplicateTasksOnPreview-bkclfnyhkfofwaaulhpstpdjggqb/SourcePackages/artifacts/DuplicateTasksOnPreview/NewRelic.xcframework ios simulator
2) Command: ProcessXCFramework /Users/mthole/Library/Developer/Xcode/DerivedData/DuplicateTasksOnPreview-bkclfnyhkfofwaaulhpstpdjggqb/SourcePackages/artifacts/DuplicateTasksOnPreview/NewRelic.xcframework ios simulator


I've posted the reproduction case on GitHub here. Tested with Xcode 12.5 Beta 3.

https://github.com/mthole/DuplicateTasksOnPreview
With Xcode 12.5 RC1 released this week, the build now succeeds, but the we get a failing error when trying to open the Preview:

Code Block
RemoteHumanReadableError: Failed to update preview.
Error encountered when sending 'previewInstances' message to agent.
==================================
| RemoteHumanReadableError
|  
| LoadingError: failed to load library at path "/Users/mthole/Library/Developer/Xcode/DerivedData/DuplicateTasksOnPreview-bkclfnyhkfofwaaulhpstpdjggqb/Build/Intermediates.noindex/Previews/MyLibrary/Products/Debug-iphonesimulator/PackageFrameworks/MyLibrary_-71ED4B7C0CDEC55C_PackageProduct.framework/MyLibrary_-71ED4B7C0CDEC55C_PackageProduct": Optional(dlopen(/Users/mthole/Library/Developer/Xcode/DerivedData/DuplicateTasksOnPreview-bkclfnyhkfofwaaulhpstpdjggqb/Build/Intermediates.noindex/Previews/MyLibrary/Products/Debug-iphonesimulator/PackageFrameworks/MyLibrary_-71ED4B7C0CDEC55C_PackageProduct.framework/MyLibrary_-71ED4B7C0CDEC55C_PackageProduct, 0): Library not loaded: @rpath/NewRelic.framework/NewRelic
|  Referenced from: /Users/mthole/Library/Developer/Xcode/DerivedData/DuplicateTasksOnPreview-bkclfnyhkfofwaaulhpstpdjggqb/Build/Intermediates.noindex/Previews/MyLibrary/Products/Debug-iphonesimulator/PackageFrameworks/MyLibrary_-71ED4B7C0CDEC55C_PackageProduct.framework/MyLibrary_-71ED4B7C0CDEC55C_PackageProduct
|  Reason: image not found)


Hi,

Apologies it took us this long to get back to you.

I'm sorry to hear you are having problems getting previews working. Best next step will be to file a feedback with diagnostics. We will need the diagnostics Xcode Previews generates in order to make sure we understand the error the previews system is encountering.

  1. When you get an error in Xcode Previews, an error banner appears in the canvas
  2. Click the "Diagnostics" button in that banner
  3. In the sheet that appears, click "Generate Report" in the bottom left of the sheet
  4. Attach (or make from the folder) the resulting zip file to the bug (will be named something like previews-diagnostics-0123456789.zip)

I encountered same exact problem.

RemoteHumanReadableError: Failed to update preview.

Error encountered when sending 'previewInstances' message to agent.

==================================

| RemoteHumanReadableError
|  
| LoadingError: failed to load library at path "/Users/won/Library/Developer/Xcode/DerivedData/Manhwakyung-ios-galpxdndpxrpxwescmggerjjhokl/Build/Intermediates.noindex/Previews/MHKUI/Products/Debug-iphonesimulator/PackageFrameworks/MHKUI_CB8D5E841E3_PackageProduct.framework/MHKUI_CB8D5E841E3_PackageProduct": Optional(dlopen(/Users/won/Library/Developer/Xcode/DerivedData/Manhwakyung-ios-galpxdndpxrpxwescmggerjjhokl/Build/Intermediates.noindex/Previews/MHKUI/Products/Debug-iphonesimulator/PackageFrameworks/MHKUI_CB8D5E841E3_PackageProduct.framework/MHKUI_CB8D5E841E3_PackageProduct, 0): Library not loaded: @rpath/SnapKit.framework/SnapKit
|  Referenced from: /Users/won/Library/Developer/Xcode/DerivedData/Manhwakyung-ios-galpxdndpxrpxwescmggerjjhokl/Build/Intermediates.noindex/Previews/MHKUI/Products/Debug-iphonesimulator/PackageFrameworks/MHKUI_CB8D5E841E3_PackageProduct.framework/MHKUI_CB8D5E841E3_PackageProduct
|  Reason: image not found)
RemoteHumanReadableError: Failed to update preview.



Error encountered when sending 'previewInstances' message to agent.



==================================



|  RemoteHumanReadableError

|  

|  LoadingError: failed to load library at path "/Users/jcospina/Library/Developer/Xcode/DerivedData/MML-csmxzsfovwegmkeoxobdomlyhxak/Build/Intermediates.noindex/Previews/SPMCore/Products/Debug-iphonesimulator/PackageFrameworks/SPMCore_3955A1BDE21E4280_PackageProduct.framework/SPMCore_3955A1BDE21E4280_PackageProduct": Optional(dlopen(/Users/jcospina/Library/Developer/Xcode/DerivedData/MML-csmxzsfovwegmkeoxobdomlyhxak/Build/Intermediates.noindex/Previews/SPMCore/Products/Debug-iphonesimulator/PackageFrameworks/SPMCore_3955A1BDE21E4280_PackageProduct.framework/SPMCore_3955A1BDE21E4280_PackageProduct, 0): Library not loaded: @rpath/Anchorage.framework/Anchorage

|    Referenced from: /Users/jcospina/Library/Developer/Xcode/DerivedData/MML-csmxzsfovwegmkeoxobdomlyhxak/Build/Intermediates.noindex/Previews/SPMCore/Products/Debug-iphonesimulator/PackageFrameworks/SPMCore_3955A1BDE21E4280_PackageProduct.framework/SPMCore_3955A1BDE21E4280_PackageProduct

|    Reason: image not found)

Same issue here. (Xcode 12.5)

Xcode Previews don't work with Package that includes a .binaryTarget
 
 
Q