|
| 1 | +# SwiftPM @testable build setting |
| 2 | + |
| 3 | +* Proposal: [SE-0000](0000-swiftpm-testable-build-setting.md) |
| 4 | +* Authors: [Jake Petroules](https://github.com/jakepetroules) |
| 5 | +* Review Manager: TBD |
| 6 | +* Status: **Awaiting review** |
| 7 | +* Implementation: [swiftlang/swift-package-manager#8004](https://github.com/swiftlang/swift-package-manager/pull/8004) |
| 8 | +* Review: ([pitch](https://forums.swift.org/t/pitch-swiftpm-testable-build-setting/75084)) |
| 9 | + |
| 10 | +## Introduction |
| 11 | + |
| 12 | +The current Swift Package Manager build system is currently hardcoded to pass the `-enable-testing` flag to the Swift compiler to enable `@testable import` when building in debug mode. |
| 13 | + |
| 14 | +Swift-evolution thread: [Pitch: [SwiftPM] @testable build setting](https://forums.swift.org/t/pitch-swiftpm-testable-build-setting/75084) |
| 15 | + |
| 16 | +## Motivation |
| 17 | + |
| 18 | +Not all targets in a given package make use of the `@testable import` feature (or wish to use it at all), but all targets are presently forced to build their code with this support enabled regardless of whether it's needed. |
| 19 | + |
| 20 | +Developers should be able to disable `@testable import` when it's not needed or desired, just as they're able to do so in Xcode's build system. |
| 21 | + |
| 22 | +On Windows in particular, where a shared library is limited to 65k exported symbols, disabling `@testable import` provides developers an option to significantly reduce the exported symbol count of a library by hiding all of the unnecessary internal APIs. It can also improve debug build performance as fewer symbols exported from a binary can result in faster linking. |
| 23 | + |
| 24 | +## Proposed solution |
| 25 | + |
| 26 | +Add a new Swift target setting API to specify whether testing should be enabled for the specified target, falling back to the current behavior by default. |
| 27 | + |
| 28 | +## Detailed design |
| 29 | + |
| 30 | +Add a new `enableTesting` API to `SwiftSetting` limited to manifests >= 6.1: |
| 31 | + |
| 32 | +```swift |
| 33 | +public struct SwiftSetting { |
| 34 | + // ... other settings |
| 35 | + |
| 36 | + @available(_PackageDescription, introduced: 6.1) |
| 37 | + public static func enableTesting( |
| 38 | + _ enable: Bool, |
| 39 | + _ condition: BuildSettingCondition? = nil |
| 40 | + ) -> SwiftSetting { |
| 41 | + ... |
| 42 | + } |
| 43 | +} |
| 44 | +``` |
| 45 | + |
| 46 | +The existing `--enable-testable-imports` / `--disable-testable-imports` command line flag to `swift-test` currently defaults to `--enable-testable-imports`. It will be changed to default to "unspecified" (respecting any target settings), and explicitly passing `--enable-testable-imports` or `--disable-testable-imports` will force all targets to enable or disable testing, respectively. |
| 47 | + |
| 48 | +## Security |
| 49 | + |
| 50 | +New language version setting has no implications on security, safety or privacy. |
| 51 | + |
| 52 | +## Impact on existing packages |
| 53 | + |
| 54 | +Since this is a new API, all existing packages will use the default behavior - testing will be enabled when building for the debug configuration, and disabled when building for the release configuration. |
| 55 | + |
| 56 | +## Alternatives considered |
| 57 | + |
| 58 | +None. |
0 commit comments