Skip to content

Commit 878d86e

Browse files
author
David Ungar
committed
Remove build record if module dependency graph write fails
1 parent 0620850 commit 878d86e

File tree

4 files changed

+35
-11
lines changed

4 files changed

+35
-11
lines changed

Sources/SwiftDriver/Driver/Driver.swift

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -971,10 +971,7 @@ extension Driver {
971971
if !childJobs.isEmpty {
972972
do {
973973
defer {
974-
self.incrementalCompilationState?.writeDependencyGraph()
975-
buildRecordInfo?.writeBuildRecord(
976-
jobs,
977-
incrementalCompilationState?.skippedCompilationInputs)
974+
writeIncrementalBuildInformation(jobs)
978975
}
979976
try performTheBuild(allJobs: childJobs,
980977
jobExecutionDelegate: toolExecutionDelegate,
@@ -1038,6 +1035,20 @@ extension Driver {
10381035
recordedInputModificationDates: recordedInputModificationDates)
10391036
}
10401037

1038+
private func writeIncrementalBuildInformation(_ jobs: [Job]) {
1039+
if let incrementalCompilationState = self.incrementalCompilationState {
1040+
let hadError = incrementalCompilationState.writeDependencyGraph()
1041+
/// Ensure that a bogus dependency graph is not used next time
1042+
guard !hadError else {
1043+
buildRecordInfo?.removeBuildRecord()
1044+
return
1045+
}
1046+
}
1047+
buildRecordInfo?.writeBuildRecord(
1048+
jobs,
1049+
incrementalCompilationState?.skippedCompilationInputs)
1050+
}
1051+
10411052
private func printBindings(_ job: Job) {
10421053
stdoutStream <<< #"# ""# <<< targetTriple.triple
10431054
stdoutStream <<< #"" - ""# <<< job.tool.basename

Sources/SwiftDriver/IncrementalCompilation/BuildRecordInfo.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,13 @@ import SwiftOptions
184184
}
185185
}
186186

187+
func removeBuildRecord() {
188+
guard let absPath = buildRecordPath.absolutePath else {
189+
return
190+
}
191+
try? fileSystem.removeFileTree(absPath)
192+
}
193+
187194
/// Before writing to the dependencies file path, preserve any previous file
188195
/// that may have been there. No error handling -- this is just a nicety, it
189196
/// doesn't matter if it fails.

Sources/SwiftDriver/IncrementalCompilation/IncrementalCompilationState.swift

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -444,23 +444,26 @@ extension IncrementalCompilationState {
444444
// MARK: - Serialization
445445

446446
extension IncrementalCompilationState {
447-
@_spi(Testing) public func writeDependencyGraph() {
447+
@_spi(Testing) public func writeDependencyGraph() -> Bool {
448448
// If the cross-module build is not enabled, the status quo dictates we
449449
// not emit this file.
450450
guard moduleDependencyGraph.info.isCrossModuleIncrementalBuildEnabled else {
451-
return
451+
return false
452452
}
453453

454454
guard
455455
let recordInfo = self.driver.buildRecordInfo
456456
else {
457+
// It's OK to silently fail because no build record will be written
458+
// so there will be no attempt to use the saved module dependency graph
459+
// next time.
457460
self.driver.diagnosticEngine.emit(
458461
.warning_could_not_write_dependency_graph)
459-
return
462+
return true
460463
}
461-
self.moduleDependencyGraph.write(to: recordInfo.dependencyGraphPath,
462-
on: self.driver.fileSystem,
463-
compilerVersion: recordInfo.actualSwiftVersion)
464+
return self.moduleDependencyGraph.write(to: recordInfo.dependencyGraphPath,
465+
on: self.driver.fileSystem,
466+
compilerVersion: recordInfo.actualSwiftVersion)
464467
}
465468
}
466469

Sources/SwiftDriver/IncrementalCompilation/ModuleDependencyGraph.swift

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -663,19 +663,22 @@ extension ModuleDependencyGraph {
663663
/// - fileSystem: The file system for this location.
664664
/// - compilerVersion: A string containing version information for the
665665
/// driver used to create this file.
666+
/// - Returns: true if had error
666667
@_spi(Testing) public func write(
667668
to path: VirtualPath,
668669
on fileSystem: FileSystem,
669670
compilerVersion: String
670-
) {
671+
) -> Bool {
671672
let data = ModuleDependencyGraph.Serializer.serialize(self, compilerVersion)
672673

673674
do {
674675
try fileSystem.writeFileContents(path,
675676
bytes: data,
676677
atomically: true)
678+
return false
677679
} catch {
678680
info.diagnosticEngine.emit(.warning_could_not_write_dep_graph(to: path, error: error))
681+
return true
679682
}
680683
}
681684

0 commit comments

Comments
 (0)