Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift Package Manager, How to add a package as development dependency?

Is there a way, to add a spm package as development dependency?

For example, is there a way we can do some thing like, developmentDependencies: { somePackage }.

(like we can easily achieved in other package managers like npm, pub, etc?)

like image 848
zrfrank Avatar asked Oct 21 '25 18:10

zrfrank


1 Answers

Actually, I can confirm1 at as of Swift 5.2 this is possible. SE-0226 defines "Target Based Dependency Resolution", which basically means that SPM will only download the dependencies that are actually required by the target(s) you use.

For example:

// swift-tools-version:5.2
// The swift-tools-version declares the minimum version of Swift required to build this package.

import PackageDescription

let package = Package(
    name: "SwiftlySearch",
    platforms: [
        .iOS(.v13)
    ],
    products: [
        .library(
            name: "SwiftlySearch",
            targets: ["SwiftlySearch"]
        ),
    ],
    dependencies: [
        .package(url: "https://github.com/nalexn/ViewInspector.git", from: "0.4.3")
    ],
    targets: [
        .target(
            name: "SwiftlySearch",
            dependencies: []
        ),
        .testTarget(
            name: "SwiftlySearchTests",
            dependencies: ["SwiftlySearch", "ViewInspector"]
        ),
    ]
)

This will only download ViewInspector for the target "SwiftlySearchTests", and not for the released library SwiftlySearch.

TL;DR: Just declare dependencies only on the targets that use them, SPM will figure out the rest.


1 I just tested this using the built-in package manager in Xcode 11.6, which behaved as expected.

like image 54
thislooksfun Avatar answered Oct 24 '25 08:10

thislooksfun



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!