@@ -145,7 +145,17 @@ pub fn write_compiler_info(build_state: &BuildState) {
145145 runtime_path : & runtime_path,
146146 generated_at : & generated_at,
147147 } ;
148- let contents = serde_json:: to_string_pretty ( & out) . unwrap_or_else ( |_| String :: new ( ) ) ;
148+ let contents = match serde_json:: to_string_pretty ( & out) {
149+ Ok ( s) => s,
150+ Err ( err) => {
151+ log:: error!(
152+ "Failed to serialize compiler-info for package {}: {}. Skipping write." ,
153+ package. name,
154+ err
155+ ) ;
156+ return ;
157+ }
158+ } ;
149159 let info_path = package. get_compiler_info_path ( ) ;
150160 let should_write = match std:: fs:: read_to_string ( & info_path) {
151161 Ok ( existing) => existing != contents,
@@ -162,8 +172,33 @@ pub fn write_compiler_info(build_state: &BuildState) {
162172 // rename within the same directory is atomic on common platforms.
163173 let tmp = info_path. with_extension ( "json.tmp" ) ;
164174 if let Ok ( mut f) = File :: create ( & tmp) {
165- let _ = f. write_all ( contents. as_bytes ( ) ) ;
166- let _ = std:: fs:: rename ( & tmp, & info_path) ;
175+ if let Err ( err) = f. write_all ( contents. as_bytes ( ) ) {
176+ log:: error!(
177+ "Failed to write compiler-info for package {} to temporary file {}: {}. Skipping rename." ,
178+ package. name,
179+ tmp. display( ) ,
180+ err
181+ ) ;
182+ let _ = std:: fs:: remove_file ( & tmp) ;
183+ return ;
184+ }
185+ if let Err ( err) = f. sync_all ( ) {
186+ log:: error!(
187+ "Failed to flush compiler-info for package {}: {}. Skipping rename." ,
188+ package. name,
189+ err
190+ ) ;
191+ let _ = std:: fs:: remove_file ( & tmp) ;
192+ return ;
193+ }
194+ if let Err ( err) = std:: fs:: rename ( & tmp, & info_path) {
195+ log:: error!(
196+ "Failed to atomically replace compiler-info for package {}: {}." ,
197+ package. name,
198+ err
199+ ) ;
200+ let _ = std:: fs:: remove_file ( & tmp) ;
201+ }
167202 }
168203 }
169204 }
0 commit comments