Xcode 12 - SwiftUI preview doesn't work on Swift Package when have another Swift Package as dependencies - 'previewInstances' message to agent
Workaround for both iOS and macOS (not tested with Catalyst)
public let imageBundle = Bundle.myModuleprivate class CurrentBundleFinder {}extension Foundation.Bundle { static var myModule: Bundle = { /* The name of your local package, prepended by "LocalPackages_" for iOS and "PackageName_" for macOS. You may have same PackageName and TargetName*/ let bundleNameIOS = "LocalPackages_TargetName" let bundleNameMacOs = "PackageName_TargetName" let candidates = [ /* Bundle should be present here when the package is linked into an App. */ Bundle.main.resourceURL, /* Bundle should be present here when the package is linked into a framework. */ Bundle(for: CurrentBundleFinder.self).resourceURL, /* For command-line tools. */ Bundle.main.bundleURL, /* Bundle should be present here when running previews from a different package (this is the path to "…/Debug-iphonesimulator/"). */ Bundle(for: CurrentBundleFinder.self).resourceURL?.deletingLastPathComponent().deletingLastPathComponent().deletingLastPathComponent(), Bundle(for: CurrentBundleFinder.self).resourceURL?.deletingLastPathComponent().deletingLastPathComponent(), ] for candidate in candidates { let bundlePathiOS = candidate?.appendingPathComponent(bundleNameIOS + ".bundle") let bundlePathMacOS = candidate?.appendingPathComponent(bundleNameMacOs + ".bundle") if let bundle = bundlePathiOS.flatMap(Bundle.init(url:)) { return bundle } else if let bundle = bundlePathMacOS.flatMap(Bundle.init(url:)) { return bundle } } fatalError("unable to find bundle") }()}
Update 1:
Sorry my last post was not accurate. This is a bug in Xcode with no work around. Just submitted to Apple a bug report (FB8880328). Also, posted a details write up with example code w/ repro steps here. Direct link to GitHub project: https://github.com/ryanholden8/SwiftUI-Preview-Failing-Test-Project
Old Post:
Got this exact error doing the same thing, putting colors in a separate package. This post helped me get to the bottom of it. I deleted the default class that is generated in the colors package. However, I did not delete the unit test that was based on that default class.
In short: Delete the auto-generated unit test in your Common
package. Or make sure all the unit tests pass.