@@ -15,7 +15,7 @@ use rustc_metadata::creader::MetadataLoaderDyn;
1515use rustc_middle:: dep_graph:: { WorkProduct , WorkProductId } ;
1616use rustc_middle:: ty:: TyCtxt ;
1717use rustc_middle:: util:: Providers ;
18- use rustc_session:: config:: { OutputFilenames , PrintRequest } ;
18+ use rustc_session:: config:: { OutputFilenames , OutputType , PrintRequest } ;
1919use rustc_session:: { EarlyDiagCtxt , Session } ;
2020
2121pub trait CallbacksExt : Callbacks + Send + ' static {
@@ -134,6 +134,18 @@ impl<C: CallbacksExt> CodegenBackend for BackendWrapper<C> {
134134 fn codegen_crate < ' tcx > ( & self , tcx : TyCtxt < ' tcx > , crate_info : & CrateInfo ) -> Box < dyn Any > {
135135 let ongoing_codegen = self . backend . codegen_crate ( tcx, crate_info) ;
136136 let outputs = tcx. output_filenames ( ( ) ) ;
137+
138+ // HACK: ZFS contains a bug that if std::fs::copy overwrites an existing file,
139+ // the data read back might be corrupted. Workaround this by removing the file beforehand.
140+ // https://github.com/openzfs/zfs/issues/18412
141+ // Remove once the fix has landed in all supported releases of ZFS.
142+ if outputs. outputs . contains_key ( & OutputType :: Object ) {
143+ let file = outputs. path ( OutputType :: Object ) ;
144+ if !file. is_stdout ( ) {
145+ _ = std:: fs:: remove_file ( file. as_path ( ) ) ;
146+ }
147+ }
148+
137149 let ( cg, work_map) = self
138150 . backend
139151 . join_codegen ( ongoing_codegen, tcx. sess , outputs) ;
0 commit comments