swiftxcodebundleswift-package-manager

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

  • 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!