@@ -904,12 +904,27 @@ impl Loader {
904904
905905 let output_path = config. output_path . as_ref ( ) . unwrap ( ) ;
906906
907- if compiler. is_like_msvc ( ) {
907+ let temp_dir = if compiler. is_like_msvc ( ) {
908908 let out = format ! ( "-out:{}" , output_path. to_str( ) . unwrap( ) ) ;
909909 command. arg ( if self . debug_build { "-LDd" } else { "-LD" } ) ;
910910 command. arg ( "-utf-8" ) ;
911+
912+ // Windows creates intermediate files when compiling (.exp, .lib, .obj), which causes
913+ // issues when multiple processes are compiling in the same directory. This creates a
914+ // temporary directory for those files to go into, which is deleted after compilation.
915+ let temp_dir = output_path. parent ( ) . unwrap ( ) . join ( format ! (
916+ "tmp_{}_{:?}" ,
917+ std:: process:: id( ) ,
918+ std:: thread:: current( ) . id( )
919+ ) ) ;
920+ std:: fs:: create_dir_all ( & temp_dir) . unwrap ( ) ;
921+
922+ command. arg ( format ! ( "/Fo{}\\ " , temp_dir. display( ) ) ) ;
911923 command. args ( cc_config. get_files ( ) ) ;
912924 command. arg ( "-link" ) . arg ( out) ;
925+ command. arg ( format ! ( "/IMPLIB:{}.lib" , temp_dir. join( "temp" ) . display( ) ) ) ;
926+
927+ Some ( temp_dir)
913928 } else {
914929 command. arg ( "-Werror=implicit-function-declaration" ) ;
915930 if cfg ! ( any( target_os = "macos" , target_os = "ios" ) ) {
@@ -921,12 +936,18 @@ impl Loader {
921936 }
922937 command. args ( cc_config. get_files ( ) ) ;
923938 command. arg ( "-o" ) . arg ( output_path) ;
924- }
939+
940+ None
941+ } ;
925942
926943 let output = command. output ( ) . with_context ( || {
927944 format ! ( "Failed to execute the C compiler with the following command:\n {command:?}" )
928945 } ) ?;
929946
947+ if let Some ( temp_dir) = temp_dir {
948+ let _ = fs:: remove_dir_all ( temp_dir) ;
949+ }
950+
930951 FileExt :: unlock ( lock_file) ?;
931952 fs:: remove_file ( lock_path) ?;
932953
0 commit comments