From 0917f65579f9ccb46b02c8ae2ae78ff44b90704b Mon Sep 17 00:00:00 2001 From: Boris Buegling Date: Wed, 6 Sep 2023 14:45:41 -0700 Subject: [PATCH] Fix testable executables once more A previous problem here was fixed in #6723, this new fix is attempting to resolve issues where macros are used transitively by a product that a test is depending on. It seems to me that those transitively available macros should not be statically linked into tests and in fact doing so can cause various issues such as linker errors on non-Darwin platforms. It does feel like eventually we need to get away from `computeDependencies(of:)` being a computation on the entire package graph and instead let each package produce separate products which we can then just use transitively, but that is a much bigger change to SwiftPM's build system. rdar://115071012 --- Sources/Build/BuildPlan.swift | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Sources/Build/BuildPlan.swift b/Sources/Build/BuildPlan.swift index 34465eb29e6..29f5f04de3d 100644 --- a/Sources/Build/BuildPlan.swift +++ b/Sources/Build/BuildPlan.swift @@ -790,7 +790,9 @@ public class BuildPlan: SPMBuildCore.BuildPlan { if product.targets.contains(target) { staticTargets.append(target) } else if product.type == .test && (target.underlyingTarget as? SwiftTarget)?.supportsTestableExecutablesFeature == true { - if let toolsVersion = graph.package(for: product)?.manifest.toolsVersion, toolsVersion >= .v5_5 { + // Only "top-level" targets should really be considered here, not transitive ones. + let isTopLevel = topLevelDependencies.contains(target.underlyingTarget) || product.targets.contains(target) + if let toolsVersion = graph.package(for: product)?.manifest.toolsVersion, toolsVersion >= .v5_5, isTopLevel { staticTargets.append(target) } }