Skip to content

Commit 5bcbf39

Browse files
authored
Fix crash bundle (#1448)
* create availableModule to avoid crash on Bundle loading * remove unnecessary line
1 parent fd48b7e commit 5bcbf39

File tree

1 file changed

+46
-1
lines changed

1 file changed

+46
-1
lines changed

Sources/XcodeGenKit/SettingsBuilder.swift

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@ extension SettingsPresetFile {
249249
symlink.parent() + relativePath,
250250
] + possibleSettingsPaths
251251
}
252-
if let moduleResourcePath = Bundle.module.path(forResource: "SettingPresets", ofType: nil) {
252+
if let moduleResourcePath = Bundle.availableModule?.path(forResource: "SettingPresets", ofType: nil) {
253253
possibleSettingsPaths.append(Path(moduleResourcePath) + "\(path).yml")
254254
}
255255

@@ -272,3 +272,48 @@ extension SettingsPresetFile {
272272
return buildSettings
273273
}
274274
}
275+
276+
private class BundleFinder {}
277+
278+
/// The default SPM generated `Bundle.module` crashes on runtime if there is no .bundle file.
279+
/// Below implementation modified from generated `Bundle.module` code which call `fatalError` if .bundle file not found.
280+
private extension Bundle {
281+
/// Returns the resource bundle associated with the current Swift module.
282+
static let availableModule: Bundle? = {
283+
let bundleName = "XcodeGen_XcodeGenKit"
284+
285+
let overrides: [URL]
286+
#if DEBUG
287+
// The 'PACKAGE_RESOURCE_BUNDLE_PATH' name is preferred since the expected value is a path. The
288+
// check for 'PACKAGE_RESOURCE_BUNDLE_URL' will be removed when all clients have switched over.
289+
// This removal is tracked by rdar://107766372.
290+
if let override = ProcessInfo.processInfo.environment["PACKAGE_RESOURCE_BUNDLE_PATH"]
291+
?? ProcessInfo.processInfo.environment["PACKAGE_RESOURCE_BUNDLE_URL"] {
292+
overrides = [URL(fileURLWithPath: override)]
293+
} else {
294+
overrides = []
295+
}
296+
#else
297+
overrides = []
298+
#endif
299+
300+
let candidates = overrides + [
301+
// Bundle should be present here when the package is linked into an App.
302+
Bundle.main.resourceURL,
303+
304+
// Bundle should be present here when the package is linked into a framework.
305+
Bundle(for: BundleFinder.self).resourceURL,
306+
307+
// For command-line tools.
308+
Bundle.main.bundleURL,
309+
]
310+
311+
for candidate in candidates {
312+
let bundlePath = candidate?.appendingPathComponent(bundleName + ".bundle")
313+
if let bundle = bundlePath.flatMap(Bundle.init(url:)) {
314+
return bundle
315+
}
316+
}
317+
return nil
318+
}()
319+
}

0 commit comments

Comments
 (0)