Skip to content

Commit 3728b41

Browse files
committed
Skip targets that contain or depend-on generated code, because generated source-code may not be available at the time of scan.
1 parent 0eeb43d commit 3728b41

File tree

2 files changed

+26
-4
lines changed

2 files changed

+26
-4
lines changed

Sources/Build/BuildOperation.swift

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,22 +127,36 @@ public final class BuildOperation: PackageStructureDelegate, SPMBuildCore.BuildS
127127
guard SwiftTargetBuildDescription.checkSupportedFrontendFlags(flags: ["import-prescan"], fs: localFileSystem) else {
128128
return
129129
}
130+
130131
for (target, commandLine) in description.swiftTargetScanArgs {
131132
do {
132133
guard let dependencies = description.targetDependencyMap[target] else {
133134
// Skip target if no dependency information is present
134135
continue
135136
}
137+
let targetDependenciesSet = Set(dependencies)
138+
guard !description.generatedSourceTargetSet.contains(target),
139+
targetDependenciesSet.intersection(description.generatedSourceTargetSet).isEmpty else {
140+
// Skip targets which contain, or depend-on-targets, with generated source-code.
141+
// Such as test discovery targets and targets with plugins.
142+
continue
143+
}
136144
let resolver = try ArgsResolver(fileSystem: localFileSystem)
137145
let executor = SPMSwiftDriverExecutor(resolver: resolver,
138146
fileSystem: localFileSystem,
139147
env: ProcessEnv.vars)
148+
149+
let consumeDiagnostics: DiagnosticsEngine = DiagnosticsEngine(handlers: [])
140150
var driver = try Driver(args: commandLine,
141-
diagnosticsEngine: diagnostics,
151+
diagnosticsEngine: consumeDiagnostics,
142152
fileSystem: localFileSystem,
143153
executor: executor)
154+
guard !consumeDiagnostics.hasErrors else {
155+
// If we could not init the driver with this command, something went wrong,
156+
// proceed without checking this target.
157+
continue
158+
}
144159
let imports = try driver.performImportPrescan().imports
145-
let targetDependenciesSet = Set(dependencies)
146160
let nonDependencyTargetsSet =
147161
Set(description.targetDependencyMap.keys.filter { !targetDependenciesSet.contains($0) })
148162
let importedTargetsMissingDependency = Set(imports).intersection(nonDependencyTargetsSet)

Sources/Build/BuildOperationBuildSystemDelegateHandler.swift

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,6 @@ final class TestDiscoveryCommand: CustomLLBuildCommand {
8282
let className = iterator.key
8383
stream <<< indent(8) <<< "testCase(\(className).__allTests__\(className)),\n"
8484
}
85-
8685
stream <<< """
8786
]
8887
}
@@ -212,6 +211,9 @@ public struct BuildDescription: Codable {
212211
/// A full swift driver command-line invocation used to dependency-scan a given Swift target
213212
let swiftTargetScanArgs: [TargetName: [CommandLineFlag]]
214213

214+
/// A set of all targets with generated source
215+
let generatedSourceTargetSet: Set<TargetName>
216+
215217
/// The built test products.
216218
public let builtTestProducts: [BuiltTestProduct]
217219

@@ -231,16 +233,22 @@ public struct BuildDescription: Codable {
231233
$0[$1.target.c99name] = deps
232234
}
233235
var targetCommandLines: [TargetName: [CommandLineFlag]] = [:]
236+
var generatedSourceTargets: [TargetName] = []
234237
for (target, description) in plan.targetMap {
235238
guard case .swift(let desc) = description else {
236239
continue
237240
}
238241
targetCommandLines[target.c99name] =
239242
try desc.emitCommandLine(scanInvocation: true) + ["-driver-use-frontend-path",
240243
plan.buildParameters.toolchain.swiftCompiler.pathString]
244+
if desc.testDiscoveryTarget {
245+
generatedSourceTargets.append(target.c99name)
246+
}
241247
}
248+
generatedSourceTargets.append(contentsOf: plan.graph.allTargets.filter {$0.type == .plugin}
249+
.map { $0.c99name })
242250
self.swiftTargetScanArgs = targetCommandLines
243-
251+
self.generatedSourceTargetSet = Set(generatedSourceTargets)
244252
self.builtTestProducts = plan.buildProducts.filter{ $0.product.type == .test }.map { desc in
245253
return BuiltTestProduct(
246254
productName: desc.product.name,

0 commit comments

Comments
 (0)