@@ -249,7 +249,7 @@ extension SettingsPresetFile {
249
249
symlink. parent ( ) + relativePath,
250
250
] + possibleSettingsPaths
251
251
}
252
- if let moduleResourcePath = Bundle . module . path ( forResource: " SettingPresets " , ofType: nil ) {
252
+ if let moduleResourcePath = Bundle . availableModule ? . path ( forResource: " SettingPresets " , ofType: nil ) {
253
253
possibleSettingsPaths. append ( Path ( moduleResourcePath) + " \( path) .yml " )
254
254
}
255
255
@@ -272,3 +272,48 @@ extension SettingsPresetFile {
272
272
return buildSettings
273
273
}
274
274
}
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