Skip to content

[6.2][BuildPlan] Fix traverseModules to account for tests with direct ma… #8943

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

Merged
merged 1 commit into from
Jul 17, 2025
Merged
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
41 changes: 34 additions & 7 deletions Sources/Build/BuildPlan/BuildPlan.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1120,13 +1120,40 @@ extension BuildPlan {
guard visited.insert(.package(package)).inserted else {
return []
}
return package.modules.compactMap {
if case .test = $0.underlying.type,
!self.graph.rootPackages.contains(id: package.id)

var successors: [TraversalNode] = []
for product in package.products {
if case .test = product.underlying.type,
!graph.rootPackages.contains(id: package.id)
{
return nil
continue
}
return .init(module: $0, context: .target)

successors.append(.init(product: product, context: .target))
}

for module in package.modules {
// Tests are discovered through an aggregate product which also
// informs their destination.
if case .test = module.type {
continue
}
successors.append(.init(module: module, context: .target))
}

return successors
}

func successors(
for product: ResolvedProduct,
destination: Destination
) -> [TraversalNode] {
guard destination == .host || product.underlying.type == .test else {
return []
}

return product.modules.map { module in
TraversalNode(module: module, context: destination)
}
}

Expand Down Expand Up @@ -1156,8 +1183,8 @@ extension BuildPlan {
successors(for: package)
case .module(let module, let destination):
successors(for: module, destination: destination)
case .product:
[]
case .product(let product, let destination):
successors(for: product, destination: destination)
}
} onNext: { current, parent in
let parentModule: (ResolvedModule, BuildParameters.Destination)? = switch parent {
Expand Down
37 changes: 37 additions & 0 deletions Tests/BuildTests/BuildPlanTraversalTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,43 @@ struct BuildPlanTraversalTests {
#expect(self.getParents(in: results, for: "HAL", destination: .host) == [])
}

@Test
func traversalWithTestThatDependsOnMacro() async throws {
let destinationTriple = Triple.arm64Linux
let toolsTriple = Triple.x86_64MacOS

let (graph, fs, scope) = try macrosTestsPackageGraph()
let plan = try await BuildPlan(
destinationBuildParameters: mockBuildParameters(
destination: .target,
triple: destinationTriple
),
toolsBuildParameters: mockBuildParameters(
destination: .host,
triple: toolsTriple
),
graph: graph,
fileSystem: fs,
observabilityScope: scope
)

// Tests that if one of the test targets directly depends
// on a macro - all tests are built for the "host".
var results: [Result] = []
plan.traverseModules {
results.append(Result(parent: $1, module: $0))
}

let package = try #require(graph.package(for: "swift-mmio"))

// Tests that if one of the test targets directly depends
// on a macro - all tests are built for the "host".
for module in package.modules where module.type == .test {
let results = getResults(for: module.name, in: results)
#expect(results.allSatisfy { $0.module.1 == .host })
}
}

@Test
func recursiveDependencyTraversal() async throws {
let destinationTriple = Triple.arm64Linux
Expand Down