Swift 5.3 SPM Resources in tests uses wrong bundle path?


I've downloaded 5.3-DEVELOPMENT-SNAPSHOT-2020-05-19-a to try out the new resources feature of the package manager. swift --version outputs: Apple Swift version 5.3-dev (LLVM 38f04f051e, Swift 587da7ce60) Target: x86_64-apple-darwin19.5.0. swift build --version: Swift Package Manager - Swift 5.3.0

I run into an issue where I cannot access resource files bundled in the test target. I am not sure if the root cause is that the feature is not fully finished yet, that it's a bug or I am just using it wrong.

My Package.swift
// swift-tools-version:5.3

import PackageDescription

let package = Package(
    name: "SwiftBeanCountParser",
    products: [
            name: "SwiftBeanCountParser",
            targets: ["SwiftBeanCountParser"]),
    dependencies: [
            url: "https://github.com/Nef10/SwiftBeanCountModel.git",
    targets: [
            name: "SwiftBeanCountParser",
            dependencies: ["SwiftBeanCountModel"]),
            name: "SwiftBeanCountParserTests",
            dependencies: ["SwiftBeanCountParser"],
            resources: [

When I run swift test it runs some tests till it fails with:

Fatal error: could not load resource bundle: /Applications/Xcode.app/Contents/Developer/usr/bin/SwiftBeanCountParser_SwiftBeanCountParserTests.bundle: file /Users/Steffen/Projects/SwiftBeanCountParser/.build/x86_64-apple-macosx/debug/SwiftBeanCountParserTests.build/DerivedSources/resource_bundle_accessor.swift, line 7 Exited with signal code 4

My code for loading the resource looks like this: let url = NSURL.fileURL(withPath: Bundle.module.path(forResource: "Minimal", ofType: "beancount")!), so I am using Bundle.module like introduced by the resource proposal.

The resource_bundle_accessor where the problem occurs does this does this: let bundlePath = Bundle.main.bundlePath + "/" + "SwiftBeanCountParser_SwiftBeanCountParserTests.bundle" So it seems that Bundle.main.bundlePath is set to /Applications/Xcode.app/Contents/Developer/usr/bin/. Acording to Bundle.main.path pointing to odd path this could be correct as this directory contains the xctest executable.

The folder which contains my code is /Users/Steffen/Projects/SwiftBeanCountParser. If I check in the .build folder, I can see that /Users/Steffen/Projects/SwiftBeanCountParser/.build/x86_64-apple-macosx/debug/SwiftBeanCountParser_SwiftBeanCountParserTests.bundle contains all my resource files.

Am I doing anything wrong? Is this feature not fully finished yet? Or is this a bug I should report (I couldn't find any similar bug or forums post)


Sounds like it to me.


I created a minimal reproducible sample project at https://github.com/Nef10/SPMResourcesInTest and filed the bug: https://bugs.swift.org/browse/SR-12912

Does any one found a solution to this problem?

If you are looking for a workaround until the bug is fixed, you can do one of two things:

  • Put the resources in an otherwise empty normal target, along with enough Swift code to pass them on in a public interface. Since no product uses the target, but rather only tests, it won’t be included when your package is used as a dependency.
  • Instead of the new resource API, use #file/#filePath along and add/remove path components to point at the resource in the source directory. Your tests will work as long as the source is still available at the location from which it was compiled.
Terms of Service

Privacy Policy

Cookie Policy