Bundling resources with a Swift package : Bundle.module not available

I am building a SwiftPackage app that includes a server for a WebSocket. Everything was going fine until I tried to read data from a .txt file to build a dictionnary.

The file name is od9.txt and is stored in the Sources/Data/Resources folder.

The snippet to read the file starts with :

guard let url = Bundle.main.url(forResource: "ods9", withExtension: "txt") else {
      fatalError("\(FICHIER_MOTS).txt non trouvé")
}

Unfortunately, at runtime the url remains definitively nil.

In the doc it tells you have to explicitly declare your file as a resource in the package manifest. Here is what I added to the manifest.

targets: [
        .target(
            name: "Data",
            resources: [
                .process("Resources/ods9.txt")]
        ),
        ...
        ],

In the doc they also say : Always use Bundle.module when you access resources.

And that’s where the problem lie : I tried everything possible to replace Bundle.main by Bundle.module but did not make it. Apparently module is not an option for my compiler.

Does someone know what I am doing wrong here ? Note the above snippet was copied from a Swift project where it works like a charm.

If it can help, here the complete Package manifest:

// swift-tools-version:6.0

import PackageDescription

let package = Package(
    name: "server",
    platforms: [
       .macOS(.v13)
    ],
    dependencies: [
        // 💧 A server-side Swift web framework.
        .package(url: "https://github.com/vapor/vapor.git", from: "4.115.0"),
        // 🔵 Non-blocking, event-driven networking for Swift. Used for custom executors
        .package(url: "https://github.com/apple/swift-nio.git", from: "2.65.0"),
    ],
    targets: [
        .target(
            name: "Data",
            resources: [
                .process("Resources/ods9.txt")]
        ),
        .executableTarget(
            name: "server",
            dependencies: [
                .product(name: "Vapor", package: "vapor"),
                .product(name: "NIOCore", package: "swift-nio"),
                .product(name: "NIOPosix", package: "swift-nio"),
            ],
            swiftSettings: swiftSettings
        ),
        .testTarget(
            name: "serverTests",
            dependencies: [
                .target(name: "server"),
                .product(name: "VaporTesting", package: "vapor"),
            ],
            swiftSettings: swiftSettings
        )
    ]
)
var swiftSettings: [SwiftSetting] { [
    .enableUpcomingFeature("ExistentialAny"),
] }

And the module where the file is read:

//
//  File.swift
//  hello
//
//  Created by Simon Wintz on 15/01/2026.
//

import Foundation

class Trie {
    var racine = Noeud(lettre: " ", fin: false)

    init() {
        guard let url = Bundle.main.url(forResource: "ods9", withExtension: "txt") else {
            fatalError("ods9.txt non trouvé")
        }

        if let dico = try? String(contentsOf: url, encoding: String.Encoding.utf8 ) {
            let mots = dico.split(separator: "\r\n")
            for mot in mots {
                var curNode = racine
                for lettre in mot.dropLast() {
                    curNode =
                        if let node = curNode.children.first(where: { $0.lettre == lettre }) {
                            node
                        } else {
                            noeud(parent: curNode, lettre: lettre)
                        }
                }
                let lettre = mot.last!
                curNode =
                if let node = curNode.children.first(where: { $0.lettre == lettre }) {
                        node
                    } else {
                        noeud(parent: curNode, lettre: lettre, fin: true)
                    }
            }
        }
    }

    private func noeud(parent: Noeud, lettre: Character, fin: Bool = false) -> Noeud {
        let newNoeud = Noeud(lettre: lettre, fin: false)
        parent.children.append(newNoeud)
        return newNoeud
    }
}

Which target is trying the fetch the resources? I believe bundle.module is local/internal to each module. you can't load a resource from another target without making a shim to do the loading.

Hi Thomasvl,

I suspected the problem might be that I don’t catch exactly the meaning of a target or even a module as these words are not explicitly used in the Xcode project structure. Therefore I tried to place the ressource file ods9.txt in the same folder as the swift files Tries with the structure that fetches the ressource (Sources/server/controllers) but this was immediately rejected by the compiler.

If you had an example with a full project structure and where the ressource file shall be placed that would be great !

"target" == target(...) in the Package.swift (or a testTarget(...))

"module" == the Swift module that exists for each target, i.e. - the sources compiled into that target that then exposes apis for other things to consume.

Bundling resources with a Swift package | Apple Developer Documentation is Apple docs on this, which includes sections that talk about letting your library expose resources to callers (via making public api for that)

Do you mean this doesn't compile: Bundle.module ?

Check this location (change the path accordingly):

Do you have this file? It should be generated automatically when you build the project.
There will be:

extension Foundation.Bundle {
    /// Returns the resource bundle associated with the current Swift module.
    static let module: Bundle = {
        ....
    }
}

That's precisely the doc I follow thoroughly. But the only thing I could not do according to doc is Bundle.module. for me, only Bundle.main passes compilation...and crashes at run time.

You mean the directory Library/Developer/Xcode... . My package name is server. I have two candidates server-dtijockaxzoqmzbipevgasldssao and server-gzyjzjsmjvuotdgjejnakszhoicd but then there is no Debug-Iphonesimulator but just Debug (probably because I am running the server on myMac ?) and then, no Derived source folder.

In the second candidate, there is a Debug-iphoneos but then only App.build, no server.build ...

Yep, ~/Library/Developer/Xcode/DerivedData/...

You are building for mac, so probably "Debug", allow some variations to the path.
If in doubt - delete the entries in there and rebuild the thing, the one that reappears is the one.

And yes, the name there could be different to "server" (IIRC that's the target or library name, it's probably "Data" for one of the inner folders in your case)

ods9.txt does not exist in Sources/Resources/. As soon as it does, Bundle.module will become available. (You probably just forgot to type Data/.)

1 Like

You mean in Sources/Data/Resources/ ? (Following the doc I juste changed the name of the folder MyLibrary to Data. Is there something wrong here ?
Because in the folders, the ods9.txt appears :


Or in the finder:

Whart do you mean the file does not exist ?

By the way, I tried to place Data in Sources/Ressources as you suggest and got this error:

Yeah ! Finally I got it. My mistake was to misunderstand what the Target was. My target is server within the Sources folder and it is declared as .executableTarget( name: "server" ...) in the manifest.
Therefore I moved my folder Data/ods9.txt to the server folder and added the ressource to the existing target (instead of creating a new target named Data).

        .executableTarget(
            name: "server",
            dependencies: [
                .product(name: "Vapor", package: "vapor"),
                .product(name: "NIOCore", package: "swift-nio"),
                .product(name: "NIOPosix", package: "swift-nio"),
            ],
            resources: [
                .process("Data/ods9.txt")],
            swiftSettings: swiftSettings
        ),

And so it works !
Thanks guys for your guidance.

1 Like