From 8d615a6da8f86ff3a65cdbd9cf86d1591516c4b1 Mon Sep 17 00:00:00 2001 From: Himanshu Singh Date: Mon, 31 Jul 2023 10:40:34 +0200 Subject: [PATCH 1/3] chore: writes compiled mongosh to lib-boxednode instead of lib to avoid linking it twice when building node --- src/index.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/index.ts b/src/index.ts index 107d4e8..deb1422 100644 --- a/src/index.ts +++ b/src/index.ts @@ -312,7 +312,7 @@ async function compileJSFileAsBinaryImpl (options: CompilationOptions, logger: L } logger.stepStarting('Inserting custom code into Node.js source'); - await fs.mkdir(path.join(nodeSourcePath, 'lib', namespace), { recursive: true }); + await fs.mkdir(path.join(nodeSourcePath, 'lib-boxednode', namespace), { recursive: true }); let entryPointTrampolineSource = await fs.readFile( path.join(__dirname, '..', 'resources', 'entry-point-trampoline.js'), 'utf8'); entryPointTrampolineSource = entryPointTrampolineSource.replace( @@ -322,9 +322,9 @@ async function compileJSFileAsBinaryImpl (options: CompilationOptions, logger: L enableBindingsPatch })); await fs.writeFile( - path.join(nodeSourcePath, 'lib', namespace, `${namespace}.js`), + path.join(nodeSourcePath, 'lib-boxednode', namespace, `${namespace}.js`), entryPointTrampolineSource); - extraJSSourceFiles.push(`./lib/${namespace}/${namespace}.js`); + extraJSSourceFiles.push(`./lib-boxednode/${namespace}/${namespace}.js`); logger.stepCompleted(); logger.stepStarting('Storing executable metadata'); From 7f465b2ed00ccdb633977433e73fd12bd0100402 Mon Sep 17 00:00:00 2001 From: Himanshu Singh Date: Mon, 31 Jul 2023 14:19:39 +0200 Subject: [PATCH 2/3] chore: ditch namespace and reference correct path in the main source --- src/index.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/index.ts b/src/index.ts index deb1422..9a3201d 100644 --- a/src/index.ts +++ b/src/index.ts @@ -312,7 +312,7 @@ async function compileJSFileAsBinaryImpl (options: CompilationOptions, logger: L } logger.stepStarting('Inserting custom code into Node.js source'); - await fs.mkdir(path.join(nodeSourcePath, 'lib-boxednode', namespace), { recursive: true }); + await fs.mkdir(path.join(nodeSourcePath, 'lib-boxednode'), { recursive: true }); let entryPointTrampolineSource = await fs.readFile( path.join(__dirname, '..', 'resources', 'entry-point-trampoline.js'), 'utf8'); entryPointTrampolineSource = entryPointTrampolineSource.replace( @@ -322,9 +322,9 @@ async function compileJSFileAsBinaryImpl (options: CompilationOptions, logger: L enableBindingsPatch })); await fs.writeFile( - path.join(nodeSourcePath, 'lib-boxednode', namespace, `${namespace}.js`), + path.join(nodeSourcePath, 'lib-boxednode', `${namespace}.js`), entryPointTrampolineSource); - extraJSSourceFiles.push(`./lib-boxednode/${namespace}/${namespace}.js`); + extraJSSourceFiles.push(`./lib-boxednode/${namespace}.js`); logger.stepCompleted(); logger.stepStarting('Storing executable metadata'); @@ -355,7 +355,7 @@ async function compileJSFileAsBinaryImpl (options: CompilationOptions, logger: L let mainSource = await fs.readFile( path.join(__dirname, '..', 'resources', 'main-template.cc'), 'utf8'); mainSource = mainSource.replace(/\bREPLACE_WITH_ENTRY_POINT\b/g, - JSON.stringify(`${namespace}/${namespace}`)); + JSON.stringify(`lib-boxednode/${namespace}`)); mainSource = mainSource.replace(/\bREPLACE_DECLARE_LINKED_MODULES\b/g, registerFunctions.map((fn) => `void ${fn}(const void**,const void**);\n`).join('')); mainSource = mainSource.replace(/\bREPLACE_DEFINE_LINKED_MODULES\b/g, From fe03a4aaf35e12e3f2956db1241907405e9faf0a Mon Sep 17 00:00:00 2001 From: Himanshu Singh Date: Tue, 1 Aug 2023 13:28:33 +0200 Subject: [PATCH 3/3] chore: conditionally generate the entry point for custom source code based on node.js version --- src/index.ts | 32 ++++++++++++++++++++++++++------ 1 file changed, 26 insertions(+), 6 deletions(-) diff --git a/src/index.ts b/src/index.ts index 9a3201d..8bda17b 100644 --- a/src/index.ts +++ b/src/index.ts @@ -271,6 +271,7 @@ async function compileJSFileAsBinaryImpl (options: CompilationOptions, logger: L const nodeSourcePath = await getNodeSourceForVersion( options.nodeVersionRange, options.tmpdir, logger); + const nodeVersion = await getNodeVersionFromSourceDirectory(nodeSourcePath); const requireMappings: [RegExp, string][] = []; const extraJSSourceFiles: string[] = []; @@ -312,7 +313,6 @@ async function compileJSFileAsBinaryImpl (options: CompilationOptions, logger: L } logger.stepStarting('Inserting custom code into Node.js source'); - await fs.mkdir(path.join(nodeSourcePath, 'lib-boxednode'), { recursive: true }); let entryPointTrampolineSource = await fs.readFile( path.join(__dirname, '..', 'resources', 'entry-point-trampoline.js'), 'utf8'); entryPointTrampolineSource = entryPointTrampolineSource.replace( @@ -321,10 +321,30 @@ async function compileJSFileAsBinaryImpl (options: CompilationOptions, logger: L requireMappings: requireMappings.map(([re, linked]) => [re.source, re.flags, linked]), enableBindingsPatch })); - await fs.writeFile( - path.join(nodeSourcePath, 'lib-boxednode', `${namespace}.js`), - entryPointTrampolineSource); - extraJSSourceFiles.push(`./lib-boxednode/${namespace}.js`); + + /** + * Since Node 20.x, external source code linked from `lib` directory started + * failing the Node.js build process because of the file being linked multiple + * times which is why we do not link the external files anymore from `lib` + * directory and instead from a different directory, `lib-boxednode`. This + * however does not work for any node version < 20 which is why we are + * conditionally generating the entry point and configure params here based on + * Node version. + */ + const { customCodeSource, customCodeConfigureParam, customCodeEntryPoint } = nodeVersion[0] >= 20 + ? { + customCodeSource: path.join(nodeSourcePath, 'lib-boxednode', `${namespace}.js`), + customCodeConfigureParam: `./lib-boxednode/${namespace}.js`, + customCodeEntryPoint: `lib-boxednode/${namespace}` + } : { + customCodeSource: path.join(nodeSourcePath, 'lib', namespace, `${namespace}.js`), + customCodeConfigureParam: `./lib/${namespace}/${namespace}.js`, + customCodeEntryPoint: `${namespace}/${namespace}` + }; + + await fs.mkdir(path.dirname(customCodeSource), { recursive: true }); + await fs.writeFile(customCodeSource, entryPointTrampolineSource); + extraJSSourceFiles.push(customCodeConfigureParam); logger.stepCompleted(); logger.stepStarting('Storing executable metadata'); @@ -355,7 +375,7 @@ async function compileJSFileAsBinaryImpl (options: CompilationOptions, logger: L let mainSource = await fs.readFile( path.join(__dirname, '..', 'resources', 'main-template.cc'), 'utf8'); mainSource = mainSource.replace(/\bREPLACE_WITH_ENTRY_POINT\b/g, - JSON.stringify(`lib-boxednode/${namespace}`)); + JSON.stringify(customCodeEntryPoint)); mainSource = mainSource.replace(/\bREPLACE_DECLARE_LINKED_MODULES\b/g, registerFunctions.map((fn) => `void ${fn}(const void**,const void**);\n`).join('')); mainSource = mainSource.replace(/\bREPLACE_DEFINE_LINKED_MODULES\b/g,