5.3 Resources support not working on with `swift test`

I'm trying to update one project to use the new Resources support, and I'm not sure if this is a case of I'm holding it wrong or something else.

https://github.com/google/gtm-session-fetcher is ObjC code, but we have a basic SwiftPM package. The tests need some resources, so it was hot-wired in the code to work around things, but I made an attempt to things over. If I open the Package.swift in Xcode 12, then the tests seem to build/pass; but if I try to use swift test on the command line, then SWIFTPM_MODULE_BUNDLE seems to always be returning me nil. I'm assuming there shouldn't be a difference in behavior for this case, do I have something configured wrong?

2 Likes

You might be running into https://github.com/apple/swift-package-manager/pull/2905

I don't think I'm hitting that, because the tests don't crash (the fatalError in the diff?); SWIFTPM_MODULE_BUNDLE is resulting in nil under swift test.

Opened https://bugs.swift.org/browse/SR-13560.

I worked around swift test issues by using xcodebuild for testing.

Yep, you're right. I just double-checked and these are two separate issues.

I'm guessing no workaround, so need to wait for a fix and then a new Swift release?

Can't think of a good workaround, but it may be possible to declare a Swift target which contains the resources and just exposes its Bundle.module via a public accessor. The ObjC target could then use that accessor to get to the bundle.

Has there been any progress on this? Seems to also be happening for pure swift projects as well.

I have a project where I have included resources in my test bundle.

Building, testing, accessing the module bundle within Xcode works fine.

running swift test and swift test --generate-linuxmain fail with the following error:

Fatal error: could not load resource bundle: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/libexec/swift/pm/BeartoothSDK_BeartoothSDKTests.bundle: file BeartoothSDKTests/resource_bundle_accessor.swift, line 7

here's my package manifest:

// swift-tools-version:5.3

import PackageDescription

let package = Package(
	name: "BeartoothSDK",
	products: [
		.library(name: "BeartoothSDK", targets: ["BeartoothSDK"]),
	],
	dependencies: [],
	targets: [
		.target(name: "BeartoothSDK", dependencies: []),
		.testTarget(
			name: "BeartoothSDKTests",
			dependencies: ["BeartoothSDK"],
			resources: [.process("Resources")]
		),
	]
)

Oh boy. I just hit the same problem as well. The accessor gets created, but it points to the wrong path. There seems to be a problem in the way the accessor figures out the path of the final product.

it tries to load the resource bundle from /Applications/Xcode.app/Contents/Developer/usr/bin/Kvitto_KvittoTests.bundle which obviously is the wrong place.

For unit tests this is the command that gets executed, which you see if you run swift test --verbose

/Applications/Xcode.app/Contents/Developer/usr/bin/xctest /Users/oliver/Projects/Kvitto/.build/x86_64-apple-macosx/debug/KvittoPackageTests.xctest

I created a bug report on the swift jira for this... not sure if that was the right place:

I opened one back in comment 4:

awesome. I will close mine and reference yours.

tried generating an Xcode project specifically for this workaround. Seems like the extension for Bundle.module never gets generated in this case...

after some digging, looks like its fixed and shipping with Xcode 12.2: https://github.com/apple/swift-package-manager/pull/2817

I don't think that is a fix.

There's a comment on it about pulling it over into the 5.3 branch. That PR mentions that it doesn't fix the issue I reported. So at this point Xcode 12.2 is likely still going to fail in this case.

Terms of Service

Privacy Policy

Cookie Policy