From 3f12b2cef97bd7840e9476c04b8646a66c15e1ab Mon Sep 17 00:00:00 2001 From: Daijiro Wachi Date: Fri, 24 Apr 2026 23:56:49 +0900 Subject: [PATCH] lib: insert argv[0] at argv[1] in setDeserializeMainFunction --- doc/api/v8.md | 2 +- lib/internal/v8/startup_snapshot.js | 6 +++--- test/embedding/test-embedding-snapshot-as-file.js | 2 +- test/embedding/test-embedding-snapshot-basic.js | 2 +- .../embedding/test-embedding-snapshot-without-code-cache.js | 2 +- test/fixtures/snapshot/typescript-main.js | 4 ++-- test/fixtures/snapshot/v8-startup-snapshot-api.js | 2 +- test/parallel/test-snapshot-argv1.js | 1 + 8 files changed, 11 insertions(+), 10 deletions(-) diff --git a/doc/api/v8.md b/doc/api/v8.md index bf868d5e74491d..6858594cda7c25 100644 --- a/doc/api/v8.md +++ b/doc/api/v8.md @@ -1362,7 +1362,7 @@ v8.startupSnapshot.setDeserializeMainFunction((shelf) => { // process.env and process.argv are refreshed during snapshot // deserialization. const lang = process.env.BOOK_LANG || 'en_US'; - const book = process.argv[1]; + const book = process.argv[2]; const name = `${book}.${lang}.txt`; console.log(shelf.storage.get(name)); }, shelf); diff --git a/lib/internal/v8/startup_snapshot.js b/lib/internal/v8/startup_snapshot.js index af5fb07925dc15..2e634b7b283df1 100644 --- a/lib/internal/v8/startup_snapshot.js +++ b/lib/internal/v8/startup_snapshot.js @@ -106,10 +106,10 @@ function setDeserializeMainFunction(callback, data) { markBootstrapComplete, } = require('internal/process/pre_execution'); - // This should be in sync with run_main_module.js until we make that - // a built-in main function. - // TODO(joyeecheung): make a copy of argv[0] and insert it as argv[1]. prepareMainThreadExecution(false); + // Like SEA (FixupArgsForSEA), insert argv[0] at argv[1] so that user + // args begin at argv[2], consistent with run_main_module.js. + process.argv.splice(1, 0, process.argv[0]); markBootstrapComplete(); callback(data); }); diff --git a/test/embedding/test-embedding-snapshot-as-file.js b/test/embedding/test-embedding-snapshot-as-file.js index 5a9b4b1d928350..b9822e8133aedf 100644 --- a/test/embedding/test-embedding-snapshot-as-file.js +++ b/test/embedding/test-embedding-snapshot-as-file.js @@ -43,7 +43,7 @@ spawnSyncAndAssert( stdout(output) { assert.deepStrictEqual(JSON.parse(output), { originalArgv: [embedtest, '__node_anonymous_main', ...buildSnapshotExecArgs], - currentArgv: [embedtest, ...runSnapshotExecArgs], + currentArgv: [embedtest, embedtest, ...runSnapshotExecArgs], }); return true; }, diff --git a/test/embedding/test-embedding-snapshot-basic.js b/test/embedding/test-embedding-snapshot-basic.js index 8ace784116df92..204b3211f9d69e 100644 --- a/test/embedding/test-embedding-snapshot-basic.js +++ b/test/embedding/test-embedding-snapshot-basic.js @@ -43,7 +43,7 @@ spawnSyncAndAssert( stdout(output) { assert.deepStrictEqual(JSON.parse(output), { originalArgv: [embedtest, '__node_anonymous_main', ...buildSnapshotExecArgs], - currentArgv: [embedtest, ...runSnapshotExecArgs], + currentArgv: [embedtest, embedtest, ...runSnapshotExecArgs], }); return true; }, diff --git a/test/embedding/test-embedding-snapshot-without-code-cache.js b/test/embedding/test-embedding-snapshot-without-code-cache.js index 577ace1188ff40..e3e12dd96de3f3 100644 --- a/test/embedding/test-embedding-snapshot-without-code-cache.js +++ b/test/embedding/test-embedding-snapshot-without-code-cache.js @@ -43,7 +43,7 @@ spawnSyncAndAssert( stdout(output) { assert.deepStrictEqual(JSON.parse(output), { originalArgv: [embedtest, '__node_anonymous_main', ...buildSnapshotExecArgs], - currentArgv: [embedtest, ...runSnapshotExecArgs], + currentArgv: [embedtest, embedtest, ...runSnapshotExecArgs], }); return true; }, diff --git a/test/fixtures/snapshot/typescript-main.js b/test/fixtures/snapshot/typescript-main.js index 4b0c8274cec312..6570433c0aab6b 100644 --- a/test/fixtures/snapshot/typescript-main.js +++ b/test/fixtures/snapshot/typescript-main.js @@ -9,8 +9,8 @@ const v8 = require('v8'); const assert = require('assert'); v8.startupSnapshot.setDeserializeMainFunction(( { ts }) => { - const input = process.argv[1]; - const output = process.argv[2]; + const input = process.argv[2]; + const output = process.argv[3]; console.error(`Compiling ${input} to ${output}`); assert(input); assert(output); diff --git a/test/fixtures/snapshot/v8-startup-snapshot-api.js b/test/fixtures/snapshot/v8-startup-snapshot-api.js index 0d32cd14c500e0..393bad4f1a828a 100644 --- a/test/fixtures/snapshot/v8-startup-snapshot-api.js +++ b/test/fixtures/snapshot/v8-startup-snapshot-api.js @@ -48,7 +48,7 @@ v8.startupSnapshot.setDeserializeMainFunction((shelf) => { // process.env and process.argv are refreshed during snapshot // deserialization. const lang = process.env.BOOK_LANG || 'en_US'; - const book = process.argv[1]; + const book = process.argv[2]; const name = `${book}.${lang}.txt`; console.error('Reading', name); console.log(shelf.storage.get(name).toString()); diff --git a/test/parallel/test-snapshot-argv1.js b/test/parallel/test-snapshot-argv1.js index b98e13aa8ccfed..d20b6a8c1f66c3 100644 --- a/test/parallel/test-snapshot-argv1.js +++ b/test/parallel/test-snapshot-argv1.js @@ -49,6 +49,7 @@ require('v8').startupSnapshot.setDeserializeMainFunction(() => { const stdout = JSON.parse(child.stdout.toString().trim()); assert.deepStrictEqual(stdout, [ + process.execPath, process.execPath, 'argv1', 'argv2',