How does an iOS app consume a local Swift package kept in its own repository, and how does that differ from a remote package dependency?
answer
- a folder with a manifest
- a path, not a URL
- no version requirement
- platforms must cover the app
basics
~20 sA local package is a folder with its own Package.swift inside the app's repository; the app links its library products like any package. SwiftPM uses whatever code sits at the path: no version requirement, no tag, edits build immediately.
solid answer
~40 sA **local package** is a directory with its own `Package.swift` that lives in the app's repository and is added to the app's project or workspace by its path. The app target then links the package's **library products**, exactly as with a remote package. Between packages the dependency is written `.package(path:)`. The differences from a remote dependency are what interviewers probe: SwiftPM enforces **no version requirement** for a local dependency and builds whatever code is at the path, so edits show up on the next build with no tag or update step. Everything else still applies: the package's `platforms` must not exceed the app's deployment target, only `public` API crosses into the app, and SwiftPM refuses cyclic target dependencies.
code
swift · 23 lines// swift-tools-version: 6.1
import PackageDescription
let package = Package(
name: "AppFeatures",
platforms: [.iOS(.v17), .macOS(.v14)],
products: [
.library(name: "AppFeatures", targets: ["AppFeatures"]),
],
dependencies: [
// A local package beside this one: no version requirement applies.
.package(path: "../Features"),
],
targets: [
.target(
name: "AppFeatures",
dependencies: [
.product(name: "Checkout", package: "Features"),
.product(name: "Profile", package: "Features"),
]
),
]
)go deeper
Know that a local package is a folder with a Package.swift in your repository, that the app links its products, and that no version requirement applies.
Explain .package(path:), why local dependencies ignore versions, and the checks that still apply: platforms, public API across modules and refused cycles.
Extract a feature into a local package without breaking the app: matching platforms, public initializers, Bundle.module for resources, and links for every target that imports it.
Judge when a local package is the right boundary and when code should graduate to a separately versioned package with its own release cadence.
## What a local package is A Swift package is any directory with a `Package.swift` manifest, sources under `Sources/<TargetName>` and tests under `Tests/`. A **local package** is simply one that lives inside the app's own repository — for example a `Packages/Features` folder next to the Xcode project — rather than at a Git URL. The app's project or workspace references the folder, and the app target links the library products the manifest declares. Two situations produce a local package: - **Modularizing the app**: moving a design system, a networking layer or a feature into package targets. - **Developing a library beside the app**: iterating on a package and the app in the same checkout before publishing it. ## How the app and other packages consume it | Consumer | How it refers to the local package | What it links | |---|---|---| | The app's Xcode project | The package folder added to the project or workspace | Library products, per target | | Another package | `.package(path: "../Features")` in its manifest | `.product(name:package:)` in a target | | The package's own tests | Its `testTarget` entries | Targets by name | A package that depends on a local package uses `.package(path:)`, or `.package(name:path:)` from tools version 5.2; tools version 6.1 added overloads that also take `traits:`. The target then names the product and the package, as in `.product(name: "Checkout", package: "Features")`. ## A worked example Suppose the repository has `Packages/Features`, whose manifest vends `Checkout` and `Profile` as library products, and an `AppFeatures` package beside it that composes them. `AppFeatures` declares `.package(path: "../Features")` and lists `.product(name: "Checkout", package: "Features")` and `.product(name: "Profile", package: "Features")` in its target. The `package:` label in `.product(name:package:)` takes the package's identity, which for a path dependency is the last path component — the folder name `Features`, not the `name:` written in that folder's manifest. The app project then references the local package folder and links the product it needs into the app target, and into any extension that imports it. No step in this chain mentions a version. ## How it differs from a remote dependency 1. **No version requirement.** SwiftPM's documentation is direct: local dependencies do not enforce version constraints and use the version that is available at the path. There is no `from:`, no tag and no update step. 2. **Edits build immediately.** Changing a file in the package changes the next app build. That is the whole point during modularization, and also why a local package is not a release boundary. 3. **No fetch.** Nothing is cloned; the code is whatever your branch contains, so the package and the app are versioned together by the repository's history. Remote dependencies, by contrast, go through resolution against tags and land in `Package.resolved`; that machinery, and how `swift package edit` temporarily overrides a remote dependency with a local checkout, belong to the lesson on version pinning. ## What still applies Moving code into a local package does not relax any of SwiftPM's checks: - **Platforms.** If the package declares `.iOS(.v17)` and the app target supports iOS 16, the build fails with an error that the package product requires a newer minimum platform version than the target supports. Keep the package's `platforms` at or below the app's deployment target. - **Access control.** The app is a different module. Types, initializers and methods it uses must be `public`; inside one package, targets can share `package` declarations without making them public (tools version 5.9 and later). - **Declared dependencies.** Each target lists the targets and products it uses. SwiftPM refuses a cycle with `cyclic dependency declaration found`, which forces a direction between modules. - **Resources.** A moved screen's assets and strings move into the target and are loaded through `Bundle.module`. ## Why teams do it - **Enforced boundaries.** A feature can only use what it declares and what is public or package-visible, so accidental coupling becomes a compile error. - **Smaller rebuild scope.** Each target is its own module: editing a feature module recompiles that module and the modules that depend on it, not unrelated features. - **Faster tests.** A package target that builds for macOS — no iOS-only frameworks such as UIKit — can have its tests run with `swift test` on the Mac, without the app or a simulator. ## Pitfalls - Declaring `platforms` higher than the app's deployment target. - Forgetting `public` on an initializer the app needs. - Linking the product into the app but not into an extension that imports it. - Treating the local package as independently versioned when it is not.
- Does a local package need its own Git repository or release tags?No. It is versioned with the app's repository, and SwiftPM uses the code at the path, so tags play no part. Tags matter only if the package later moves to its own repository and other projects depend on it by URL with a version requirement.
- Can you run a local package's tests without building the app?Yes, when the tested targets also build for macOS. With no iOS-only frameworks such as UIKit in them, `swift test` builds only the package and runs its test targets on the Mac, with no app or simulator involved. Targets that need UIKit are tested through an iOS build instead.
- A local package dependency still needs a version requirement such as from: to resolve.
- SwiftPM uses the version pinned in the local package's own Package.resolved.
- A package can declare a newer iOS minimum than the app; it is skipped on older devices.
- Types in a local package are visible to the app because they share one repository.
- Two package targets may depend on each other as long as neither is a product.