Skip to content

Commit a3e78e2

Browse files
committed
[BuildPlan] Fix traverseModules to account for tests with direct macro dependencies
This change aligns `traverseModules` with `computeDestinations` in regard to test targets that have direct macro dependencies: if at least one test target directly depends on a macro, all of the tests are built for the "host". Resolves: swiftlang/sourcekit-lsp#2205
1 parent beecce0 commit a3e78e2

File tree

2 files changed

+71
-7
lines changed

2 files changed

+71
-7
lines changed

Sources/Build/BuildPlan/BuildPlan.swift

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1120,13 +1120,40 @@ extension BuildPlan {
11201120
guard visited.insert(.package(package)).inserted else {
11211121
return []
11221122
}
1123-
return package.modules.compactMap {
1124-
if case .test = $0.underlying.type,
1125-
!self.graph.rootPackages.contains(id: package.id)
1123+
1124+
var successors: [TraversalNode] = []
1125+
for product in package.products {
1126+
if case .test = product.underlying.type,
1127+
!graph.rootPackages.contains(id: package.id)
11261128
{
1127-
return nil
1129+
continue
11281130
}
1129-
return .init(module: $0, context: .target)
1131+
1132+
successors.append(.init(product: product, context: .target))
1133+
}
1134+
1135+
for module in package.modules {
1136+
// Tests are discovered through an aggregate product which also
1137+
// informs their destination.
1138+
if case .test = module.type {
1139+
continue
1140+
}
1141+
successors.append(.init(module: module, context: .target))
1142+
}
1143+
1144+
return successors
1145+
}
1146+
1147+
func successors(
1148+
for product: ResolvedProduct,
1149+
destination: Destination
1150+
) -> [TraversalNode] {
1151+
guard destination == .host || product.underlying.type == .test else {
1152+
return []
1153+
}
1154+
1155+
return product.modules.map { module in
1156+
TraversalNode(module: module, context: destination)
11301157
}
11311158
}
11321159

@@ -1156,8 +1183,8 @@ extension BuildPlan {
11561183
successors(for: package)
11571184
case .module(let module, let destination):
11581185
successors(for: module, destination: destination)
1159-
case .product:
1160-
[]
1186+
case .product(let product, let destination):
1187+
successors(for: product, destination: destination)
11611188
}
11621189
} onNext: { current, parent in
11631190
let parentModule: (ResolvedModule, BuildParameters.Destination)? = switch parent {

Tests/BuildTests/BuildPlanTraversalTests.swift

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,43 @@ struct BuildPlanTraversalTests {
123123
#expect(self.getParents(in: results, for: "HAL", destination: .host) == [])
124124
}
125125

126+
@Test
127+
func traversalWithTestThatDependsOnMacro() async throws {
128+
let destinationTriple = Triple.arm64Linux
129+
let toolsTriple = Triple.x86_64MacOS
130+
131+
let (graph, fs, scope) = try macrosTestsPackageGraph()
132+
let plan = try await BuildPlan(
133+
destinationBuildParameters: mockBuildParameters(
134+
destination: .target,
135+
triple: destinationTriple
136+
),
137+
toolsBuildParameters: mockBuildParameters(
138+
destination: .host,
139+
triple: toolsTriple
140+
),
141+
graph: graph,
142+
fileSystem: fs,
143+
observabilityScope: scope
144+
)
145+
146+
// Tests that if one of the test targets directly depends
147+
// on a macro - all tests are built for the "host".
148+
var results: [Result] = []
149+
plan.traverseModules {
150+
results.append(Result(parent: $1, module: $0))
151+
}
152+
153+
let package = try #require(graph.package(for: "swift-mmio"))
154+
155+
// Tests that if one of the test targets directly depends
156+
// on a macro - all tests are built for the "host".
157+
for module in package.modules where module.type == .test {
158+
let results = getResults(for: module.name, in: results)
159+
#expect(results.allSatisfy { $0.module.1 == .host })
160+
}
161+
}
162+
126163
@Test
127164
func recursiveDependencyTraversal() async throws {
128165
let destinationTriple = Triple.arm64Linux

0 commit comments

Comments
 (0)