5

I'm currently developing a Swift Package in which I would like to use other Swift Packages. To do so I added the packages I want to use to the dependencies in my Package.swift

dependencies: [
    // Dependencies declare other packages that this package depends on.
    // .package(url: /* package url */, from: "1.0.0"),
    .package(url: "https://github.com/jedisct1/swift-sodium.git", .upToNextMajor(from: "0.9.1")),
    .package(url: "https://github.com/Alamofire/Alamofire.git", .upToNextMajor(from: "5.5.0"))
],

This is my full Package.swift:

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

import PackageDescription

let package = Package(
    name: "SpaceCryptography",
    platforms: [
        .iOS(.v10),
        .watchOS(.v3)
    ],
    products: [
        // Products define the executables and libraries a package produces, and make them visible to other packages.
        .library(
            name: "SpaceCryptography",
            targets: ["SpaceCryptography"]),
    ],
    dependencies: [
        // Dependencies declare other packages that this package depends on.
        // .package(url: /* package url */, from: "1.0.0"),
        .package(url: "https://github.com/jedisct1/swift-sodium.git", .upToNextMajor(from: "0.9.1")),
        .package(url: "https://github.com/Alamofire/Alamofire.git", .upToNextMajor(from: "5.5.0"))
    ],
    targets: [
        // Targets are the basic building blocks of a package. A target can define a module or a test suite.
        // Targets can depend on other targets in this package, and on products in packages this package depends on.
        .target(
            name: "SpaceCryptography",
            dependencies: []),
        .testTarget(
            name: "SpaceCryptographyTests",
            dependencies: ["SpaceCryptography"]),
    ]
)

The packages then appear under "Package Dependencies in the Project Navigator. But when I try to use "import Alamofire" I get an error saying "No such module 'Alamofire'. How to properly use these packages in my own package?

Tim Langner
  • 357
  • 3
  • 16
  • In `targets`, did you set the dependency? Here, you just specified that your package needs them, but since you can have multiple targets, you need to tell the target that it depends on one or another. Look at the Alamofire+Image https://github.com/Alamofire/AlamofireImage/blob/master/Package.swift that specifiy it with `dependencies: ["Alamofire"]`. – Larme Feb 04 '22 at 10:12
  • No, I didn't do that. I gave the dependencies a name now and added them to my target dependencies. Now I was able to import them. Thanks! – Tim Langner Feb 04 '22 at 10:15

2 Answers2

5

As previous mentioned, you have to set the dependencies "Alamofire" and "Sodium" to the target. For example:

.target(
   name: "SpaceCryptography",
   dependencies: ["Alamofire", "Sodium"]),

I recommend to also give names to the dependencies packages defined above:

dependencies: [
        // Dependencies declare other packages that this package depends on.
        // .package(url: /* package url */, from: "1.0.0"),
        .package(name: "Sodium", url: "https://github.com/jedisct1/swift-sodium.git", .upToNextMajor(from: "0.9.1")),
        .package(name: "Alamofire" url: "https://github.com/Alamofire/Alamofire.git", .upToNextMajor(from: "5.5.0"))
    ]
Gerrit
  • 138
  • 7
3

You need to add them to the dependencies part of the targets. Like this :

targets: [
    // Targets are the basic building blocks of a package. A target can define a module or a test suite.
    // Targets can depend on other targets in this package, and on products in packages this package depends on.
    .target(
        name: "SpaceCryptography",
        dependencies: ["Alamofire"]),
    .testTarget(
        name: "SpaceCryptographyTests",
        dependencies: ["SpaceCryptography", "Alamofire"]),
]
  • 1
    This worked for the Alamofire package. However, I had to explicitly specify a name for the Sodium package to make it throw no errors. – Tim Langner Feb 04 '22 at 10:26