@@ -271,6 +271,7 @@ async function compileJSFileAsBinaryImpl (options: CompilationOptions, logger: L
271271
272272 const nodeSourcePath = await getNodeSourceForVersion (
273273 options . nodeVersionRange , options . tmpdir , logger ) ;
274+ const nodeVersion = await getNodeVersionFromSourceDirectory ( nodeSourcePath ) ;
274275
275276 const requireMappings : [ RegExp , string ] [ ] = [ ] ;
276277 const extraJSSourceFiles : string [ ] = [ ] ;
@@ -312,7 +313,6 @@ async function compileJSFileAsBinaryImpl (options: CompilationOptions, logger: L
312313 }
313314
314315 logger . stepStarting ( 'Inserting custom code into Node.js source' ) ;
315- await fs . mkdir ( path . join ( nodeSourcePath , 'lib-boxednode' ) , { recursive : true } ) ;
316316 let entryPointTrampolineSource = await fs . readFile (
317317 path . join ( __dirname , '..' , 'resources' , 'entry-point-trampoline.js' ) , 'utf8' ) ;
318318 entryPointTrampolineSource = entryPointTrampolineSource . replace (
@@ -321,10 +321,30 @@ async function compileJSFileAsBinaryImpl (options: CompilationOptions, logger: L
321321 requireMappings : requireMappings . map ( ( [ re , linked ] ) => [ re . source , re . flags , linked ] ) ,
322322 enableBindingsPatch
323323 } ) ) ;
324- await fs . writeFile (
325- path . join ( nodeSourcePath , 'lib-boxednode' , `${ namespace } .js` ) ,
326- entryPointTrampolineSource ) ;
327- extraJSSourceFiles . push ( `./lib-boxednode/${ namespace } .js` ) ;
324+
325+ /**
326+ * Since Node 20.x, external source code linked from `lib` directory started
327+ * failing the Node.js build process because of the file being linked multiple
328+ * times which is why we do not link the external files anymore from `lib`
329+ * directory and instead from a different directory, `lib-boxednode`. This
330+ * however does not work for any node version < 20 which is why we are
331+ * conditionally generating the entry point and configure params here based on
332+ * Node version.
333+ */
334+ const { customCodeSource, customCodeConfigureParam, customCodeEntryPoint } = nodeVersion [ 0 ] >= 20
335+ ? {
336+ customCodeSource : path . join ( nodeSourcePath , 'lib-boxednode' , `${ namespace } .js` ) ,
337+ customCodeConfigureParam : `./lib-boxednode/${ namespace } .js` ,
338+ customCodeEntryPoint : `lib-boxednode/${ namespace } `
339+ } : {
340+ customCodeSource : path . join ( nodeSourcePath , 'lib' , namespace , `${ namespace } .js` ) ,
341+ customCodeConfigureParam : `./lib/${ namespace } /${ namespace } .js` ,
342+ customCodeEntryPoint : `${ namespace } /${ namespace } `
343+ } ;
344+
345+ await fs . mkdir ( path . dirname ( customCodeSource ) , { recursive : true } ) ;
346+ await fs . writeFile ( customCodeSource , entryPointTrampolineSource ) ;
347+ extraJSSourceFiles . push ( customCodeConfigureParam ) ;
328348 logger . stepCompleted ( ) ;
329349
330350 logger . stepStarting ( 'Storing executable metadata' ) ;
@@ -355,7 +375,7 @@ async function compileJSFileAsBinaryImpl (options: CompilationOptions, logger: L
355375 let mainSource = await fs . readFile (
356376 path . join ( __dirname , '..' , 'resources' , 'main-template.cc' ) , 'utf8' ) ;
357377 mainSource = mainSource . replace ( / \b R E P L A C E _ W I T H _ E N T R Y _ P O I N T \b / g,
358- JSON . stringify ( `lib-boxednode/ ${ namespace } ` ) ) ;
378+ JSON . stringify ( customCodeEntryPoint ) ) ;
359379 mainSource = mainSource . replace ( / \b R E P L A C E _ D E C L A R E _ L I N K E D _ M O D U L E S \b / g,
360380 registerFunctions . map ( ( fn ) => `void ${ fn } (const void**,const void**);\n` ) . join ( '' ) ) ;
361381 mainSource = mainSource . replace ( / \b R E P L A C E _ D E F I N E _ L I N K E D _ M O D U L E S \b / g,
0 commit comments