@@ -151,32 +151,45 @@ pub fn compile_clvm(
151151 & mut result_stream,
152152 ) ?;
153153
154- {
155- let output_path_obj = Path :: new ( output_path) ;
156- let output_dir = output_path_obj
157- . parent ( )
158- . map ( |p| Ok ( p) )
159- . unwrap_or_else ( || Err ( "could not get parent of output path" ) ) ?;
160-
161- let mut temp_output_file = NamedTempFile :: new_in ( output_dir) . map_err ( |e| {
162- format ! (
163- "error creating temporary compiler output for {}: {:?}" ,
164- input_path, e
165- )
166- } ) ?;
167-
168- let _ = temp_output_file
169- . write_all ( & result_stream. get_value ( ) . hex ( ) . as_bytes ( ) )
170- . map_err ( |_| format ! ( "failed to write to {:?}" , temp_output_file. path( ) ) ) ?;
171-
172- temp_output_file. persist ( output_path. clone ( ) ) . map_err ( |e| {
173- format ! (
174- "error persisting temporary compiler output {}: {:?}" ,
175- output_path, e
176- )
177- } ) ?;
154+ let output_path_obj = Path :: new ( output_path) ;
155+ let output_dir = output_path_obj
156+ . parent ( )
157+ . map ( |p| Ok ( p) )
158+ . unwrap_or_else ( || Err ( "could not get parent of output path" ) ) ?;
159+
160+ let target_data = result_stream. get_value ( ) . hex ( ) ;
161+
162+ // Try to detect whether we'd put the same output in the output file.
163+ // Don't proceed if true.
164+ if let Ok ( prev_content) = fs:: read_to_string ( output_path. clone ( ) ) {
165+ let prev_trimmed = prev_content. trim ( ) ;
166+ let trimmed = target_data. trim ( ) ;
167+ if prev_trimmed == trimmed {
168+ // It's the same program, bail regardless.
169+ return Ok ( output_path. to_string ( ) ) ;
170+ }
178171 }
179- } ;
172+
173+ // Make the contents appear atomically so that other test processes
174+ // won't mistake an empty file for intended output.
175+ let mut temp_output_file = NamedTempFile :: new_in ( output_dir) . map_err ( |e| {
176+ format ! (
177+ "error creating temporary compiler output for {}: {:?}" ,
178+ input_path, e
179+ )
180+ } ) ?;
181+
182+ let _ = temp_output_file
183+ . write_all ( & target_data. as_bytes ( ) )
184+ . map_err ( |_| format ! ( "failed to write to {:?}" , temp_output_file. path( ) ) ) ?;
185+
186+ temp_output_file. persist ( output_path. clone ( ) ) . map_err ( |e| {
187+ format ! (
188+ "error persisting temporary compiler output {}: {:?}" ,
189+ output_path, e
190+ )
191+ } ) ?;
192+ }
180193
181194 Ok ( output_path. to_string ( ) )
182195}
0 commit comments