Open jeffhodsdon opened 7 months ago
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 bazel run :AltaMain
and not have bundle accessor issues?
@mattrobmattrob yes, using https://github.com/cgrindel/rules_swift_package_manager. PhoneNumberKit
does run on the simulator but not the preview.
Hmm, it seems odd that it's crashing in the generated resource_bundle_accessor
from rules_swift_package_manager when it has its own bundle accessor (PhoneNumberKit/Bundle+Resources.swift
). Something similar WRT SwiftUI previews is even mentioned:
Have you seen this before, @cgrindel? Can the generation of that resource_bundle_accessor
be disabled, @jeffhodsdon?
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 Bundle.main
(or equivalent) is the path to the resources but when dynamically linking/in previews this is not the case.
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 rules_swift_package_manager
to be something like this?
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 —