Hello,
I'm running into what looks like incorrect behavior in Swift Package Manager's support for package traits (as introduced in SE-0450).
Setup
I have a Swift package structured like this:
BackendA
: imports FoundationBackendB
: does not import FoundationMain
: conditionally depends on eitherBackendA
orBackendB
The package defines these traits:
traits: [
"UseBackendA",
"UseBackendB",
.default(enabledTraits: ["UseBackendA"])
]
And Main
conditionally links:
.target(
name: "Main",
dependencies: [
.target(name: "BackendA", condition: .when(traits: ["UseBackendA"])),
.target(name: "BackendB", condition: .when(traits: ["UseBackendB"]))
]
)
Problem
In a consumer package, I set:
.package(url: "...", traits: ["UseBackendB"])
However, when building the package and inspecting the binary (ldd
, nm
, etc.), it still links Foundation, even though:
BackendB
does not import FoundationBackendA
is not used
This was tested in a clean swift-latest
Docker container to rule out host SDK contamination.
Questions
- Could this be a bug or is this known?
- Should
.target(condition: .when(traits: ...))
completely exclude the target from being built and linked?
If needed, I can share a minimal example package that reproduces this.
Thanks in advance!