skip to content

In a Package.swift manifest, how do `swiftSettings` set one target's language mode, default isolation and conditional flags, and why is `unsafeFlags` a problem in a published library?

level: seniorimportance: should knowfreq 38%

answer

  1. settings belong to one target
  2. each setting has a tools version
  3. MainActor.self or nil
  4. typed settings over raw flags

basics

~20 s

swiftSettings apply per target: .swiftLanguageMode(.v5) (tools 6.0) keeps one target in Swift 5 mode, .defaultIsolation(MainActor.self) (6.2) makes unannotated code main-actor isolated, and .define adds conditions. unsafeFlags makes products containing the target ineligible for use by other packages.

solid answer

~40 s

Every target factory takes `swiftSettings:`, and each setting applies to that target's module. The package-level `swiftLanguageModes` lists the package's modes and replaced `swiftLanguageVersions` in tools version 6.0, when `.swiftLanguageMode(.v5)` arrived to hold back one target while the rest default to Swift 6. `.defaultIsolation(MainActor.self)`, from 6.2, makes unannotated code main-actor isolated; `nil` or no setting means `nonisolated`. `.define("VERBOSE_HTTP", .when(configuration: .debug))` adds a valueless `#if` condition, and conditions can also name platforms or, from 6.1, traits. `unsafeFlags` passes arbitrary compiler flags, and the documentation says products containing that target become ineligible for use by other packages; with SwiftPM 6.4 the root package and a local path dependency still built with it, so a passing local build proves nothing. Prefer typed settings such as `.treatAllWarnings(as: .error)`.

code

swift · 25 lines
swift
// swift-tools-version: 6.2
import PackageDescription

let package = Package(
    name: "ClientKit",
    platforms: [.iOS(.v17), .macOS(.v14)],
    products: [
        .library(name: "Networking", targets: ["Networking"]),
        .library(name: "DesignSystem", targets: ["DesignSystem"]),
    ],
    targets: [
        .target(
            name: "Networking",
            swiftSettings: [
                .swiftLanguageMode(.v5),                  // not yet migrated
                .define("VERBOSE_HTTP", .when(configuration: .debug)),
            ]
        ),
        .target(
            name: "DesignSystem",
            swiftSettings: [.defaultIsolation(MainActor.self)]
        ),
    ],
    swiftLanguageModes: [.v6]
)

go deeper

for a junior

Recall that swiftSettings are per target and that unsafeFlags is a red flag in a package other teams depend on.

for a middle

Explain package-level swiftLanguageModes versus a per-target swiftLanguageMode, what defaultIsolation changes, and how .when conditions narrow a setting to a platform, configuration or trait.

for a senior

Use per-target language modes and defaultIsolation to migrate modules one at a time, and replace unsafe flags with typed settings before a library is published.

for a principal

Decide which settings a team standardises across its packages, such as default isolation for UI modules and warnings as errors, and which tools version that commits clients to.

