TExcelTAD
Home › Mobile Build Pipelines: Gradle/AGP, Xcode, SwiftPM & Fastlane › SwiftPM Dependencies & Package.resolved

SwiftPM Dependencies & Package.resolved

Version requirements in Package.swift say what's allowed. Package.resolved records what was chosen: the exact version and Git revision of every package in the graph. It is SwiftPM's lockfile, and for apps it belongs in version control, exactly like conan.lock, Cargo.lock or Gradle's dependency locks.

The file

json
{
  "originHash" : "5c4e2a1b9f…",
  "pins" : [
    {
      "identity" : "swift-argument-parser",
      "kind" : "remoteSourceControl",
      "location" : "https://github.com/apple/swift-argument-parser.git",
      "state" : {
        "revision" : "41982a3656a71c768319979febd796c6fd111d5c",
        "version" : "1.5.0"
      }
    },
    {
      "identity" : "swift-collections",
      "kind" : "remoteSourceControl",
      "location" : "https://github.com/apple/swift-collections.git",
      "state" : {
        "revision" : "671108c96644956dddcd89dd59c203dcdb36cec7",
        "version" : "1.1.4"
      }
    }
  ],
  "version" : 3
}
Field Meaning
identity the package's identity (derived from the URL's last path component)
kind remoteSourceControl (Git), localSourceControl, or registry
state.version + state.revision the tag and the commit it pointed to; a re-pointed tag is detected
state.branch for branch dependencies (only the revision is reproducible)
originHash a hash of the requirements that produced the file, so tools can tell whether it's stale
version the file format (3 in current toolchains)

Where it lives:

Project type Path
a Swift package Package.resolved next to Package.swift
an Xcode project Hello.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved
an Xcode workspace Hello.xcworkspace/xcshareddata/swiftpm/Package.resolved

The Xcode paths are easy to forget in .gitignore rules that exclude xcshareddata or *.xcworkspace. If it isn't committed, every CI run resolves afresh, and two builds of the same commit can use different dependency versions.

Resolving, updating, enforcing

bash
swift package resolve                     # resolve using Package.resolved where possible
swift package update                      # re-resolve to the newest allowed versions (rewrites the file)
swift package update swift-collections    # update one package only
swift build --force-resolved-versions     # fail instead of changing Package.resolved (CI)

# Xcode projects
xcodebuild -resolvePackageDependencies -workspace Hello.xcworkspace -scheme Hello
xcodebuild … -onlyUsePackageVersionsFromResolvedFile        # fail if the file is out of date
xcodebuild … -disableAutomaticPackageResolution             # never resolve implicitly
xcodebuild … -clonedSourcePackagesDirPath build/SourcePackages   # put checkouts where CI can cache them

On CI, use the "fail instead of changing" flags: a pull request that changed Package.swift without updating Package.resolved should go red, not silently resolve something nobody reviewed. Updates then happen on purpose, in their own pull request (Renovate and Dependabot both understand Package.resolved).

Detecting drift: a runnable model

A small check you can run in CI or a pre-commit hook: compare two Package.resolved files (say, main and the pull request) and report what changed and whether a version moved backwards or across a major:

python · try it yourself
import json

main = json.loads("""{"pins": [
 {"identity": "swift-argument-parser", "state": {"revision": "41982a3", "version": "1.5.0"}},
 {"identity": "swift-collections",     "state": {"revision": "671108c", "version": "1.1.4"}},
 {"identity": "swift-log",             "state": {"revision": "96a2f8a", "version": "1.6.1"}}],
 "version": 3}""")
pr = json.loads("""{"pins": [
 {"identity": "swift-argument-parser", "state": {"revision": "0fbc884", "version": "1.6.1"}},
 {"identity": "swift-collections",     "state": {"revision": "9e0f1aa", "version": "1.1.4"}},
 {"identity": "swift-numerics",        "state": {"revision": "e0ec0f5", "version": "1.0.3"}},
 {"identity": "swift-log",             "state": {"revision": "b1a2c3d", "version": "2.0.0"}}],
 "version": 3}""")

def pins(doc):
    return {p["identity"]: p["state"] for p in doc["pins"]}

def major(v):
    return int(v.split(".")[0])

old, new = pins(main), pins(pr)
for name in sorted(old.keys() | new.keys()):
    a, b = old.get(name), new.get(name)
    if a is None:
        print(f"ADDED    {name} {b['version']}")
    elif b is None:
        print(f"REMOVED  {name}")
    elif a["version"] != b["version"]:
        flag = "  <-- major upgrade, review the changelog" if major(b["version"]) > major(a["version"]) else ""
        print(f"UPDATED  {name} {a['version']} -> {b['version']}{flag}")
    elif a["revision"] != b["revision"]:
        print(f"RETAGGED {name} {a['version']}: {a['revision']} -> {b['revision']}  <-- same tag, different commit!")
UPDATED  swift-argument-parser 1.5.0 -> 1.6.1
RETAGGED swift-collections 1.1.4: 671108c -> 9e0f1aa  <-- same tag, different commit!
UPDATED  swift-log 1.6.1 -> 2.0.0  <-- major upgrade, review the changelog
ADDED    swift-numerics 1.0.3

The RETAGGED case is why the file stores revisions: a tag that moved to a different commit is either a maintainer's mistake or a supply-chain attack, and either way the build should stop until a human looks.

Private packages, registries, binaries

  • Private Git packages need credentials on CI: an SSH key added to the agent, or HTTPS with a token in ~/.netrc (both swift and xcodebuild read it; xcodebuild also needs -scmProvider system to use the system Git and its credentials).
  • Package registries (SE-0292) serve packages over HTTP instead of Git: swift package-registry set https://packages.example.com and dependencies by ID, e.g. .package(id: "example.core", from: "1.0.0"). Artifactory, Nexus and GitHub Packages support the protocol; checksums go into Package.resolved with kind: registry.
  • Binary targets (.binaryTarget(name:url:checksum:)) download a zipped .xcframework whose SHA-256 must match the manifest (swift package compute-checksum Core.xcframework.zip).
  • CocoaPods (Podfile, Podfile.lock) still powers many older apps, but it's in maintenance mode and its maintainers have announced that the central Trunk repository becomes read-only; new projects use SwiftPM.

Test yourself

Where is Package.resolved for a standalone Xcode project (no workspace)?

  1. Next to Hello.xcodeproj
  2. Hello.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved
  3. ~/Library/Developer/Xcode/DerivedData
  4. It doesn't have one

Which flag makes xcodebuild fail instead of silently re-resolving when Package.resolved is out of date?

  1. -allowProvisioningUpdates
  2. -onlyUsePackageVersionsFromResolvedFile
  3. -derivedDataPath
  4. -skipMacroValidation

Package.resolved shows the same version for a package but a different revision. What does that mean?

  1. Nothing; revisions are cosmetic
  2. The tag now points at a different commit: investigate before building
  3. SwiftPM upgraded the package
  4. The cache is stale