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.

GitHub - google/gtm-session-fetcher: Google Toolbox for Mac - 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?

3 Likes

You might be running into [SR-12912] Fix crash in test targets with Bundle.module in 5.3 by MaxDesiatov · Pull Request #2905 · apple/swift-package-manager · GitHub

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 [SR-13560] swiftpm: SWIFTPM_MODULE_BUNDLE returning nil · Issue #4500 · apple/swift-package-manager · GitHub.

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

1 Like

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.

Just tried things with Swift 5.5, and I'm still getting nil for SWIFTPM_MODULE_BUNDLE

I think I am hitting the same issue. Was it ever resolved for anyone via a workaround? I've tried adding various unsafe flags in Package.swift to try and reproduce what Xcode does but I cannot get xcodebuild to succeed where swift build does. Of course, it took me a lot of tweaking to get swift build to work at all while Xcode 13.2 would compile and run tests without issue.

For what its worth, this is what I am trying to build/test via command-line: https://github.com/bradhowes/SF2Lib

1 Like