## Where Swift settings live Every target factory takes `swiftSettings:`, an array of `SwiftSetting` values, alongside `cSettings`, `cxxSettings` and `linkerSettings`. A setting applies to the module of the target that declares it; for upcoming features the documentation says so explicitly, noting that enabling one in a target does not affect its dependencies. Each setting has its own tools-version gate: | Setting | What it does | Tools version | |---|---|---| | `.define(_:_:)` | adds a compilation condition for `#if`, with no value | 5.0 | | `.unsafeFlags(_:_:)` | passes arbitrary command-line flags to the compiler | 5.0 | | `.enableUpcomingFeature(_:_:)` | turns on a named upcoming feature | 5.8 | | `.interoperabilityMode(_:_:)` | C or C++ interoperability for the module | 5.9 | | `.swiftLanguageMode(_:_:)` | the target's Swift language mode | 6.0 | | `.defaultIsolation(_:_:)` | default actor isolation for unannotated code | 6.2 | | `.strictMemorySafety(_:)` | strict memory-safety checking | 6.2 | | `.treatAllWarnings(as:_:)`, `.treatWarning(_:as:_:)` | warnings as errors, for all or for one named warning | 6.2 | ## Language mode: package-wide or one target The package-level `swiftLanguageModes:` parameter lists the language modes the package supports; it replaced `swiftLanguageVersions`, which tools version 6.0 deprecated. Since 6.0 one target can override it with `.swiftLanguageMode(.v5)`, which is how a team migrates target by target. Because targets default to the Swift 6 mode from tools version 6.0, a raised tools version plus one per-target `.v5` keeps an unmigrated module building. `.swiftLanguageVersion(_:)` exists only as a deprecated spelling renamed to `.swiftLanguageMode(_:)`. What the Swift 6 mode changes in the language is a separate lesson. ## Default isolation `.defaultIsolation(MainActor.self)`, from tools version 6.2, makes unannotated code in the target main-actor isolated. The only valid arguments are `MainActor.self` and `nil`; with `nil`, or without the setting, the compiler infers unannotated code as `nonisolated`. It suits UI-facing modules such as a design system: in the scenario package, `public let sharedStore = ThemeStore()`, a global holding a non-Sendable class, compiles in Swift 6 mode only with the setting on. A networking or parsing module usually stays on the default. ## Conditions Most settings take an optional `BuildSettingCondition`: - `.when(platforms: [.iOS])` limits a setting to platforms; - `.when(configuration: .debug)` limits it to a build configuration; - from tools version 6.1, `.when(traits: ["RemoteLogging"])`, also combinable with platforms and configuration, ties a setting to a package trait. `.define("VERBOSE_HTTP", .when(configuration: .debug))` makes `#if VERBOSE_HTTP` true in debug builds of that one target. Unlike a C macro, the condition carries no value. ## Why unsafeFlags is a trap in a library `.unsafeFlags` passes any command-line flag to the compiler, and `LinkerSetting.unsafeFlags` does the same for the linker. Because SwiftPM cannot judge what a flag does to the build, the documentation states that using unsafe flags makes the products containing the target ineligible for use by other packages. The trap is that local builds look fine: the root package builds with them, and in a check with SwiftPM 6.4 a local path dependency using them still built, so a green local build does not show that the library can be consumed the way the documentation describes. The fix is nearly always a typed setting: 1. A hand-passed warnings-as-errors flag becomes `.treatAllWarnings(as: .error)`, which the documentation describes as equivalent to `-warnings-as-errors`. 2. A hand-passed upcoming-feature flag becomes `.enableUpcomingFeature("Name")`. 3. A hand-passed language-mode flag becomes `.swiftLanguageMode(.v5)`. 4. A hand-passed `-D` condition becomes `.define("NAME")`. When a typed setting needs a newer tools version than some clients have, raise the tools version, or keep `Package.swift` at the newest version and serve older toolchains from a version-specific manifest such as `[email protected]`. ## Reading a settings block in review A few checks catch most mistakes before they reach a dependent app: - **Typos fail quietly.** The documentation says targets ignore unknown upcoming and experimental feature names, so a misspelt feature name passed to `.enableUpcomingFeature(_:)` does nothing; confirm the feature actually took effect. - **Every setting has a gate.** A setting newer than the manifest's tools version is a manifest compile error such as `'defaultIsolation' is unavailable`, not a silent no-op. - **Strict memory safety only warns.** `.strictMemorySafety()` reports uses of unsafe constructs as warnings that annotations such as `@unsafe` and `unsafe` acknowledge; pair it with a warnings policy if the team wants it enforced. - **Conditions beat copies.** One target with `.when(configuration: .debug)` is easier to keep correct than two near-identical targets.

  • The package must still build for clients on tools version 6.0, but you want `.defaultIsolation(MainActor.self)`; what are the options?
    The setting needs tools version 6.2, so a 6.0 manifest cannot name it. Either raise the tools version and drop those clients, or keep `Package.swift` at 6.2 and add `[email protected]` without the setting, and give that module explicit `@MainActor` annotations so it compiles under both manifests.
  • Why replace `.unsafeFlags(["-warnings-as-errors"])` with `.treatAllWarnings(as: .error)`?
    The typed setting, available from tools version 6.2, is documented as equivalent to passing `-warnings-as-errors`, and it does not carry the unsafe-flags rule that makes products ineligible for other packages. It can also be scoped with a condition, such as release builds only.

  • swiftLanguageVersions is the current way to choose a package's language mode.
  • A swiftSettings entry on one target also applies to every target that depends on it.
  • unsafeFlags is harmless in a library because the flags only touch its own module.
  • defaultIsolation accepts any global actor type, such as a custom DatabaseActor.
  • .define("API_LEVEL=2") gives the condition a value, as a C macro would.

Assumes SwiftPM 6.4; swiftLanguageMode needs tools version 6.0, trait conditions 6.1, and defaultIsolation, strictMemorySafety and treatAllWarnings 6.2.