@@ -114,6 +114,13 @@ struct JobResult {
114114 /// `Jobs` must include all of the compilation jobs.
115115 /// `Inputs` will hold all the primary inputs that were not compiled because of incremental compilation
116116 func writeBuildRecord( _ jobs: [ Job ] , _ skippedInputs: Set < TypedVirtualPath > ? ) {
117+ guard let absPath = buildRecordPath. absolutePath else {
118+ diagnosticEngine. emit (
119+ . warning_could_not_write_build_record_not_absolutePath( buildRecordPath) )
120+ return
121+ }
122+ preservePreviousBuildRecord ( absPath)
123+
117124 let buildRecord = BuildRecord (
118125 jobs: jobs,
119126 finishedJobResults: finishedJobResults,
@@ -134,11 +141,6 @@ struct JobResult {
134141 diagnosticEngine. emit ( . warning_could_not_serialize_build_record( error) )
135142 return
136143 }
137- guard let absPath = buildRecordPath. absolutePath else {
138- diagnosticEngine. emit (
139- . warning_could_not_write_build_record_not_absolutePath( buildRecordPath) )
140- return
141- }
142144 do {
143145 try fileSystem. writeFileContents ( absPath,
144146 bytes: ByteString ( encodingAsUTF8: contents) )
@@ -149,6 +151,16 @@ struct JobResult {
149151 }
150152 }
151153
154+ /// Before writing to the dependencies file path, preserve any previous file
155+ /// that may have been there. No error handling -- this is just a nicety, it
156+ /// doesn't matter if it fails.
157+ /// Added for the sake of compatibility with the legacy driver.
158+ private func preservePreviousBuildRecord( _ oldPath: AbsolutePath ) {
159+ let newPath = oldPath. withTilde ( )
160+ try ? fileSystem. move ( from: oldPath, to: newPath)
161+ }
162+
163+
152164// TODO: Incremental too many names, buildRecord BuildRecord outofdatemap
153165 func populateOutOfDateBuildRecord(
154166 inputFiles: [ TypedVirtualPath ] ,
@@ -188,3 +200,9 @@ struct JobResult {
188200 finishedJobResults. append ( JobResult ( job, result) )
189201 }
190202}
203+
204+ fileprivate extension AbsolutePath {
205+ func withTilde( ) -> Self {
206+ parentDirectory. appending ( component: basename + " ~ " )
207+ }
208+ }
0 commit comments