Skip to content

WIP: Implement SE-0455: SwiftPM @testable build setting #8004

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -979,6 +979,8 @@ public final class SwiftModuleBuildDescription {

/// Testing arguments according to the build configuration.
private var testingArguments: [String] {
let enableTesting: Bool

if self.isTestTarget {
// test targets must be built with -enable-testing
// since its required for test discovery (the non objective-c reflection kind)
Expand All @@ -991,11 +993,27 @@ public final class SwiftModuleBuildDescription {
result += ["-Xfrontend", "-enable-cross-import-overlays"]

return result
} else if self.buildParameters.enableTestability {
return ["-enable-testing"]
} else if let enableTestability = self.buildParameters.testingParameters.explicitlyEnabledTestability {
// Let the command line flag override
enableTesting = enableTestability
} else {
return []
// Use the target settings
let enableTestabilitySetting = self.buildParameters.createScope(for: self.target).evaluate(.ENABLE_TESTABILITY)
if !enableTestabilitySetting.isEmpty {
enableTesting = enableTestabilitySetting.contains(where: { $0 == "YES" })
} else {
// By default, decide on testability based on debug/release config
// the goals of this being based on the build configuration is
// that `swift build` followed by a `swift test` will need to do minimal rebuilding
// given that the default configuration for `swift build` is debug
// and that `swift test` requires building with testable enabled if @testable is being used.
// when building and testing in release mode, one can use the '--disable-testable-imports' flag
// to disable testability in `swift test`, but that requires that the tests do not use the @testable imports feature
enableTesting = self.buildParameters.configuration == .debug
}
}

return enableTesting ? ["-enable-testing"] : []
}

/// Module cache arguments.
Expand Down
4 changes: 2 additions & 2 deletions Sources/Commands/SwiftTestCommand.swift
Original file line number Diff line number Diff line change
Expand Up @@ -189,8 +189,8 @@ struct TestCommandOptions: ParsableArguments {
var xUnitOutput: AbsolutePath?

/// Generate LinuxMain entries and exit.
@Flag(name: .customLong("testable-imports"), inversion: .prefixedEnableDisable, help: "Enable or disable testable imports. Enabled by default.")
var enableTestableImports: Bool = true
@Flag(name: .customLong("testable-imports"), inversion: .prefixedEnableDisable, help: "Enable or disable testable imports. Based on target settings by default.")
var enableTestableImports: Bool?

/// Whether to enable code coverage.
@Flag(name: .customLong("code-coverage"),
Expand Down
16 changes: 16 additions & 0 deletions Sources/PackageDescription/BuildSettings.swift
Original file line number Diff line number Diff line change
Expand Up @@ -440,6 +440,22 @@ public struct SwiftSetting: Sendable {
return SwiftSetting(
name: "swiftLanguageMode", value: [.init(describing: mode)], condition: condition)
}

/// Whether `@testable` is enabled by passing the `-enable-testing` to the Swift compiler.
///
/// - Since: First available in PackageDescription 6.2.
///
/// - Parameters:
/// - enable: Whether to enable `@testable`.
/// - condition: A condition that restricts the application of the build setting.
@available(_PackageDescription, introduced: 6.2)
public static func enableTestableImport(
_ enable: Bool,
_ condition: BuildSettingCondition? = nil
) -> SwiftSetting {
return SwiftSetting(
name: "enableTestableImport", value: [.init(describing: enable)], condition: condition)
}
}

/// A linker build setting.
Expand Down
14 changes: 14 additions & 0 deletions Sources/PackageLoading/ManifestJSONParser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -554,6 +554,20 @@ extension TargetBuildSettingDescription.Kind {
}

return .swiftLanguageMode(version)
case "enableTestableImport":
guard let rawVersion = values.first else {
throw InternalError("invalid (empty) build settings value")
}

if values.count > 1 {
throw InternalError("invalid build settings value")
}

guard let value = Bool(rawVersion) else {
throw InternalError("invalid boolean value: \(rawVersion)")
}

return .enableTestableImport(value)
default:
throw InternalError("invalid build setting \(name)")
}
Expand Down
10 changes: 10 additions & 0 deletions Sources/PackageLoading/PackageBuilder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1016,6 +1016,12 @@ public final class PackageBuilder {
}
}

for setting in manifestTarget.settings {
if case let .enableTestableImport(enable) = setting.kind, enable, setting.condition?.config == "release" {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I might need to check for config==nil as well

self.observabilityScope.emit(warning: "'\(potentialModule.name)' should not enable `@testable import` when building in release mode")
}
}

// Create and return the right kind of target depending on what kind of sources we found.
if sources.hasSwiftSources {
return try SwiftModule(
Expand Down Expand Up @@ -1223,6 +1229,10 @@ public final class PackageBuilder {
}

values = [version.rawValue]

case .enableTestableImport(let enable):
decl = .ENABLE_TESTABILITY
values = enable ? ["YES"] : ["NO"]
}

// Create an assignment for this setting.
Expand Down
2 changes: 2 additions & 0 deletions Sources/PackageModel/BuildSettings.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
public enum BuildSettings {
/// Build settings declarations.
public struct Declaration: Hashable {
public static let ENABLE_TESTABILITY: Declaration = .init("ENABLE_TESTABILITY")

// Swift.
public static let SWIFT_ACTIVE_COMPILATION_CONDITIONS: Declaration =
.init("SWIFT_ACTIVE_COMPILATION_CONDITIONS")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,15 @@ public enum TargetBuildSettingDescription {

case swiftLanguageMode(SwiftLanguageVersion)

case enableTestableImport(Bool)

public var isUnsafeFlags: Bool {
switch self {
case .unsafeFlags(let flags):
// If `.unsafeFlags` is used, but doesn't specify any flags, we treat it the same way as not specifying it.
return !flags.isEmpty
case .headerSearchPath, .define, .linkedLibrary, .linkedFramework, .interoperabilityMode,
.enableUpcomingFeature, .enableExperimentalFeature, .swiftLanguageMode:
.enableUpcomingFeature, .enableExperimentalFeature, .swiftLanguageMode, .enableTestableImport:
return false
}
}
Expand Down
8 changes: 8 additions & 0 deletions Sources/PackageModel/ManifestSourceGeneration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -534,6 +534,12 @@ fileprivate extension SourceCodeFragment {
params.append(SourceCodeFragment(from: condition))
}
self.init(enum: setting.kind.name, subnodes: params)
case .enableTestableImport(let enable):
params.append(SourceCodeFragment(boolean: enable))
if let condition = setting.condition {
params.append(SourceCodeFragment(from: condition))
}
self.init(enum: setting.kind.name, subnodes: params)
}
}
}
Expand Down Expand Up @@ -688,6 +694,8 @@ extension TargetBuildSettingDescription.Kind {
return "enableExperimentalFeature"
case .swiftLanguageMode:
return "swiftLanguageMode"
case .enableTestableImport:
return "enableTestableImport"
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,18 +105,6 @@ extension BuildParameters {
}
}

/// Whether building for testability is enabled.
public var enableTestability: Bool {
// decide on testability based on debug/release config
// the goals of this being based on the build configuration is
// that `swift build` followed by a `swift test` will need to do minimal rebuilding
// given that the default configuration for `swift build` is debug
// and that `swift test` normally requires building with testable enabled.
// when building and testing in release mode, one can use the '--disable-testable-imports' flag
// to disable testability in `swift test`, but that requires that the tests do not use the testable imports feature
self.testingParameters.explicitlyEnabledTestability ?? (self.configuration == .debug)
}

/// The style of test product to produce.
public var testProductStyle: TestProductStyle {
return triple.isDarwin() ? .loadableBundle : .entryPointExecutable(
Expand Down
8 changes: 0 additions & 8 deletions Sources/XCBuildSupport/PIFBuilder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,6 @@ struct PIFBuilderParameters {
/// Whether the toolchain supports `-package-name` option.
let isPackageAccessModifierSupported: Bool

/// Whether or not build for testability is enabled.
let enableTestability: Bool

/// Whether to create dylibs for dynamic library products.
let shouldCreateDylibForDynamicProducts: Bool

Expand Down Expand Up @@ -343,7 +340,6 @@ final class PackagePIFProjectBuilder: PIFProjectBuilder {
debugSettings[.GCC_OPTIMIZATION_LEVEL] = "0"
debugSettings[.ONLY_ACTIVE_ARCH] = "YES"
debugSettings[.SWIFT_OPTIMIZATION_LEVEL] = "-Onone"
debugSettings[.ENABLE_TESTABILITY] = "YES"
debugSettings[.SWIFT_ACTIVE_COMPILATION_CONDITIONS, default: []].append("DEBUG")
debugSettings[.GCC_PREPROCESSOR_DEFINITIONS, default: ["$(inherited)"]].append("DEBUG=1")
addBuildConfiguration(name: "Debug", settings: debugSettings)
Expand All @@ -354,10 +350,6 @@ final class PackagePIFProjectBuilder: PIFProjectBuilder {
releaseSettings[.GCC_OPTIMIZATION_LEVEL] = "s"
releaseSettings[.SWIFT_OPTIMIZATION_LEVEL] = "-Owholemodule"

if parameters.enableTestability {
releaseSettings[.ENABLE_TESTABILITY] = "YES"
}

addBuildConfiguration(name: "Release", settings: releaseSettings)

for product in package.products.sorted(by: { $0.name < $1.name }) {
Expand Down
1 change: 0 additions & 1 deletion Sources/XCBuildSupport/XcodeBuildSystem.swift
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,6 @@ extension PIFBuilderParameters {
self.init(
triple: buildParameters.triple,
isPackageAccessModifierSupported: buildParameters.driverParameters.isPackageAccessModifierSupported,
enableTestability: buildParameters.enableTestability,
shouldCreateDylibForDynamicProducts: buildParameters.shouldCreateDylibForDynamicProducts,
toolchainLibDir: (try? buildParameters.toolchain.toolchainLibDir) ?? .root,
pkgConfigDirectories: buildParameters.pkgConfigDirectories,
Expand Down