@@ -271,6 +271,7 @@ async function compileJSFileAsBinaryImpl (options: CompilationOptions, logger: L
271
271
272
272
const nodeSourcePath = await getNodeSourceForVersion (
273
273
options . nodeVersionRange , options . tmpdir , logger ) ;
274
+ const nodeVersion = await getNodeVersionFromSourceDirectory ( nodeSourcePath ) ;
274
275
275
276
const requireMappings : [ RegExp , string ] [ ] = [ ] ;
276
277
const extraJSSourceFiles : string [ ] = [ ] ;
@@ -312,7 +313,6 @@ async function compileJSFileAsBinaryImpl (options: CompilationOptions, logger: L
312
313
}
313
314
314
315
logger . stepStarting ( 'Inserting custom code into Node.js source' ) ;
315
- await fs . mkdir ( path . join ( nodeSourcePath , 'lib' , namespace ) , { recursive : true } ) ;
316
316
let entryPointTrampolineSource = await fs . readFile (
317
317
path . join ( __dirname , '..' , 'resources' , 'entry-point-trampoline.js' ) , 'utf8' ) ;
318
318
entryPointTrampolineSource = entryPointTrampolineSource . replace (
@@ -321,10 +321,30 @@ async function compileJSFileAsBinaryImpl (options: CompilationOptions, logger: L
321
321
requireMappings : requireMappings . map ( ( [ re , linked ] ) => [ re . source , re . flags , linked ] ) ,
322
322
enableBindingsPatch
323
323
} ) ) ;
324
- await fs . writeFile (
325
- path . join ( nodeSourcePath , 'lib' , namespace , `${ namespace } .js` ) ,
326
- entryPointTrampolineSource ) ;
327
- extraJSSourceFiles . push ( `./lib/${ namespace } /${ 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 ) ;
328
348
logger . stepCompleted ( ) ;
329
349
330
350
logger . stepStarting ( 'Storing executable metadata' ) ;
@@ -355,7 +375,7 @@ async function compileJSFileAsBinaryImpl (options: CompilationOptions, logger: L
355
375
let mainSource = await fs . readFile (
356
376
path . join ( __dirname , '..' , 'resources' , 'main-template.cc' ) , 'utf8' ) ;
357
377
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 ( ` ${ namespace } / ${ namespace } ` ) ) ;
378
+ JSON . stringify ( customCodeEntryPoint ) ) ;
359
379
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,
360
380
registerFunctions . map ( ( fn ) => `void ${ fn } (const void**,const void**);\n` ) . join ( '' ) ) ;
361
381
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