Generating resource_bundle_accessor, Type 'Bundle' has no member 'module'
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` deson'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!
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/ResourcesTests/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 share@3x.png inside the bundle?find ~/Library/Developer/Xcode/DerivedData* -iname Assets.car -exec xcrun --sdk iphoneos assetutil --info {} \; | grep -E "share.*png"
If everything fails and you suspect a bug check the bug database for SPM.
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.3import 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 👍