61

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.

Mojtaba Hosseini
  • 95,414
  • 31
  • 268
  • 278
  • Where is this resource_bundle_accessor file located? I tried everything suggested in the answers and it will just not work. Might as well create it myself instead then. – koen Oct 17 '20 at 17:50
  • 2
    No you must NOT create it yourself!!! It is autogenerated and that is the main point of the question. – Mojtaba Hosseini Oct 17 '20 at 18:24
  • I actually posted a related question here this morning: https://stackoverflow.com/questions/64403646/swift-package-manager-with-resources-compile-errors - maybe you can spot an error in my `Package.swift` file? – koen Oct 17 '20 at 18:56

8 Answers8

107

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 contains Resources inside the target's Directory!

Example

AND❗️❗️

The Resources MUST contain something and MUST NOT be an empty directory

AND❗️❗️❗️

Don't forget to build (cmd+b) the code to make the .module be created!

Mojtaba Hosseini
  • 95,414
  • 31
  • 268
  • 278
  • 2
    Future readers, it seems that these solutions don't work yet for Swift 5.5 and executable targets: cf. [this comment on a bug report](https://bugs.swift.org/browse/SR-13773?focusedCommentId=64312&page=com.atlassian.jira.plugin.system.issuetabpanels%3Acomment-tabpanel#comment-64312) and my own experience – Mick F Dec 11 '21 at 17:35
  • 3
    It worked for me, the Resources folder must be not-empty! – nayooti Aug 23 '22 at 09:48
  • I found I needed to restart Xcode for it to recognise the directory was there. – huwr Feb 17 '23 at 05:44
  • 1
    Oh the Second point needs special clarification (although the pic depicts it so nicely). The **Resources** folder needs to be inside Sources->Package Folder Name. – rptwsthi Jun 12 '23 at 13:56
33

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 share@3x.png 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.

Jano
  • 62,815
  • 21
  • 164
  • 192
7

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

blackjacx
  • 9,011
  • 7
  • 45
  • 56
  • 1
    Don't make the mistake I did: be sure you have `// swift-tools-version:5.3` or greater at the top of your package file, instead of, say, `swift-tools-version:5.1` because it was copied from another package as a template. If not 5.3 or above, `Bundle` will not be generated and the error messages are not super clear. – mm2001 Sep 18 '20 at 02:37
3

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:

  1. You don't have any resources specified in Package.swift. Fix like this:
.target(
    name: "MyLibrary",
    dependencies: [
        /* your dependencies */
    ],
    resources: [
        .copy("JsonData"),
        .process("Assets.xcassets"),
    ]
),
  1. The specified paths don't exist or are empty.

    Fix by checking that you've actually placed your resources inside the Sources/MyLibrary directory. A common mistake is to place them directly to Sources or to a different targets subfolder.

Jeehut
  • 20,202
  • 8
  • 59
  • 80
1

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")
highFly
  • 95
  • 1
  • 5
1
  1. Make sure that the Resources folder exists and that it has files
  2. Bundle.module is autogenerated by XCode when the Resources folder is not empty. However it is internal
  3. Add @testable import YourPackage to make makes internal files in your package sources available to tests.
t.ios
  • 1,024
  • 12
  • 18
1

If you have your text.json in the Resources folder, make sure that you have .process("Resources/text.json") path in your Package.swift

Tikhonov Aleksandr
  • 13,945
  • 6
  • 39
  • 53
0

In my case, I've tried all the solutions and nothing helped until I've restarted Xcode.

Done on Xcode 14.0.0

edu.art
  • 11