swift¶
The swift archetype is for SwiftPM packages and CLIs: a repo with a Package.swift at its root. It formats, lints,
tests, builds and releases the package with the toolchain the repo pins. Xcode projects (xcodebuild) are out of scope.
# mise.toml
[tools]
swift = "6.4.1"
[vars]
swift_product = "mytool"
[task_config]
includes = [".mise/common/tasks.toml", ".mise/archetypes/swift/tasks.toml"]
Required pins¶
The library doesn't pin Swift, so each repo tracks its own toolchain at its own cadence:
swift: the repo's Swift toolchain, installed from swift.org by mise'score:swiftbackend.
swift format, llvm-cov and llvm-profdata ship inside that toolchain, so formatting and coverage follow the repo's
pin with no shared pins of their own. goreleaser and syft come from the shared pins.
On macOS the swift.org toolchain still takes the SDK, and XCTest, from Xcode or the Command Line Tools, so one of those must be installed. Swift Testing ships with the toolchain itself.
Tasks¶
| Task | What it does |
|---|---|
fmt |
swift format format --in-place, then prose format, then license headers. |
lint |
license check, swift format lint --strict, containers, shell, workflows, prose. |
test |
swift test --enable-code-coverage, writing JUnit reports and coverage/lcov.info. |
build |
swift build -c release, for every product or the one swift_product names. |
ci |
lint, test, build, docs-build. |
pr |
fmt, lint, test, build, docs-build, commit. |
update |
swift package update, which rewrites Package.resolved. |
snapshot |
goreleaser release --snapshot --clean --skip=sign,notarize. |
release |
goreleaser release --clean. Needs a vX.Y.Z tag and credentials. |
resolve |
Hidden. swift package resolve, honouring a committed Package.resolved. test and build run it. |
Formatting¶
fmt and lint run swift format over the repo's Swift files, as git lists them: tracked files plus untracked files
that aren't ignored. Dependency checkouts under .build/, the .mise submodule and anything in .gitignore are never
touched. A .swift-format file at the repo root configures both passes. lint runs with --strict, so warnings fail
the gate too. A repo with no Swift files skips both passes with a message.
Dependencies¶
resolve runs before test and build. When Package.resolved is committed, it resolves with
--force-resolved-versions, so an out-of-date lockfile fails the gate instead of drifting silently. A package that
doesn't commit Package.resolved, which is common for libraries, resolves normally. make update moves dependencies to
the latest versions Package.swift allows and rewrites Package.resolved.
Coverage¶
test writes everything under coverage/, where the reusable CI workflow uploads from:
coverage/junit.xml: XCTest results.coverage/junit-swift-testing.xml: Swift Testing results, which SwiftPM writes beside the XCTest report.coverage/lcov.info:llvm-cov export -format=lcovover the test bundle, excluding.build/andTests/. Codecov ingests it directly.
Extra swift test flags for one run go in SWIFT_TEST_FLAGS:
A package without a Tests/ directory skips test with a message.
Build¶
build is a release build. With swift_product empty it builds every product; set the knob, or SWIFT_PRODUCT for one
run, to build just one. The task prints the directory the binaries landed in.
There's no version stamping. Swift has no equivalent of Go's -ldflags -X, so a repo that wants its version in the
binary generates a source file in a repo-local task and runs it before build (see
Extending & overriding).
Releases¶
GoReleaser OSS has no Swift builder, so the archetype ships a shim, scripts/goreleaser-build.sh, that a repo's
.goreleaser.yaml sets as the go builder's tool. GoReleaser runs it once per target with GOOS and GOARCH set. The
shim maps the pair to a Swift target, runs swift build -c release --product <product>, and copies the binary to where
GoReleaser expects it. Go-only flags such as -ldflags are ignored.
# .goreleaser.yaml
version: 2
builds:
- builder: go
tool: .mise/archetypes/swift/scripts/goreleaser-build.sh
no_main_check: true # there's no Go main package to find
binary: mytool
env: [SWIFT_PRODUCT=mytool]
goos: [darwin]
goarch: [arm64, amd64]
The product comes from SWIFT_PRODUCT in the build's env, falling back to the swift_product knob, so a repo that
sets the knob can drop the env line. Each build entry produces one product; a package that releases several products
lists one build per product.
GOOS/GOARCH |
Swift target |
|---|---|
darwin/arm64 |
--triple arm64-apple-macosx |
darwin/amd64 |
--triple x86_64-apple-macosx |
linux/amd64 |
--swift-sdk x86_64-swift-linux-musl (Static Linux SDK) |
linux/arm64 |
--swift-sdk aarch64-swift-linux-musl (Static Linux SDK) |
The Linux targets build fully static binaries with the
Static Linux SDK, which must be
installed (swift sdk install …) for the same Swift version the repo pins. They work from a macOS or a Linux host. Any
other pair fails with an error.
snapshot skips the same two steps as the go archetype's:
sign: cosign keyless signing needs the GitHub Actions OIDC token, so it only works in the release workflow.notarize: a repo with anotarize.macosblock enables it wheneverMACOS_SIGN_P12is exported, and a developer who has the org's signing material in their shell shouldn't Developer ID-sign and notarize a throwaway snapshot by accident.
Real releases run in CI: the reusable release workflow calls mise run release --draft and publishes the draft once
every asset has landed. Override release in the repo to add release steps; they must leave the git tree clean.
Housekeeping¶
Add .build/ and .swiftpm/ to .gitignore (swift package init does this for you), along with coverage/ and
dist/. The license and prose tasks already skip both directories.
Knobs¶
| Knob | Default | Env override | Meaning |
|---|---|---|---|
swift_product |
every product | SWIFT_PRODUCT |
Product build builds, and the GoReleaser shim's fallback product. |
Per-run only: SWIFT_TEST_FLAGS.
Source¶
archetypes/swift/tasks.toml
# Copyright 2026 BitWise Media Group Ltd
# SPDX-License-Identifier: MIT
#
# swift — archetype for a SwiftPM package or CLI (a Package.swift at the repo
# root). Xcode projects (xcodebuild) are out of scope.
#
# Include AFTER common/tasks.toml in the consumer's root mise.toml:
# [task_config]
# includes = [".mise/common/tasks.toml", ".mise/archetypes/swift/tasks.toml"]
#
# Required root [tools] pin — the library does not pin Swift, so each repo
# tracks its own toolchain at its own cadence (mise's core:swift backend
# installs the swift.org release):
# swift = "6.4.1"
# `swift format`, llvm-cov and llvm-profdata ship inside that toolchain, so
# formatting and coverage follow the repo's pin with no shared pins of their
# own. goreleaser comes from the shared pins. On macOS the swift.org toolchain
# still takes the SDK (and XCTest) from Xcode or the Command Line Tools, so
# one of those must be installed; Swift Testing ships with the toolchain.
#
# The only structural knob is swift_product (empty = every product); the
# per-invocation values SWIFT_PRODUCT and SWIFT_TEST_FLAGS are environment
# variables read inside the scripts — `make build SWIFT_PRODUCT=x` works
# because make exports command-line variables to recipe subshells.
#
# A repo's .swift-format configuration is picked up automatically. There is no
# version stamping: Swift has no -X/-ldflags equivalent, so a repo that wants
# its version in the binary generates a source file in a repo-local task.
# Resolve dependencies, honouring a committed Package.resolved (resolution
# fails rather than silently drifting when it is out of date). A package
# without one — a library that leaves it uncommitted — resolves normally.
[resolve]
hide = true
dir = "{{cwd}}"
run = "if [ -f Package.resolved ]; then swift package resolve --force-resolved-versions; else swift package resolve; fi"
[update]
description = "update dependencies to the latest allowed versions (rewrites Package.resolved)"
dir = "{{cwd}}"
run = "swift package update"
# Auto-fix pass wired into the `fmt` rollup. No resolve dependency: formatting
# reads the sources only (see scripts/format.sh).
[swift-fmt]
hide = true
dir = "{{cwd}}"
run = "{{config_root}}/.mise/archetypes/swift/scripts/format.sh fmt"
# Check-mode pass wired into the `lint` rollup: swift format lint --strict.
[swift-lint]
hide = true
dir = "{{cwd}}"
run = "{{config_root}}/.mise/archetypes/swift/scripts/format.sh check"
# swift test with code coverage → coverage/ (JUnit + LCOV, see scripts/test.sh).
[swift-test]
hide = true
dir = "{{cwd}}"
depends = ["resolve"]
run = "{{config_root}}/.mise/archetypes/swift/scripts/test.sh"
# Release build of one product or all of them (see scripts/build.sh). The knob
# reaches the script as TOOLCHAIN_SWIFT_PRODUCT so the plain SWIFT_PRODUCT
# spelling stays free for per-invocation env overrides.
[swift-build]
hide = true
dir = "{{cwd}}"
depends = ["resolve"]
env = { TOOLCHAIN_SWIFT_PRODUCT = "{{vars.swift_product}}" }
run = "{{config_root}}/.mise/archetypes/swift/scripts/build.sh"
# GoReleaser has no Swift builder; a repo's .goreleaser.yaml drives
# `swift build` through the go builder with scripts/goreleaser-build.sh as its
# `tool` (see that script). The knob rides along so the shim can find the
# product when the build's env does not name one.
# --skip=sign: cosign keyless signing needs the GitHub Actions OIDC token, so it
# only works in the release workflow — locally it would fail or prompt.
# --skip=notarize: a repo with a notarize.macos block enables it whenever
# MACOS_SIGN_P12 is exported, so a developer who has the org's signing material
# in their shell does not Developer ID-sign and notarize a throwaway snapshot by
# accident (run goreleaser by hand for a deliberate end-to-end test).
# GoReleaser shells out to syft for SBOMs; syft is on PATH via the pins.
[snapshot]
description = "build a local release snapshot (binaries + archives, no publish, signing, or notarization)"
dir = "{{cwd}}"
env = { TOOLCHAIN_SWIFT_PRODUCT = "{{vars.swift_product}}" }
run = "goreleaser release --snapshot --clean --skip=sign,notarize"
# The reusable release workflow runs `mise run release --draft` (the release
# stays a draft until the workflow publishes it, last), with the org's signing
# and notarization material in the environment. A repo that needs extra release
# steps overrides this task; whatever it runs must leave the git tree clean, or
# GoReleaser refuses to release.
[release]
description = "build and publish a release (needs a vX.Y.Z tag + creds)"
dir = "{{cwd}}"
env = { TOOLCHAIN_SWIFT_PRODUCT = "{{vars.swift_product}}" }
run = "goreleaser release --clean"
# Canonical contract: sequential composites preserve the old serial-make
# ordering (fmt before lint; mutating passes never race).
[fmt]
description = "swift format + prose format, inject license headers"
run = [{ task = "swift-fmt" }, { task = "fmt-prose" }, { task = "license" }]
[lint]
description = "all check-mode static analysis"
run = [
{ task = "license-check" },
{ task = "swift-lint" },
{ task = "lint-containers" },
{ task = "lint-shell" },
{ task = "lint-workflows" },
{ task = "lint-prose" },
]
[build]
description = "release build of the package's products"
run = [{ task = "swift-build" }]
[test]
description = "run the tests with coverage"
run = [{ task = "swift-test" }]
[ci]
description = "the gates the reusable CI workflow runs"
run = [{ task = "lint" }, { task = "test" }, { task = "build" }, { task = "docs-build" }]
[pr]
description = "full local gate before a pull request"
run = [
{ task = "fmt" },
{ task = "lint" },
{ task = "test" },
{ task = "build" },
{ task = "docs-build" },
{ task = "commit" },
]
archetypes/swift/scripts/format.sh
#!/bin/sh
# Copyright 2026 BitWise Media Group Ltd
# SPDX-License-Identifier: MIT
#
# swift format (shipped in the Swift toolchain the repo pins) over the repo's
# Swift sources. The file list comes from git — tracked plus untracked-but-not-
# ignored files — so dependency checkouts under .build/, the .mise submodule
# and anything ignored are never touched; .build/ and .swiftpm/ are excluded
# explicitly too, for a repo that forgot to ignore them. The repo's
# .swift-format configuration is picked up automatically. A repo with no Swift
# sources is skipped with a message.
#
# format.sh fmt swift format format --in-place
# format.sh check swift format lint --strict (warnings fail the gate)
set -eu
mode="${1-}"
case "$mode" in
fmt) set -- format --in-place --parallel ;;
check) set -- lint --strict --parallel ;;
*)
echo "usage: format.sh fmt|check" >&2
exit 2
;;
esac
swift_files() {
git ls-files -z --cached --others --exclude-standard -- '*.swift' \
':(exclude).build' ':(exclude).swiftpm'
}
if [ -z "$(swift_files | tr -d '\0' | head -c 1)" ]; then
echo "swift format: no Swift sources; skipping"
exit 0
fi
swift_files | xargs -0 swift format "$@"
archetypes/swift/scripts/test.sh
#!/bin/sh
# Copyright 2026 BitWise Media Group Ltd
# SPDX-License-Identifier: MIT
#
# swift test with code coverage, writing a JUnit report and an LCOV export
# under coverage/ (where the reusable CI workflow uploads from). Swift Testing
# results land in coverage/junit-swift-testing.xml beside the XCTest ones in
# coverage/junit.xml. The LCOV comes from llvm-cov — shipped in the pinned
# toolchain — over the test bundle SwiftPM built, minus dependency checkouts
# and the tests themselves. A package without a Tests/ directory is skipped
# with a message.
#
# SWIFT_TEST_FLAGS: extra `swift test` flags for one run, word-split on
# purpose (`make test SWIFT_TEST_FLAGS="--sanitize=thread --filter Parser"`).
set -eu
if [ ! -d Tests ]; then
echo "swift-test: no Tests/ directory; skipping"
exit 0
fi
mkdir -p coverage
# shellcheck disable=SC2086 # SWIFT_TEST_FLAGS is a flag list
swift test --enable-code-coverage --xunit-output coverage/junit.xml ${SWIFT_TEST_FLAGS:-}
bin="$(swift build --show-bin-path)"
profdata="$bin/codecov/default.profdata"
xctest=""
for bundle in "$bin"/*PackageTests.xctest; do
if [ -d "$bundle" ]; then
# macOS: a bundle with the executable inside.
name="$(basename "$bundle" .xctest)"
xctest="$bundle/Contents/MacOS/$name"
elif [ -f "$bundle" ]; then
# Linux: the executable itself.
xctest="$bundle"
fi
done
if [ -z "$xctest" ] || [ ! -f "$profdata" ]; then
echo "swift-test: no test bundle or coverage profile produced; skipping the LCOV export"
exit 0
fi
llvm-cov export -format=lcov -instr-profile="$profdata" \
-ignore-filename-regex='\.build/|/Tests/' "$xctest" >coverage/lcov.info
echo "swift-test: coverage written to coverage/lcov.info"
archetypes/swift/scripts/build.sh
#!/bin/sh
# Copyright 2026 BitWise Media Group Ltd
# SPDX-License-Identifier: MIT
#
# Release build of the package: every product, or the one the swift_product
# knob (arriving as TOOLCHAIN_SWIFT_PRODUCT) names. SWIFT_PRODUCT in the
# environment overrides the knob per invocation (`make build
# SWIFT_PRODUCT=mytool`). Prints where the binaries landed.
#
# No version stamping: Swift has no -X/-ldflags equivalent. A repo that wants
# its version in the binary generates a source file in a repo-local task that
# runs before `build`.
set -eu
product="${SWIFT_PRODUCT:-${TOOLCHAIN_SWIFT_PRODUCT:-}}"
if [ -n "$product" ]; then
swift build -c release --product "$product"
else
swift build -c release
fi
echo "swift-build: binaries in $(swift build -c release --show-bin-path)"
archetypes/swift/scripts/goreleaser-build.sh
#!/bin/sh
# Copyright 2026 BitWise Media Group Ltd
# SPDX-License-Identifier: MIT
#
# GoReleaser `tool` shim: lets a repo's .goreleaser.yaml drive `swift build`
# through the go builder, since GoReleaser OSS has no Swift builder.
# GoReleaser runs it as `<tool> build [go flags] -o <out> <main>` with GOOS and
# GOARCH set for the target; the shim maps that pair to a Swift target, builds
# the product in release mode, and copies the binary to <out>. Go-only flags
# (-ldflags, -trimpath, -tags, …) and <main> are ignored.
#
# builds:
# - builder: go
# tool: .mise/archetypes/swift/scripts/goreleaser-build.sh
# no_main_check: true # there is no Go main package to find
# env: [SWIFT_PRODUCT=mytool]
# goos: [darwin]
# goarch: [arm64, amd64]
#
# The product comes from SWIFT_PRODUCT (the build's env) or the swift_product
# knob (TOOLCHAIN_SWIFT_PRODUCT, set by the snapshot/release tasks).
#
# darwin/arm64 --triple arm64-apple-macosx
# darwin/amd64 --triple x86_64-apple-macosx
# linux/amd64 --swift-sdk x86_64-swift-linux-musl (Static Linux SDK)
# linux/arm64 --swift-sdk aarch64-swift-linux-musl (Static Linux SDK)
#
# The Linux targets build fully static binaries with the Static Linux SDK
# matching the pinned toolchain (`swift sdk install …`, see swift.org); they
# work from a macOS or a Linux host alike.
set -eu
out=""
while [ $# -gt 0 ]; do
case "$1" in
-o)
out="${2-}"
if [ $# -gt 1 ]; then shift; fi
shift
;;
-o=*)
out="${1#-o=}"
shift
;;
*) shift ;;
esac
done
if [ -z "$out" ]; then
echo "goreleaser-build.sh: no -o <output> argument (run it as a GoReleaser build tool)" >&2
exit 2
fi
product="${SWIFT_PRODUCT:-${TOOLCHAIN_SWIFT_PRODUCT:-}}"
if [ -z "$product" ]; then
echo "goreleaser-build.sh: no product — set SWIFT_PRODUCT in the build's env or the swift_product knob" >&2
exit 2
fi
target="${GOOS:-}/${GOARCH:-}"
case "$target" in
darwin/arm64) set -- --triple arm64-apple-macosx ;;
darwin/amd64) set -- --triple x86_64-apple-macosx ;;
linux/amd64) set -- --swift-sdk x86_64-swift-linux-musl ;;
linux/arm64) set -- --swift-sdk aarch64-swift-linux-musl ;;
*)
echo "goreleaser-build.sh: unsupported target $target (darwin|linux × arm64|amd64)" >&2
exit 2
;;
esac
echo "==> swift build $product ($target)"
swift build -c release "$@" --product "$product"
bin="$(swift build -c release "$@" --show-bin-path)"
mkdir -p "$(dirname "$out")"
cp "$bin/$product" "$out"