SwiftUI Previews in Swift Packages

I can't seem to get my SwiftUI preview to work in a Swift Package created with the "Multiplatform Swift Package" Xcode 12 template. Does this feature require macOS 11+? I'm currently on Catalina with the Xcode 12 beta.

Here's what I get from the SwiftUI Preview Diagnostics on macOS:
Code Block
HumanReadableNSError: connectToPreviewHost: Failed to connect to 2247: Error Domain=com.apple.dt.ProcessAttachUtilities Code=0 "Failed to get task for pid 2247: 0" UserInfo={NSLocalizedDescription=Failed to get task for pid 2247: 0}
com.apple.dt.xcodepreviews.service (17):


and on iOS:
Code Block
'SwiftUIView_Previews' is not a member type of 'NoiseKit'
----------------------------------------
CompileDylibError: Failed to build SwiftUIView.swift
Compiling failed: 'SwiftUIView_Previews' is not a member type of 'NoiseKit'
/Users/rudedogg/Library/Developer/Xcode/DerivedData/NoiseKit-gefbuwplfdnsqkghveaurldpgavw/Build/Intermediates.noindex/Previews/NoiseKit/Intermediates.noindex/NoiseKit.build/Debug-appletvsimulator/NoiseKit.build/Objects-normal/x86_64/SwiftUIView.2.preview-thunk.swift:22:43: error: 'SwiftUIView_Previews' is not a member type of 'NoiseKit'
typealias SwiftUIView_Previews = NoiseKit.SwiftUIView_Previews
                                 ~~~~~~~~ ^
/Volumes/Files/work/packages/NoiseKit/Sources/NoiseKit/NoiseKit.swift:1:8: note: 'NoiseKit' declared here
struct NoiseKit {
       ^
/Users/rudedogg/Library/Developer/Xcode/DerivedData/NoiseKit-gefbuwplfdnsqkghveaurldpgavw/Build/Intermediates.noindex/Previews/NoiseKit/Intermediates.noindex/NoiseKit.build/Debug-appletvsimulator/NoiseKit.build/Objects-normal/x86_64/SwiftUIView.2.preview-thunk.swift:21:34: error: 'SwiftUIView' is not a member type of 'NoiseKit'
typealias SwiftUIView = NoiseKit.SwiftUIView
                        ~~~~~~~~ ^
/Volumes/Files/work/packages/NoiseKit/Sources/NoiseKit/NoiseKit.swift:1:8: note: 'NoiseKit' declared here
struct NoiseKit {
       ^

Accepted Reply

This was fixed in Xcode 12 beta 5.

Replies

After looking more closely at https://developer.apple.com/forums/thread/651547 I realized I didn't rename or delete the default struct {Package Name} created by Xcode. After doing that, previews work on iOS/tvOS. Unfortunately they are still broken on macOS. I also tested macOS 11, with the same results.

The error is still the same, something about how it can't attach to the process:

Code Block
HumanReadableNSError: connectToPreviewHost: Failed to connect to 4083: Error Domain=com.apple.dt.ProcessAttachUtilities Code=0 "Failed to get task for pid 4083: 0" UserInfo={NSLocalizedDescription=Failed to get task for pid 4083: 0}
com.apple.dt.xcodepreviews.service (17):

I'm still having this issue with macOS SwiftUI previews in Swift Packages. Running Xcode 12 Beta 2, macOS 11 Beta 2.
Same here on macOS Big Sur beta 4 with Xcode 12 beta 4.
This was fixed in Xcode 12 beta 5.
With beta 6 my package previews stopped with "Could not find host for previews", very annoying. I created a sample project using that template with a view that just has a text field in it, and that will only work if the testTarget part of Package.swift is commented out. If that is in there then Xcode won't build the test target with the proper architecture.
With Xcode 12b6 the previews will only work if there is exactly one product and one target for that product. Test targets give link errors, another block in those sections cause failure. Any more targets, even if unused, cause failures in finding the host for the preview.