How to init .product(name: , package) in Swift Package

ydcat
2 min readJul 6, 2024

Assume you init a swift package with command:

swift package init - name MyApp - type executable

and want to add package dependency, for example, `the composable architecture`.

For `dependencies` in Package, it is obvious. Just fill url and other specification in `.package()`.

import PackageDescription
let package = Package(
name: "MyApp",
dependencies: [
.package(url: "https://github.com/pointfreeco/swift-composable-architecture/blob/main/Package.swift", branch: "main"),
],
targets: [
.executableTarget(
name: "MyCLI",
dependencies: [
],
path: "Sources"),
]
)

But for `dependencies` in `targets`, when you use `.product(name: , package: )` initializer, it is not that straid forward.
As in the `.product(name:, product:)`, the `name` parameter means the name of the product, the `package` parameter means the name of the package. The method to find the right property is, get to the package source code you want to add.

// Package.swift of the composable architecture
// swift-tools-version:5.9
import CompilerPluginSupport
import PackageDescription
let package = Package(
name: "swift-composable-architecture",

products: [
.library(
name: "ComposableArchitecture",
targets: ["ComposableArchitecture"]
)
]

)

In the `Package.swift`, the `name` property of Package as in `Package(name: “”)` should be the package name. the `name` property in `executableTarget()` could fill in the name parameter of `.product()`.
Therefore, the final config should be as follow.

import PackageDescription
let package = Package(
name: "MyApp",
dependencies: [
.package(url: "https://github.com/pointfreeco/swift-composable-architecture/blob/main/Package.swift", branch: "main"),
],
targets: [
.executableTarget(
name: "MyCLI",
dependencies: [
.product(name: "ComposableArchitecture", package: "swift-composable-architecture")
],
path: "Sources"),
]
)

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

ydcat
ydcat

Written by ydcat

0 Followers

Sharing thoughts about tech & idea.

No responses yet

Write a response