# Swift Package Manager Workflow on a Remote Mac

Canonical: https://figment.so/blog/mac-swift-package-manager-remote

Run swift package resolve, swift build and swift test over SSH; none needs a window server. Any Figment Mac plan works: the Intel Mac VM ($25 a month) covers Xcode 26.3 or earlier, Apple silicon plans cover newer toolchains.

**Run `swift package resolve`, `swift build` and `swift test` over SSH on the rented Mac, exactly as you would on your laptop.** Every `swift` subcommand (`build`, `test`, `package resolve`, `package describe`) is a plain command-line tool with no window-server dependency, so a Linux-first library author only needs the Mac for the Apple-platform test passes. The references are the [Swift Package Manager documentation](https://www.swift.org/documentation/package-manager/) and Apple's [Adding package dependencies to your app](https://developer.apple.com/documentation/xcode/adding-package-dependencies-to-your-app). The Makefile below assumes SSH is configured for your host.

## Which SPM commands work the same over SSH?

All of these:

| Command | What it does |
|---|---|
| `swift package resolve` | Fetches and pins all dependencies to `Package.resolved` |
| `swift build` | Compiles all targets in debug configuration |
| `swift build -c release` | Compiles with optimizations into `.build/release/` |
| `swift test` | Builds and runs all test targets |
| `swift test --parallel` | Runs test cases in parallel across available cores |
| `swift test --filter TestSuiteName` | Runs only the named suite or test method |
| `swift package describe --type json` | Prints the package graph in JSON |
| `swift package show-dependencies` | Prints the resolved dependency tree |

`swift package generate-xcodeproj` was removed from recent Swift toolchains. Modern Xcode opens `Package.swift` directly, so a pure SPM package does not need a generated project.

## How do I resolve dependencies and keep the .build cache?

Run `swift package resolve` once per toolchain; it fetches each dependency from its Git URL and writes `Package.resolved`, and later builds reuse the resolved graph.

```bash
ssh user@YOUR_MAC_HOST "cd ~/projects/MyLibrary && swift package resolve"
```

Resolved sources and compiled artifacts both live in `.build/`. On a Mac that stays allocated, the directory survives between sessions, so incremental builds recompile only changed sources. To move the cache to another host, archive it after a clean build:

```bash
# Save on the current Mac
ssh user@OLD_HOST "cd ~/projects/MyLibrary && tar czf ~/build-cache.tar.gz .build"
scp user@OLD_HOST:~/build-cache.tar.gz ~/build-caches/MyLibrary-$(date +%Y%m%d).tar.gz

# Restore on a new Mac before the next build
scp ~/build-caches/MyLibrary-20261110.tar.gz user@NEW_HOST:~/build-cache.tar.gz
ssh user@NEW_HOST "cd ~/projects/MyLibrary && tar xzf ~/build-cache.tar.gz"
```

A restored `.build` is tied to a specific Swift toolchain and target triple. If you upgrade Xcode or move between Intel and arm64 Macs, run `swift package clean` first, or expect spurious link errors. A persistent `.build` is a cache, not a backup: keep your source in Git.

## How do I run tests and capture JUnit XML?

`swift test` streams human-readable results to stdout, and recent toolchains also accept `--xunit-output` for CI systems that read JUnit XML. Confirm the flag with `swift test --help` on your Mac, since it varies by Swift release:

```bash
ssh user@YOUR_MAC_HOST \
  "cd ~/projects/MyLibrary && swift test --parallel --xunit-output /tmp/results.xml"

scp user@YOUR_MAC_HOST:/tmp/results.xml ~/ci-results/macos-$(date +%Y%m%d).xml
```

If your toolchain does not expose `--xunit-output`, pipe the verbose output through `xcpretty`:

```bash
ssh user@YOUR_MAC_HOST \
  "cd ~/projects/MyLibrary && set -o pipefail; swift test --verbose 2>&1" \
  | xcpretty --report junit --output ~/ci-results/macos.xml
```

The remote `set -o pipefail` matters: without it a failing `swift test` can be masked by a successful `xcpretty` exit, and a red build arrives looking green.

## What does a Makefile for remote targets look like?

Override `REMOTE_HOST` and `REMOTE_DIR` on the command line. The exit code of `swift test` propagates back through the pipe, so `make` fails when tests fail.

```makefile
# Makefile — remote Swift testing on a rented Mac
# Usage:
#   make test-macos REMOTE_HOST=user@mac.example.com REMOTE_DIR=/Users/user/projects/MyLibrary

REMOTE_HOST ?= user@YOUR_MAC_HOST
REMOTE_DIR  ?= ~/projects/MyLibrary
LOCAL_SRC   := $(shell git rev-parse --show-toplevel)
SSH         := ssh -o StrictHostKeyChecking=accept-new

.PHONY: sync test-macos build-macos clean-macos sync-build-cache

# Push local source; exclude .build so the remote cache stays intact
sync:
	rsync -avz --delete-excluded \
	  --exclude '.build' --exclude '.git' \
	  "$(LOCAL_SRC)/" "$(REMOTE_HOST):$(REMOTE_DIR)/"

# Resolve, build, and test on the remote Mac; stream output live
test-macos: sync
	$(SSH) $(REMOTE_HOST) \
	  "bash -c 'set -euo pipefail; cd $(REMOTE_DIR) && \
	   swift package resolve && \
	   swift test --parallel --verbose 2>&1 | tee /tmp/swift-test-output.txt'"

# Release build without running tests
build-macos: sync
	$(SSH) $(REMOTE_HOST) \
	  "bash -c 'set -euo pipefail; cd $(REMOTE_DIR) && swift build -c release'"

# Archive the .build directory for reuse on another host
sync-build-cache:
	$(SSH) $(REMOTE_HOST) \
	  "cd $(REMOTE_DIR) && tar czf ~/build-cache.tar.gz .build"
	mkdir -p "$(LOCAL_SRC)/build-caches"
	scp "$(REMOTE_HOST):~/build-cache.tar.gz" \
	  "$(LOCAL_SRC)/build-caches/macos-$(shell date +%Y%m%d).tar.gz"

clean-macos:
	$(SSH) $(REMOTE_HOST) \
	  "cd $(REMOTE_DIR) && swift package clean"
```

The `bash -c 'set -euo pipefail; ...'` wrapper calls bash explicitly so `pipefail` behaves the same whatever the remote account's login shell is. zsh has been the macOS default since Catalina, and its handling of `pipefail` inside one-shot SSH commands varies, so calling bash keeps the test exit code deterministic. This is untested reference code: run it against your own project before using it in CI.

## How do I keep the .build cache useful?

Three habits:

1. **Sync incrementally.** `rsync` transfers only changed files, so a one-line commit does not re-upload the whole package.
2. **Pin the toolchain.** Use a `.swift-version` file or `xcode-select -s` so the same compiler produces every artifact. A mismatch silently invalidates the cache.
3. **Keep debug and release separate.** `swift build` writes to `.build/debug/` and `swift build -c release` writes to `.build/release/`, so you can hold both without clobbering either.

## What about repositories with both Package.swift and an Xcode project?

Drive dependency management from the SPM CLI, and `xcodebuild` picks up the result. `xcodebuild` resolves SPM dependencies at build time and caches them in DerivedData, but the SPM CLI still manages them:

```bash
swift package show-dependencies
swift package update SomeDependency
git add Package.resolved && git commit -m "chore: bump SomeDependency"
```

`xcodebuild` uses the new `Package.resolved` on its next invocation, with no project-file edit. For library authors chasing Linux and macOS parity, pair this SSH workflow with a Linux runner on the same commit: a test that passes on both without `#if canImport(Darwin)` guards is a credible cross-platform signal.

## What Mac builds your Swift packages?

The [Intel Mac VM](/mac-hosting#intel-vm) ($25 a month, $2 a day, 4 shared vCPUs, 4GB RAM) runs macOS Sequoia with Xcode 26.3 or earlier, which covers pure Swift libraries and most macOS-framework code. Choose the [Apple Silicon Mac VM](/mac-hosting#apple-silicon-vm) ($39 a month, 4GB RAM) for Xcode 26.4 or later, Xcode 27 or arm64-only packages, and check the macOS version shown before purchase against Apple's Xcode table. VM vCPUs are shared, so time your own project on a day plan before you commit to a month.

**Related guides:** [Build Your First iOS App on a Rented Mac Mini](/blog/mac-mini-hosting-xcode-first-build) · [iOS SDK Upgrade on a Remote Mac](/blog/mac-ios-sdk-upgrade-remote-mac) · [GitHub Actions Self-Hosted Runner on Mac](/blog/mac-github-actions-self-hosted-runner)
