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
}
}




