Search code examples
iosswiftdependenciesswift-package-manager

Swift Package Manager - Exclude dependency


I'm having a second thought using multiple products inside SPM. Here is the thing.

I’ll provide two products in the package. One is “Tool” and the other one is “ToolNetworking”. The first one has some swift classes, nothing special, no dependencies at all. The second one also has some swift logic, but a dependency on Alamofire lib.

Here is the config:

// swift-tools-version:5.1
import PackageDescription

let package = Package(
  name: "Tool",
  platforms: [.iOS(.v11)],
  products: [
    .library(name: “Tool", targets: [“Tool"]),
    .library(name: “ToolNetworking", targets: [“ToolNetworking"])
  ],
  dependencies: [
    .package(url: "https://github.com/Alamofire/Alamofire.git", .exact("5.1.0"))
  ],
  targets: [
    .target(
      name: "Tool",
      path: "Sources",
      exclude: ["Networking"]
    ),
    .target(
      name: "ToolNetworking",
      dependencies: ["Tool", "Alamofire"],
      path: "Sources",
      sources: ["Networking"]
    )
  ],
  swiftLanguageVersions: [.v5]
)

When I want to install the package, I can choose between the two. If I select ToolNetworking, it will also install Alamofire dependency.

For Tool product I only want SPM to install my code, no dependencies. Here is my question. How can I exclude Alamofire when I install only Tool product since I didn't define a dependencies field?

Thanks


Solution

  • A first step would be to upgrade to Swift 5.2. The Swift Package Manager shipped with Swift 5.2 starts to implement exactly this behaviour (as described in SE-0226).

    However, since it's not fully implemented yet, it could be that Xcode will still check out Alamofire. It should not link it in the end, though. So if you only use the Tool product, the resulting binary will not have any traces of Alamofire inside it :-)