Search code examples
swiftxcodeswift-package-manager

Swift Package Manager, target based dependency


I'm trying to get dependency target based in SPM, I have Package.swift defined here:

enter image description here

So I have RxSwift defined in package dependencies but it's NOT used in target ReCaptcha but only in target ReCaptchaRx

What I expected to happen:

If I choose to select only one target without Rx I expected it to fetch only ReCaptcha dependency and ignore Rx.

enter image description here

What actually happen:

After importing only ReCaptcha I'm getting package resolved but unusable, if I choose on import window ReCaptchaRx I'm getting as expected usable Rx what is fine. But why then SPM even fetching and resolving RxSwift in this case?

enter image description here

Question

Why Xcode is resolving RxSwift on target that does not have a dependency to it? Can that be prevented? I would expect similar behaviour as with AppSwizzle in this example that if target is not used is not being resolved.

Source code:

This is an open source library available here: https://github.com/JakubMazur/ReCaptcha/blob/swift-package-manager/Package.swift

If you want to checkout this in SPM will resolve package from https://github.com/JakubMazur/ReCaptcha on branch swift-package-manager


Solution

  • Basically, Swift Package Manager is not yet sophisticated enough to not resolve all transitive dependencies, even if they are exclusively used by a target you are not depending on.

    While it is clutter that all transitive dependencies show in the Xcode sidebar package dependencies list, I do not believe it implies that your Demo app would actually ship with any of those unused targets/dependencies. It is only clutter in the sense that they show in that sidebar and that they are checked out in cache directories.

    Slightly different: as of SPM 5.2, SPM is smart enough to not download transitive dependencies only used by your dependencies' test targets. Perhaps one day this will be improved to cover the case you ask about as well.