Skip to content

Commit d2a9630

Browse files
authored
Enable cross-import overlays in test targets. (#8158)
This PR enables cross-import overlays when building test targets. Swift Testing cannot link to most modules because it would/could introduce circular dependencies that prevent those modules from being tested using Swift Testing. However, we need to introduce some API that uses types from Foundation and other modules, which means we need to use the cross-import overlays feature supported by the compiler. Cross-import overlays haven't been through Swift Evolution, so we don't want to enable the feature in the general case. We talked with @beccadax who agreed that because our use case is relatively unique, enabling these overlays just for test targets was probably the right approach until/unless the feature can be fully reviewed and supported. We're also looking at consulting available information (the dependency scanner) to determine if there is any dependency on Swift Testing. If there isn't one, we don't need this flag. That's a "future direction" for this PR at any rate.
1 parent 49c9fed commit d2a9630

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed

Sources/Build/BuildDescription/SwiftModuleBuildDescription.swift

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -982,7 +982,15 @@ public final class SwiftModuleBuildDescription {
982982
if self.isTestTarget {
983983
// test targets must be built with -enable-testing
984984
// since its required for test discovery (the non objective-c reflection kind)
985-
return ["-enable-testing"]
985+
var result = ["-enable-testing"]
986+
987+
// Test targets need to enable cross-import overlays because Swift
988+
// Testing cannot directly link to most other modules and needs to
989+
// provide API that works with e.g. Foundation. (Developers can
990+
// override this flag by passing -disable-cross-import-overlays.)
991+
result += ["-Xfrontend", "-enable-cross-import-overlays"]
992+
993+
return result
986994
} else if self.buildParameters.enableTestability {
987995
return ["-enable-testing"]
988996
} else {

Tests/BuildTests/BuildPlanTests.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2337,6 +2337,8 @@ final class BuildPlanTests: XCTestCase {
23372337
"-enable-batch-mode",
23382338
"-Onone",
23392339
"-enable-testing",
2340+
"-Xfrontend",
2341+
"-enable-cross-import-overlays",
23402342
.equal(self.j),
23412343
"-DSWIFT_PACKAGE",
23422344
"-DDEBUG",

0 commit comments

Comments
 (0)