-
Notifications
You must be signed in to change notification settings - Fork 86
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
SwiftUI preview view's spm dependency can not locate bundle #2977
Comments
Are you using https://github.com/cgrindel/rules_swift_package_manager to create these targets? Does it run on a simulator outside of Xcode using |
@mattrobmattrob yes, using https://github.com/cgrindel/rules_swift_package_manager. |
Hmm, it seems odd that it's crashing in the generated Have you seen this before, @cgrindel? Can the generation of that |
The accessors have different names. I presume that PhoneNumberKit is using their custom accessor internally. What is it about SwiftUI preview that would just call the generated accessor? 🤔 Since all Swift packages with resources have this accessor generated, do all Swift packages fail in SwiftUI preview? |
FWIW in our Cash App code we ran into dynamic framework paths being the root cause of crashes when loading bundles. A lot of code assumes |
This is what we updated our resource bundle generator stencil to look like: import Foundation
extension Bundle {
private final class {{param.moduleName}}Sentinel {}
@objc({{param.lowerCamelCaseModuleName}}ResourcesBundle)
{{param.accessLevel}} static var {{param.lowerCamelCaseModuleName}}Resources: Bundle {
let container = Bundle(for: {{param.moduleName}}Sentinel.self)
let resourceBundleName = "{{param.moduleName}}Resources"
if let resources = container.url(forResource: resourceBundleName, withExtension: "bundle") {
return Bundle(url: resources)!
}
#if targetEnvironment(simulator)
// During Xcode Previews the framework is run from an XCPreviewAgent.
// The resource and framework live outside of this apps bundle.
// Xcode will set `DYLD_FRAMEWORK_PATH` to the path of `BUILT_PRODUCTS_DIR` for the framework.
// This allows us to get the path to the framework and load the resources from there.
if ProcessInfo.processInfo.environment["XCODE_RUNNING_FOR_PREVIEWS"] == "1" {
let dyldPath = ProcessInfo.processInfo.environment["DYLD_FRAMEWORK_PATH"].map(URL.init(fileURLWithPath:))
guard
let dyldBaseName = dyldPath?.lastPathComponent,
let frameworkURL = dyldPath?.appendingPathComponent("\(dyldBaseName).framework"),
let framework = Bundle(url: frameworkURL),
let resourcesURL = framework.url(forResource: resourceBundleName, withExtension: "bundle"),
let resources = Bundle(url: resourcesURL)
else {
fatalError("Unable to load \(resourceBundleName) for Xcode Previews")
}
return resources
}
#endif
fatalError("Unable to load \(resourceBundleName)")
}
} |
@luispadron Should we update the accessors that we generate in |
I wouldn't want to differ from the bundle accessor that SPM generates at all, if possible. Are you all generating that to overcome this SPM related issue or just making Swift Previews work with your bundle accessor, @luispadron? |
Description
Hello! Thank all for this project. It is the best and helps the ecosystem very much.
I'm running into an issue getting a SwiftUI preview working. Particularly one that relies on a third party decency that has bundled data — PhoneNumberKit
I believe I'm getting a crash on the generated
Bundle.module
extension. Info from an Apple Tool Dev on.module
I've looked into the working directory of the swift ui preview and do see the bundle.
The search method PhonenumberKit looks for the bundle —
https://github.com/marmelroy/PhoneNumberKit/blob/3.4.5/PhoneNumberKit/Bundle%2BResources.swift
Reproduction steps
Rules —
Expected behavior
Swift UI preview to work.
rules_xcodeproj version
1.18.0
Xcode version
15.3
Bazel version
7.1.0
rules_apple version
3.3.0
rules_swift version
1.17.0
Additional information
Test SwiftUI View —
The text was updated successfully, but these errors were encountered: