Package traits also don’t yet prevent the resolution of “not used” dependencies, which may perhaps also be improved in the future, but AFAIK at present package traits are also insufficient to avoid unused dependencies from being resolved.
I did some work in this area, and you’re correct in that package traits don’t entirely handle the case for omitting unused dependencies – the current criteria for traits to omit a package dependency is that the dependency has to be:
- Guarded by an un-enabled trait in each target dependency reference, e.g.:
targets: [
.executableTarget(
// ... target info
dependencies: [
.product(
name: "ProductFromPackageDependency",
package: "MyPackageDependency",
condition: .when(traits: ["GuardingMyPackageDependency"])
)
]
)
]
- The dependency is not used in any other case (i.e. there is no target dependency declared that uses this package dependency without a trait condition).
To specifically address whether SwiftPM handles unused dependencies: there is an experimental flag that I did some work on –experimental-prune-unused-dependencies that should omit package dependencies that aren’t used, but since it’s experimental it’s not something that I would say is 100% reliable at the moment. I’m hoping to see some development here in the future, and if folks try it out I would love to know your thoughts! ![]()