I'm working on a Swift package and want to lint its build in Xcode without relying on external tools. For Xcode projects, it's possible to integrate swift-format as a build phase to enforce code style and show warnings.
Is there a way to achieve the same for Swift packages using only Xcode or Swift Package Manager, without external dependencies?
Package.swift
import PackageDescription
let package = Package(
name: "MyPackage",
products: [
.library(
name: "MyPackage",
targets: ["MyPackage"]
)
],
targets: [
.target(
name: "MyPackage"
),
]
)
MyPackage.swift
struct MyPackage {
var a: Int
var b: Int
var c: Int
}
The goal is to get a warning when the package is build because of too many spaces before var b: Int
line.
I found a solution to this issue and am sharing my findings:
With Xcode 16, swift-format
is included as part of the Xcode toolchain, eliminating the need for external libraries and making Swift file formatting more convenient.
You can lint your Swift package without third-party dependencies by:
Package.swift
fileswift-format
from Xcode 16When your package builds, Xcode will show warnings for formatting issues like the inconsistent indentation in my example:
struct MyPackage {
var a: Int
var b: Int // This will show a warning in Xcode
var c: Int
}
The warnings are displayed directly in Xcode’s issue navigator during the build process.
For a comprehensive guide with step-by-step instructions and sample code, refer to this article: Linting a Swift Package with swift-format
Note that while the article references adding swift-format
as a dependency, with Xcode 16 you can simplify this approach by using the built-in toolchain version instead.
(Full disclosure: I contributed to this article at SnappMobile.)