Versioning, Resolving Conflicts and Swift Plugins
Managing version ranges, resolving dependency conflicts and using build tool plugins.
Versioning, Resolving Conflicts and Swift Plugins is a free Swift Academy lesson on CoddyKit. This is lesson 4 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the Swift Academy learning path, and your progress syncs across the web and the CoddyKit app. The Swift Academy course includes 4 lessons in total.
Semantic Versioning in SPM
SPM uses semver: MAJOR.MINOR.PATCH. Declare requirements with from:, exact:, or .upToNextMajor.
.package(url: "https://github.com/apple/swift-log", from: "1.5.0"),
.package(url: "https://github.com/vapor/vapor", exact: "4.83.0"),
.package(url: "https://github.com/pointfreeco/swift-composable-architecture",
.upToNextMajor(from: "1.0.0"))Package.resolved
Package.resolved pins exact versions for all dependencies. Commit it for apps; omit it for libraries.
// Package.resolved (auto-generated):
// Pins exact commit hash and version for every dependency
// Run "swift package update" to update pinned versionsResolving Version Conflicts
When two packages require different versions of the same dependency, SPM tries to find a compatible version. Conflicts that cannot be resolved cause a build error.
// PackageA requires Logging 1.4.0
// PackageB requires Logging 1.5.0
// SPM resolves to 1.5.0 (satisfies both >= 1.x)
// Conflict: PackageA requires Logging 1.x, PackageB requires 2.xForcing a Version
Add the conflicting dependency directly to your app's Package.swift to override the version that SPM would otherwise choose.
// Force Logging 1.5.0 by declaring it directly:
.package(url: "https://github.com/apple/swift-log", exact: "1.5.0")Build Tool Plugins
Build tool plugins run during the build to generate source code, process resources, or run linters automatically.
// In Package.swift:
.plugin(
name: "SourceGenerator",
capability: .buildTool()
)Implementing a Build Tool Plugin
A build tool plugin conforms to BuildToolPlugin and returns BuildCommands or PrebuildCommands.
import PackagePlugin
@main
struct SourceGenerator: BuildToolPlugin {
func createBuildCommands(context: PluginContext, target: Target) async throws -> [Command] {
let output = context.pluginWorkDirectory.appending("Generated.swift")
return [.buildCommand(
displayName: "Generating sources",
executable: try context.tool(named: "gen-tool").path,
arguments: ["--output", output],
outputFiles: [output]
)]
}
}Command Plugins
Command plugins run on demand from Xcode or the CLI, useful for formatting, linting, or generating documentation.
// In Package.swift:
.plugin(
name: "FormatPlugin",
capability: .command(intent: .sourceCodeFormatting())
)Running a Command Plugin
Execute command plugins from the terminal or Xcode right-click menu.
// CLI:
swift package plugin format-source-code
// Xcode: right-click target → "Format Source Code"Using SwiftLint as a Build Plugin
SwiftLint ships as an SPM plugin that runs automatically on every build.
.package(url: "https://github.com/realm/SwiftLint", from: "0.54.0"),
// In target:
.plugin(name: "SwiftLintBuildToolPlugin", package: "SwiftLint")Plugin Sandbox
Plugins run in a sandbox with limited file system access for security. They can only write to their designated work directory.
// Accessible from plugins:
// context.pluginWorkDirectory — writable work dir
// context.package.directory — read-only source
// target.sourceFiles — source file listQuick Check
What is the purpose of Package.resolved in a Swift package?
Lesson Recap
Use semver ranges in Package.swift. Commit Package.resolved for apps for reproducible builds. Resolve version conflicts by declaring the dependency directly. Build tool plugins auto-run during build (linters, code generators). Command plugins run on demand. Plugins are sandboxed for security.
Learn Swift with an AI tutor — free
Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.
- Courses
- 122
- Lessons
- 409
Frequently Asked Questions
Is the “Versioning, Resolving Conflicts and Swift Plugins” lesson free?
Yes — any 3 lessons of the Swift Academy learning path, including “Versioning, Resolving Conflicts and Swift Plugins”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The Swift Academy course includes 4 lessons in total.
What will I learn in “Versioning, Resolving Conflicts and Swift Plugins”?
Managing version ranges, resolving dependency conflicts and using build tool plugins. You practise Swift Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Swift Academy?
No prior experience is required. Swift Academy on CoddyKit is structured for beginners through advanced learners, so you can start here or from the beginning and move at your own pace. This is lesson 4 of 4.
How long does the “Versioning, Resolving Conflicts and Swift Plugins” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Swift Academy lesson?
Yes. Every Swift Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Package.swift: Targets, Products and Dependencies
- Local Packages and Feature Modules
- Binary Targets and XCFrameworks
- Versioning, Resolving Conflicts and Swift Plugins