Treating Warnings As Errors In Swift Packages

In Swift 6.2, Swift Packages give us control over which compiler warnings to treat as errors.

Finer-grained Controls for Compiler Warnings

The Swift compiler has options to treat all warnings as errors or suppress all warnings:

-warnings-as-errors
-suppress-warnings

In Xcode, you add those compiler flags to the build settings for a target. In the “Swift Compiler - Custom Flags” section add the options to the “Other Swift Flags” setting:

Other Swift Flags set to -warnings-as-errors

What if you only want to treat some warnings as errors?

In Swift 6.1, compiler options allow you to treat a specific diagnostic groups as warnings or errors:

-Wwarning <group>
-Werror <group>

For example, to treat the DeprecatedDeclaration diagnostic group as an error:

-Werror DeprecatedDeclaration

The order you add the controls is important. For example, if we default to treating all warnings as errors, but want to keep the DeprecatedDeclaration group as a warning:

-warnings-as-errors
-Wwarning DeprecatedDeclaration

Note that when adding this to the build settings in Xcode you add the diagnostic group on a new line:

-Wwarning and DeprecatedDeclaration on separate lines

Xcode adds a link to the documentation when showing the warning/error in the source code editor:

Xcode warning message that doSomething is deprecated with question mark in a circle

Clicking on the question mark opens a link to the Swift documentation for the relevant diagnostic group.

That’s great. The problem was that Swift 6.1 did not support these controls in Swift Packages. Apple fixed that problem in Swift 6.2 which shipped with Xcode 26.

Warning Controls for Swift Packages

First you need to update your Swift tools version to 6.2 in your Package.swift file:

// swift-tools-version: 6.2

You then have access to some new swiftSettings:

treatAllWarnings(as: .error)
treatAllWarnings(as: .warning)
treatWarning("group", as: .error)
treatWarning("group", as: .warning)

Modifying a target to treat all warnings as errors, except the DeprecatedDeclaration group:

.target(
  name: "MyFeature",
  swiftSettings: [
    .treatAllWarnings(as: .error),
    .treatWarning("DeprecatedDeclaration", as: .warning)
  ]
),

The warning control settings only apply when the package is built directly. They are ignored if the package is used as a remote dependency.

To apply the controls to all targets in a Swift Package. Add the following to the end of the Package.swift file:

for target in package.targets {
  var settings = target.swiftSettings ?? []
  settings.append(contentsOf: [
    .treatAllWarnings(as: .error),
    .treatWarning("DeprecatedDeclaration", as: .warning)
  ])
  target.swiftSettings = settings
}

Learn More