Package dependency in own package

Hi All,

I create a Swift Package (Xcode 14.3) and add a package dependency (GitHub - stephencelis/SQLite.swift: A type-safe, Swift-language layer over SQLite3.) by copying the autogenerated dependency clause into my "Package.swift" file. The "package.resolved" file is created, all good.

Somehow I cannot use the package (SQLite) from my code because the "import SQLite" is not found. This happens from a newly created and otherwise empty package as well.

How can I use the dependent package in my own package?

Sorry for the seemingly simple question but despite a lot of googleing and fiddling with targets/products, I don't get this to work. UI options in Xcode to add targets etc. exist only for apps, not for packages.

Any hints what I am doing wrong here greatly appreciated.

Cheers

2 Likes

There's two places that you need to add dependencies to a swift package, and the second snippet isn't typically included in project READMEs - depending on the package, it can be a little quirky or hard to work out what to use.

The first piece is what you copied - that adds a dependency stanza to the overall Package declaration. The second is what I suspect you were missing - adding a reference to the target that needs the dependency.

The second place, that wasn't obviously referenced in the README of the library project, is the bit you add to a specific target:

dependencies: [
    .product(name: "SQLite", package: "SQLite.swift")
]

And to show you a fuller example, the Package.swift below is a quick-n-dirty example using the specific dependency you referenced:

// swift-tools-version: 5.8

import PackageDescription

let package = Package(
    name: "temp",
    products: [
        .library(
            name: "temp",
            targets: ["temp"]),
    ],
    dependencies: [
        .package(url: "https://github.com/stephencelis/SQLite.swift.git", from: "0.14.1")
    ],
    targets: [
        .target(
            name: "temp",
            dependencies: [
                .product(name: "SQLite", package: "SQLite.swift")
            ]
        ),
        .testTarget(
            name: "tempTests",
            dependencies: ["temp"]),
    ]
)

4 Likes

i should probably start adding these to my READMEs…

2 Likes

Thank you so much! Exactly what I needed. Makes sense now that I see it but somehow couldn't figure it out from documentation. Weird package manager doesn't just automate these things from the UI.

2 Likes