SPM dependencies branch is deprecated on Xcode 13.3 and Swift 5.6
Asked Answered
L

1

15

Xcode 13.3 shows warnings for deprecation on the dependencies for external packages.

For example:

// swift-tools-version:5.6
import PackageDescription

let package = Package(
    name: "MyPackage",
    platforms: [
        .iOS(.v15)
    ],
    products: [
        .library(
            name: "MyPackage",
            targets: ["MyTarget"]),
    ],
    dependencies: [
        .package(
            name: "MyDependency", 
            url: "https://github.com/someorg/somepackage.git",
            .branch("main"))
    ],
    targets: [
        .target(
            name: "MyTarget",
            dependencies: ["MyDependency"])
    ]
)

This shows the warning:

'package(name:url:_:)' is deprecated: use specific requirement APIs instead (e.g. use 'branch:' instead of '.branch')

However applying the recommended update makes the dependency not reachable anymore from MyPackage.

Ladonnalady answered 15/3, 2022 at 14:33 Comment(0)
L
25

It is necessary to set the dependency as a product.

Specifically with .product(name: [PackageName], package: [Name of repository]), example:

// swift-tools-version:5.6
import PackageDescription

let package = Package(
    name: "MyPackage",
    platforms: [
        .iOS(.v15)
    ],
    products: [
        .library(
            name: "MyPackage",
            targets: ["MyTarget"]),
    ],
    dependencies: [
        .package(
            url: "https://github.com/someorg/somepackage.git",
            branch: "main")
    ],
    targets: [
        .target(
            name: "MyTarget",
            dependencies: [
                .product(
                    name: "MyDependency", 
                    package: "somepackage")])
    ]
)

Update

This is still relevant in Xcode 14.3.1 if you have a package with multiple products/libraries.

Ladonnalady answered 15/3, 2022 at 14:33 Comment(1)
It worked for me, thanks!Portiaportico

© 2022 - 2025 — McMap. All rights reserved.