Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 241
Mojtaba Hosseini Avatar asked Aug 03 '20 21:08

Mojtaba Hosseini


Video Answer


2 Answers

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!

Example

AND❗️❗️

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

like image 114
Mojtaba Hosseini Avatar answered Oct 04 '22 11:10

Mojtaba Hosseini


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.

like image 36
Jano Avatar answered Oct 04 '22 11:10

Jano