skip to content

In a Swift package manifest, how do targets map to modules, and what does a library product expose to the packages that depend on it?

level: middleimportance: must knowfreq 62%

answer

  1. one target, one module
  2. Sources/<TargetName> by convention
  3. products are the outside contract
  4. clients import modules, not product names

basics

~20 s

Each SwiftPM target builds one module, by default from Sources/<TargetName>. A library product is the outward-facing unit: it vends the public API of the targets it lists, and other packages depend on products, never on bare targets.

solid answer

~40 s

A target is SwiftPM's build unit and compiles into exactly one module, the name a Swift file writes after `import`. Targets live in `Sources/<TargetName>` (tests in `Tests/<TargetName>`) unless `path:` says otherwise, and they depend on each other by name inside the package. A product is what the package offers outside: `.library` vends the public API of the targets it lists, `.executable` a runnable tool, `.plugin` a plugin. Clients declare `.product(name:package:)` but import the listed targets' module names. `internal` stays in its module and `package` declarations stay inside the package. A target in no product, such as an `HTTPCore` helper, cannot be named by clients: SwiftPM reports `product 'HTTPCore' ... not found`. Leave the library `type:` out so SwiftPM picks static or dynamic linking for the client.

code

swift · 19 lines
swift
// swift-tools-version: 6.1
import PackageDescription

let package = Package(
    name: "ClientKit",
    platforms: [.iOS(.v17), .macOS(.v14)],
    products: [
        .library(name: "Networking", targets: ["Networking"]),
        .library(name: "DesignSystem", targets: ["DesignSystem"]),
        .executable(name: "tokengen", targets: ["TokenGen"]),
    ],
    targets: [
        .target(name: "HTTPCore"),                       // Sources/HTTPCore, no product
        .target(name: "Networking", dependencies: ["HTTPCore"]),
        .target(name: "DesignSystem"),
        .executableTarget(name: "TokenGen", dependencies: ["DesignSystem"]),
        .testTarget(name: "NetworkingTests", dependencies: ["Networking"]),
    ]
)

go deeper

for a junior

Recall the mapping: one target is one module in Sources/<TargetName>, and a library product is what another package can depend on.

for a middle

Explain how products differ from targets, why clients import module names, what stays hidden in a target no product lists, and what an omitted library type means.

for a senior

Review a manifest by its products as a public contract, catch accidental imports of internal targets, and push back on forced dynamic or static library types.

for a principal

Decide which targets become products at all, since each product is a promise to clients that is costly to rename or merge later.

## A target is a module A **target** is SwiftPM's unit of build: each target compiles into exactly one **module**, the name a Swift file writes after `import`. A target holds Swift or C-family sources, not both. Unless `path:` says otherwise, SwiftPM finds a target's sources by convention: - regular and executable targets in `Sources/<TargetName>` (it also searches `Source`, `src` and `srcs`); - test targets in `Tests/<TargetName>`; - a custom `path:` must stay inside the package root, so `../Shared` is invalid. The factory says what kind of module it builds: | Factory | What it builds | |---|---| | `.target` | a regular library module | | `.executableTarget` | a program's main module, with `main.swift` or a type marked `@main` | | `.testTarget` | tests for the package's other targets | | `.macro` | a macro implementation (needs `import CompilerPluginSupport`) | | `.plugin` | a build tool or command plugin | Binary targets that wrap an XCFramework, and system library targets, exist too; integrating a vendor binary is a separate lesson. ## Dependencies inside and across packages Inside one package, a target names another target directly: `dependencies: ["HTTPCore"]` or `.target(name: "HTTPCore")`. Across packages, a target can only name a **product**, with `.product(name: "Networking", package: "ClientKit")`, where `package:` is the identity SwiftPM derives for that dependency (for a local path dependency, the directory name). The `package:` argument has been mandatory since tools version 5.2. ## A product is the public surface A **product** is the externally visible artifact SwiftPM assembles from one or more targets. There are three kinds: - `.library(name:type:targets:)` vends library targets so clients can import their public API; - `.executable(name:targets:)` vends an executable target so clients can run it; - `.plugin(name:targets:)` vends plugin targets to clients. What a library product exposes is the **public** API of the modules it lists. `internal` declarations stay inside their module, and `package` declarations (tools version 5.9 and later) reach other targets in the same package but never clients; access levels themselves are covered with the Swift language. The product name is used only in dependency declarations, because clients import modules: a product `ClientKitUI` that lists `DesignSystem` and `Icons` is consumed with `import DesignSystem` and `import Icons`, and there is no `ClientKitUI` module to import. A target that no product lists is an implementation detail. A client that asks for it is refused when the graph loads: `product 'HTTPCore' required by package 'app' target 'App' not found in package 'Lib'`. One thing a reviewer should still watch for: an `import HTTPCore` can compile in a client when that module is already in the build through a product the client does use. That is accidental reach, not a declared dependency, and it breaks the day the package renames or merges its internal targets. Products matter for **other** packages. In the root package, `swift run TokenGen` runs an executable target even without an `.executable` product; the product is what lets a client package run it, under the product's own name. In the scenario, the `.executable(name: "tokengen", targets: ["TokenGen"])` product is invoked as `swift run tokengen`, while the module inside is still `TokenGen`, the same split between product names and module names that applies to libraries. ## Static, dynamic or automatic | `type:` on `.library` | Meaning | |---|---| | omitted (`nil`) | SwiftPM chooses static or dynamic linking based on the client's preference; `swift package dump-package` shows it as `automatic` | | `.static` | always a statically linked library | | `.dynamic` | always a dynamically linked library | The PackageDescription documentation recommends leaving `type:` out and forcing a type only when the library does not support both kinds of linking. A package can also vend the same targets twice, for example a `Paper` product plus `PaperStatic` with `.static` and `PaperDynamic` with `.dynamic`, and let each client choose. ## Reading a manifest in review 1. List the products first: they are the package's public contract. 2. For each product, follow its targets and their `dependencies`; everything reachable is built, but only listed targets are importable by name. 3. Check that internal helpers are in no product, and that clients do not import them anyway. 4. Check that each library leaves `type:` unset unless there is a stated reason.

  • Why does `import ClientKitUI` fail when ClientKitUI is a library product listing DesignSystem and Icons?
    A product name is only used in dependency declarations; what gets imported are modules, and each module is a target. The client writes `import DesignSystem` and `import Icons`. There is no module called ClientKitUI unless a target has that name.
  • When would you set `type: .dynamic` on a library product?
    Rarely. The documentation recommends leaving the type out so SwiftPM chooses static or dynamic linking for the client, and forcing one only when the library does not support both. A package that wants to offer both can vend the same targets as two extra products, one `.static` and one `.dynamic`.
  • How does a target depend on a product of a local package in the same repository?
    The package declares `.package(path: "../Lib")`, and the target uses `.product(name: "Networking", package: "Lib")`. For a path dependency the package identity is the directory name, so `package:` must match it.

A package is a workshop: each target is a workbench that produces one part, a module, and the products are what is on the shop counter. Customers can only order what is on the counter, and once the order arrives they use the parts inside by the parts' own names.

  • A client imports a library by its product name.
  • Every target in a package is available to packages that depend on it.
  • A library product should always set type: .dynamic to keep app binaries small.
  • A library product also exposes the internal declarations of its targets to clients.
  • One target can mix Swift and Objective-C sources in the same folder.