Skip to content

Commit 6481220

Browse files
authored
[Build] BuildPlan: Always discover test modules through their aggrega… (#7879)
…te products ### Motivation: Fixes a situation when tests were build for both host and target even though they should have been built only for the host (i.e. when one of the modules depends on a macro). ### Modifications: - Adjusted `BuildPlan.computeDestinations` to never add `test` modules to successor list of a package and allow `test` products to always iterate their module dependencies instead. ### Result: If one of the test targets depends on a macro we'd never attempt to build others for "target".
1 parent 2969d44 commit 6481220

File tree

4 files changed

+29
-6
lines changed

4 files changed

+29
-6
lines changed

Sources/Build/BuildPlan/BuildPlan.swift

+4-4
Original file line numberDiff line numberDiff line change
@@ -986,9 +986,9 @@ extension BuildPlan {
986986
}
987987

988988
for module in package.modules {
989-
if case .test = module.underlying.type,
990-
!graph.rootPackages.contains(id: package.id)
991-
{
989+
// Tests are discovered through an aggregate product which also
990+
// informs their destination.
991+
if case .test = module.underlying.type {
992992
continue
993993
}
994994

@@ -1002,7 +1002,7 @@ extension BuildPlan {
10021002
for product: ResolvedProduct,
10031003
destination: Destination
10041004
) -> [TraversalNode] {
1005-
guard destination == .host else {
1005+
guard destination == .host || product.underlying.type == .test else {
10061006
return []
10071007
}
10081008

Sources/_InternalTestSupport/MockPackageGraphs.swift

+6
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ package func macrosTestsPackageGraph() throws -> MockPackageGraph {
137137
"/swift-mmio/Sources/MMIOMacros/source.swift",
138138
"/swift-mmio/Sources/MMIOMacrosTests/source.swift",
139139
"/swift-mmio/Sources/MMIOMacro+PluginTests/source.swift",
140+
"/swift-mmio/Sources/NOOPTests/source.swift",
140141
"/swift-syntax/Sources/SwiftSyntax/source.swift",
141142
"/swift-syntax/Sources/SwiftSyntaxMacrosTestSupport/source.swift",
142143
"/swift-syntax/Sources/SwiftSyntaxMacros/source.swift",
@@ -203,6 +204,11 @@ package func macrosTestsPackageGraph() throws -> MockPackageGraph {
203204
.target(name: "MMIOMacros")
204205
],
205206
type: .test
207+
),
208+
TargetDescription(
209+
name: "NOOPTests",
210+
dependencies: [],
211+
type: .test
206212
)
207213
]
208214
),

Tests/BuildTests/CrossCompilationBuildPlanTests.swift

+7-1
Original file line numberDiff line numberDiff line change
@@ -295,9 +295,15 @@ final class CrossCompilationBuildPlanTests: XCTestCase {
295295
fileSystem: fs,
296296
observabilityScope: scope
297297
)
298+
299+
// Make sure that build plan doesn't have any "target" tests.
300+
for (_, description) in plan.targetMap where description.module.underlying.type == .test {
301+
XCTAssertEqual(description.buildParameters.destination, .host)
302+
}
303+
298304
let result = try BuildPlanResult(plan: plan)
299305
result.checkProductsCount(2)
300-
result.checkTargetsCount(16)
306+
result.checkTargetsCount(17)
301307

302308
XCTAssertTrue(try result.allTargets(named: "SwiftSyntax")
303309
.map { try $0.swift() }

Tests/PackageGraphTests/CrossCompilationPackageGraphTests.swift

+12-1
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,11 @@ final class CrossCompilationPackageGraphTests: XCTestCase {
116116
"SwiftSyntaxMacrosTestSupport",
117117
"SwiftSyntaxMacrosTestSupport"
118118
)
119-
result.check(testModules: "MMIOMacrosTests", "MMIOMacro+PluginTests")
119+
// TODO: NOOPTests are mentioned twice because in the graph they appear
120+
// as if they target both "tools" and "destination", see the test below.
121+
// Once the `buildTriple` is gone, there is going to be only one mention
122+
// left.
123+
result.check(testModules: "MMIOMacrosTests", "MMIOMacro+PluginTests", "NOOPTests", "NOOPTests")
120124
result.checkTarget("MMIO") { result in
121125
result.check(buildTriple: .destination)
122126
result.check(dependencies: "MMIOMacros")
@@ -180,6 +184,13 @@ final class CrossCompilationPackageGraphTests: XCTestCase {
180184
XCTAssertEqual(graph.package(for: result.target)?.identity, .plain("swift-syntax"))
181185
}
182186
}
187+
188+
result.checkTargets("NOOPTests") { results in
189+
XCTAssertEqual(results.count, 2)
190+
191+
XCTAssertEqual(results.filter({ $0.target.buildTriple == .tools }).count, 1)
192+
XCTAssertEqual(results.filter({ $0.target.buildTriple == .destination }).count, 1)
193+
}
183194
}
184195
}
185196

0 commit comments

Comments
 (0)