Skip to content

Commit d0e0930

Browse files
author
David Ungar
committed
Remove build record if module dependency graph write fails
1 parent 31b2322 commit d0e0930

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
@@ -988,10 +988,7 @@ extension Driver {
988988
if !childJobs.isEmpty {
989989
do {
990990
defer {
991-
self.incrementalCompilationState?.writeDependencyGraph()
992-
buildRecordInfo?.writeBuildRecord(
993-
jobs,
994-
incrementalCompilationState?.skippedCompilationInputs)
991+
writeIncrementalBuildInformation(jobs)
995992
}
996993
try performTheBuild(allJobs: childJobs,
997994
jobExecutionDelegate: toolExecutionDelegate,
@@ -1055,6 +1052,20 @@ extension Driver {
10551052
recordedInputModificationDates: recordedInputModificationDates)
10561053
}
10571054

1055+
private func writeIncrementalBuildInformation(_ jobs: [Job]) {
1056+
if let incrementalCompilationState = self.incrementalCompilationState {
1057+
let hadError = incrementalCompilationState.writeDependencyGraph()
1058+
/// Ensure that a bogus dependency graph is not used next time
1059+
guard !hadError else {
1060+
buildRecordInfo?.removeBuildRecord()
1061+
return
1062+
}
1063+
}
1064+
buildRecordInfo?.writeBuildRecord(
1065+
jobs,
1066+
incrementalCompilationState?.skippedCompilationInputs)
1067+
}
1068+
10581069
private func printBindings(_ job: Job) {
10591070
stdoutStream <<< #"# ""# <<< targetTriple.triple
10601071
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
@@ -496,23 +496,26 @@ extension IncrementalCompilationState {
496496
// MARK: - Serialization
497497

498498
extension IncrementalCompilationState {
499-
@_spi(Testing) public func writeDependencyGraph() {
499+
@_spi(Testing) public func writeDependencyGraph() -> Bool {
500500
// If the cross-module build is not enabled, the status quo dictates we
501501
// not emit this file.
502502
guard moduleDependencyGraph.info.isCrossModuleIncrementalBuildEnabled else {
503-
return
503+
return false
504504
}
505505

506506
guard
507507
let recordInfo = self.driver.buildRecordInfo
508508
else {
509+
// It's OK to silently fail because no build record will be written
510+
// so there will be no attempt to use the saved module dependency graph
511+
// next time.
509512
self.driver.diagnosticEngine.emit(
510513
.warning_could_not_write_dependency_graph)
511-
return
514+
return true
512515
}
513-
self.moduleDependencyGraph.write(to: recordInfo.dependencyGraphPath,
514-
on: self.driver.fileSystem,
515-
compilerVersion: recordInfo.actualSwiftVersion)
516+
return self.moduleDependencyGraph.write(to: recordInfo.dependencyGraphPath,
517+
on: self.driver.fileSystem,
518+
compilerVersion: recordInfo.actualSwiftVersion)
516519
}
517520
}
518521

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)