'Generating resource_bundle_accessor, Type 'Bundle' has no member 'module'
Some times Xcode can not determine the module parameter in the Bundle.
Type 'Bundle' has no member 'module'
My investigations show that SPM generates an extension on the module (some times) for this property automatically in a file called resource_bundle_accessor like:
import class Foundation.Bundle
private class BundleFinder {}
extension Foundation.Bundle {
/// Returns the resource bundle associated with the current Swift module.
static var module: Bundle = {
let bundleName = "ABUIKit_ABStyleKit"
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: BundleFinder.self).resourceURL,
// For command-line tools.
Bundle.main.bundleURL,
]
for candidate in candidates {
let bundlePath = candidate?.appendingPathComponent(bundleName + ".bundle")
if let bundle = bundlePath.flatMap(Bundle.init(url:)) {
return bundle
}
}
fatalError("unable to find bundle named ABUIKit_ABStyleKit")
}()
}
But sometimes it won't. Why is that and how can I make it work automatically again (Without the need to manually implement that.)
Both situations happen on Xcode 12 beta.3
Update
Sometimes it shows:
'module' is inaccessible due to 'internal' protection level
And it's not showing the file at all.
Solution 1:[1]
SPM generates the resource_bundle_accessor only if the corresponding target contains resources as the argument like:
.target(
name: "ChenzookKit",
dependencies: ["Apollo"],
resources: [.process("Resources")] // <- `copy` or `process` doesn't really matter
),
Also, note that it should be a valid resource path.
AND??
The project MUST actaully contains Resources inside the target's Directory!
AND????
Don't forget to build (cmd+b) the code to make the .module be created!
Solution 2:[2]
If you see errors like this:
Error: found 1 file(s) which are unhandled; explicitly declare them as resources or exclude from the target
Type 'Bundle' has no member 'module'
Then review the following five conditions:
1. Check that the first line of your Package.swift declares the use of swift-tools-version:5.3 or a later version.
// swift-tools-version:5.3
2. Check the Resource folder is under the target folder. For instance,
Sources/MyTarget/Resources
Tests/MyTargetTests/Resources
3. Check that you already added at least one resource. For instance,
Tests/MyTargetTests/Resources/paginatedLabels.json
4. Check that you open the package by opening the file Package.swift with Xcode.
5. Check that the Package.swift file declares a resources element, like this:
.testTarget(
name: "MyTargetTests",
dependencies: ["MyTarget"],
resources: [
.process("Resources")
]
),
At this point, the compiler synthesized a file resource_bundle_accessor.swift with this content:
extension Foundation.Bundle {
static var module: Bundle = {
...
To check that the file was indeed synthesized:
# is the bundle being synthesized when you build?
find ~/Library/Developer/Xcode/DerivedData* -iname resource_bundle_accessor.swift
To load resources from the package bundle use Bundle.module, e.g.
UIImage(named: "share", in: Bundle.module, compatibleWith: nil)
To find the path to the package bundle directory:
MODULE=MyModuleName && find -E ~/Library/Developer/Xcode/DerivedData -regex ".*$MODULE_$MODULE.bundle"
To check that the package bundle contains a particular resource, e.g. an image:
# I’m building for iPhone 12 (@3x). Is [email protected] inside the bundle?
find ~/Library/Developer/Xcode/DerivedData* -iname Assets.car -exec xcrun --sdk iphoneos assetutil --info {} \; | grep -E "share.*png"
Here is an example that uses custom directories:
targets: [
.target(
name: "Kit",
dependencies: [],
path: "sources/main",
resources: [
.process("resources")
]
),
where directories are:
Kit/
??? sources/
? ??? main/
? ??? SourceFile.swift
? ??? resources/
? ??? file.json
??? Package.swift
If everything fails and you suspect a bug check the bug database for SPM.
Solution 3:[3]
Okay I found a solution, so Swift actually generates the Bundle.module file ?
The documentation explicitly states that you have to put your Resources under the folder <project_root>/Sources/<MyTarget>/ since SPM scopes resources by target. The target definition looks then like this for my repo SHSearchBar (compare file structure on Github):
// swift-tools-version:5.3
import PackageDescription
targets: [
.target(
name: "SHSearchBar",
resources: [.copy("Resources")]
)
]
Target Folder: <project_root>/Sources/SHSearchBar
Resource Folder: <project_root>/Sources/SHSearchBar/Resources
By the way, to build an iOS package from command line you can use this command uses the iOS 14 SDK:
swift build -Xswiftc "-sdk" -Xswiftc "\`xcrun --sdk iphonesimulator --show-sdk-path\`" -Xswiftc "-target" -Xswiftc "x86_64-apple-ios14.0-simulator"
To make my little post here complete I also want to mention that a package using the Bundle.module approach can be integrated in apps that run on iOS <14 too since the generated extension does not contain any new API ?
Solution 4:[4]
Bundle.module will only be generated by SwiftPM if the resources in the Package.swift file is not empty and the specified resources actually exist.
So there are two possible reasons why it might not work for you:
- You don't have any
resourcesspecified inPackage.swift. Fix like this:
.target(
name: "MyLibrary",
dependencies: [
/* your dependencies */
],
resources: [
.copy("JsonData"),
.process("Assets.xcassets"),
]
),
The specified paths don't exist or are empty.
Fix by checking that you've actually placed your resources inside the
Sources/MyLibrarydirectory. A common mistake is to place them directly toSourcesor to a different targets subfolder.
Solution 5:[5]
You can also expose the bundle by creating a class with static let:
public class YourPackageBundle {
public static let yourPackageBundle = Bundle.module
}
Then you can access it in your public classes and use it:
public init(backgroundImage: Image? = nil,
logoImage: Image? = nil,
userIconImage: Image? = nil,
userTextFieldPlaceholder:String = NSLocalizedString("global_username", bundle: YourPackageBundle.yourPackageBundle, comment: "User placeholder text")
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | l --marc l |
| Solution 2 | |
| Solution 3 | |
| Solution 4 | Jeehut |
| Solution 5 | highFly |

