All options to specify a Swift package version

Photo by Jiawei Zhao on Unsplash

All options to specify a Swift package version

ยท

3 min read

How you specify a remote package dependency determines if and how updates are resolved. Learn what the recommended way is and why. Even a daring option that lets you take any updates and skip several major versions.

Remote packages can be used as dependencies for an Xcode project or Swift package. All the listed functions below assume that you declare a package dependency in your Swift package manifest. Still, the same rules are also available when declaring a package dependency in your Xcode project.

Up to the next major version

Apple's recommended way to specify a remote package dependency. ๐Ÿ‘

It allows you to specify the minimum version you require, allows updates that include bug fixes and backward-compatible feature updates, but requires you to explicitly update to a new major version of the dependency.

This approach provides the maximum flexibility on which version to use, while making sure you donโ€™t update to a version with breaking changes, and helps to prevent conflicts in your dependency graph.

// Adds a package dependency that uses the version requirement, starting with the given minimum version, going up to the next major version.
.package(url: "https://example.com/example-package.git", from: "1.2.3")

Example can resolve into a selected version like 1.2.3, 1.2.4, or 1.3.0, but not 2.0.0.

Up to but not including a specified maximum version

// Adds a package dependency starting with a specific minimum version, up to but not including a specified maximum version.
.package(url: "https://example.com/example-package.git", "1.2.3"..<"2.3.6")

Example can resolve into a selected version like 1.2.3, 1.2.4, 1.2.5, ..., 2.0.0, ..., 2.3.5 but not 2.3.6.

โš ๏ธ Specifying a wide range allows the daring developer to uptake multiple or even any major version. Use with extreme caution! ๐Ÿ’ฃ

.package(url: "https://example.com/example-package.git", "2.0.0"..<"999.999.999")

Up to and including a specific maximum version

// Adds a package dependency starting with a specific minimum version, going up to and including a specific maximum version.
.package(url: "https://example.com/example-package.git", "1.2.6"..."2.3.6")

Example can resolve into a selected version like 1.2.3, 1.2.4, 1.2.5, ..., 2.0.0, ..., 2.3.5 as well as 2.3.6.

Exact version

Specifying exact version requirements are not recommended as they can cause conflicts in your dependency graph when multiple other packages depend on a package. As Swift packages follow the semantic versioning convention, think about specifying a version range instead.

// Adds a package dependency that uses the exact version requirement.
.package(url: "https://example.com/example-package.git", exact: "1.2.3")

Exact Revision

Note that packages that use commit-based dependency requirements canโ€™t be depended upon by packages that use version-based dependency requirements; you should remove commit-based dependency requirements before publishing a version of your package.

// Adds a remote package dependency given a revision requirement.
.package(url: "https://example.com/example-package.git", revision: "6297dc937f209f9d28f2c71f1c2623b20312648d")

Branch

Note that packages that use branch-based dependency requirements canโ€™t be depended upon by packages that use version-based dependency requirements; you should remove branch-based dependency requirements before publishing a version of your package.

// Adds a remote package dependency given a branch requirement.
.package(url: "https://example.com/example-package.git, branch: "main")

Did you find this article valuable?

Support Marco Eidinger by becoming a sponsor. Any amount is appreciated!