Enabling strict-concurrency across entire package?

with tools version 5.8, we can now turn on strict concurrency checking with

swiftSettings: [.enableUpcomingFeature("StrictConcurrency")]

but this needs to be added to each target in the manifest. can this be applied to all targets in the manifest at once?

1 Like

Not sure if there's a better way but you can create your own extension:

// In `Package.swift`

extension Array where Element == Target {
    func strictConcurrency() -> [Target] {
        map {
            var copy = $0
            copy.swiftSettings = (copy.swiftSettings ?? [])
                + [.enableUpcomingFeature("StrictConcurrency")]
            return copy
        }
    }
}
1 Like
let package = Package(
  // ...
)

for target in package.targets {
  var swiftSettings = target.swiftSettings ?? []
  defer { target.swiftSettings = swiftSettings }

  swiftSettings.append(.enableUpcomingFeature("StrictConcurrency"))

  // And it easily composes with other global things:

  swiftSettings.append(
    .define(
      "APPLE_PLATFORM",
      .when(platforms: [.macOS, .tvOS, .iOS, .watchOS])
    )
  )

  #if os(Windows)
    target.plugins = nil
    swiftSettings.append(
      .define("PLUGINS_UNAVAILABLE")
    )
  #endif
}
2 Likes

wow! i always forget how much we can do in the manifests… if only we could share these scripts across multiple packages…