@@ -22,6 +22,7 @@ const libraryPath = path.join(
2222const fixtureSourcePath = path . join ( __dirname , 'fixture_library' , 'ffi_test_library.c' ) ;
2323const fixtureExportsPath = path . join ( __dirname , 'fixture_library' , 'ffi_test_library.def' ) ;
2424const buildLockPath = `${ libraryPath } .lock` ;
25+ const tempLibraryPath = `${ libraryPath } .tmp` ;
2526
2627function sleep ( ms ) {
2728 Atomics . wait ( new Int32Array ( new SharedArrayBuffer ( 4 ) ) , 0 , 0 , ms ) ;
@@ -32,13 +33,15 @@ function getCompiler(defaultCompiler) {
3233}
3334
3435function getBuildCommand ( ) {
36+ const outputPath = tempLibraryPath ;
37+
3538 if ( process . platform === 'win32' ) {
3639 return {
3740 command : getCompiler ( 'cl' ) ,
3841 args : [
3942 '/nologo' ,
4043 '/LD' ,
41- `/Fe:${ libraryPath } ` ,
44+ `/Fe:${ outputPath } ` ,
4245 fixtureSourcePath ,
4346 '/link' ,
4447 `/DEF:${ fixtureExportsPath } ` ,
@@ -53,7 +56,7 @@ function getBuildCommand() {
5356 '-dynamiclib' ,
5457 '-fPIC' ,
5558 '-o' ,
56- libraryPath ,
59+ outputPath ,
5760 fixtureSourcePath ,
5861 ] ,
5962 } ;
@@ -65,24 +68,28 @@ function getBuildCommand() {
6568 '-shared' ,
6669 '-fPIC' ,
6770 '-o' ,
68- libraryPath ,
71+ outputPath ,
6972 fixtureSourcePath ,
7073 ] ,
7174 } ;
7275}
7376
7477function buildFixtureLibrary ( ) {
7578 fs . mkdirSync ( fixtureBuildDir , { recursive : true } ) ;
79+ fs . rmSync ( tempLibraryPath , { force : true } ) ;
7680
7781 const { command, args } = getBuildCommand ( ) ;
7882 const { status, error, stderr } = childProcess . spawnSync ( command , args , {
7983 stdio : [ 'ignore' , 'ignore' , 'pipe' ] ,
8084 } ) ;
8185
82- if ( status === 0 && fs . existsSync ( libraryPath ) ) {
86+ if ( status === 0 && fs . existsSync ( tempLibraryPath ) ) {
87+ fs . renameSync ( tempLibraryPath , libraryPath ) ;
8388 return ;
8489 }
8590
91+ fs . rmSync ( tempLibraryPath , { force : true } ) ;
92+
8693 const renderedCommand = [ command , ...args ] . join ( ' ' ) ;
8794 const details = ( error ?. message || stderr ?. toString ( ) || `exit code ${ status } ` ) . trim ( ) ;
8895 throw new Error (
@@ -106,16 +113,13 @@ function ensureFixtureLibrary() {
106113
107114 const deadline = Date . now ( ) + 30000 ;
108115 while ( Date . now ( ) < deadline ) {
109- if ( fs . existsSync ( libraryPath ) ) {
110- return ;
111- }
112116 if ( ! fs . existsSync ( buildLockPath ) ) {
113117 break ;
114118 }
115119 sleep ( 50 ) ;
116120 }
117121
118- if ( fs . existsSync ( libraryPath ) ) {
122+ if ( fs . existsSync ( libraryPath ) && ! fs . existsSync ( buildLockPath ) ) {
119123 return ;
120124 }
121125 }
0 commit comments