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'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.
1 Answer
Reset to default 0I found a solution to this issue and am sharing my findings:
Linting Swift Packages in Xcode 16 Without External Dependencies
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.
Solution Summary
You can lint your Swift package without third-party dependencies by:
- Creating a build tool plugin in your
Package.swift
file - Implementing the plugin to use the built-in
swift-format
from Xcode 16 - Adding the plugin to your target
When 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.)
swift-format
something that can be explored. I'll post my findings. – Oleksii Kolomiiets Commented Feb 11 at 14:39