Skip to content

Commit ecf1b1c

Browse files
Merge pull request #43 from mongodb-js/MONGOSH-1536-avoid-linking-compiled-mongosh-twice
2 parents 16215d0 + fe03a4a commit ecf1b1c

File tree

1 file changed

+26
-6
lines changed

1 file changed

+26
-6
lines changed

src/index.ts

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -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', namespace), { 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', 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);
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(/\bREPLACE_WITH_ENTRY_POINT\b/g,
358-
JSON.stringify(`${namespace}/${namespace}`));
378+
JSON.stringify(customCodeEntryPoint));
359379
mainSource = mainSource.replace(/\bREPLACE_DECLARE_LINKED_MODULES\b/g,
360380
registerFunctions.map((fn) => `void ${fn}(const void**,const void**);\n`).join(''));
361381
mainSource = mainSource.replace(/\bREPLACE_DEFINE_LINKED_MODULES\b/g,

0 commit comments

Comments
 (0)