SwiftGen: 6.4.0
Xcode: 12.2/12.3
iOS: 13+
I'm using local Swift packages to organise code within my workspace. This has been working fine until I tried adding snapshot tests to my feature packages, which depend on another package containing SwiftGen assets.
My workspace is currently structured like this (where -> means depends on):
MyApp (Xcode Project) -> Login (Package) -> UILibrary (Package)
When adding snapshot tests to my UILibrary, the snapshot tests work fine, and everything loads correctly. However, when I add snapshot tests to my Login package (which depends on UILibrary) the tests reach a fatal error when the UILibrary is trying to load the Color assets generated by SwiftGen which used the generated Bundle.module extension generated by Xcode.
fatalError("unable to find bundle named UILibrary_UILibrary")
Does anyone have any ideas on how to fix/workaround this? I'm currently facing re-organising my code as Xcode frameworks instead of Swift packages, as I've not had this issue on previous projects.
Things I have tried:
Bundle(for: BundleToken.self) instead of Bundle.module – still can't load.My Login Package.swift file looks like this:
let package = Package(
name: "Login",
platforms: [.iOS(.v13)],
products: [
.library(name: "Login", type: .dynamic, targets: ["Login"])
],
dependencies: [
.package(path: "../Shared/UILibrary"),
.package(name: "SnapshotTesting", url: "https://github.com/pointfreeco/swift-snapshot-testing.git", from: "1.8.1"),
],
targets: [
.target(name: "Login", dependencies: ["UILibrary"]),
.testTarget(name: "LoginTests", dependencies: ["Login", "SnapshotTesting"])
]
)
My UILibrary Package.swift file looks like this:
let package = Package(
name: "UILibrary",
platforms: [.iOS(.v13)],
products: [
.library(name: "UILibrary", type: .dynamic, targets: ["UILibrary"])
],
dependencies: [
.package(name: "SnapshotTesting", url: "https://github.com/pointfreeco/swift-snapshot-testing.git", from: "1.8.1"),
],
targets: [
.target(name: "UILibrary", dependencies: [], resources: [.process("Resources")]),
.testTarget(name: "UILibrary Tests", dependencies: ["UILibrary", "SnapshotTesting"])
]
)
Note: Resources contains my colors & image asset catalogs so they are processed as part of the UILibrary target.
I have raised an issue on the Swift forums here where a temporary workaround has been provided. I'll also file a bug report with Apple for this.
Hey @bengilroy,
From what I could gather, seems like a Swift (package manager) issue? Not much we can do about that.
The workaround that was mentioned, was customizing the used Bundle. You can do this easily by setting the template parameter, check the template documentation and config documentation.
For example:
strings:
inputs:
- en.lproj/Localizable.strings
- en.lproj/Localizable.stringsdict
outputs:
templateName: structured-swift5
output: Strings.swift
params:
bundle: Bundle.myModule
If needed, you could use a different configuration for normal vs. testing scenarios, or have the Bundle.myModule variable be a calculated var that changes value depending on the situation. Up to you how you'd like to handle this.
Thanks @djbe I didn't know about the custom bundle param, that simplifies my workaround a little. Feel free to close this if you want as it's an SPM issue 😀