Skip to content

Commit 209c803

Browse files
authored
bootstrap: do not expand process.argv[1] for snapshot entry points
In applications with entry points deserialized from snapshots, they don't need an additional CLI argument for the entry point script so process.argv[1] is going to be other user-defiend arguments. We could consider copying process.argv[0] as process.argv[1] like what we do for single executable applications, but for now just don't exapnd it so it's easier to backport to previous releases. PR-URL: #47466 Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Chengzhong Wu <legendecas@gmail.com>
1 parent a9c3d92 commit 209c803

File tree

2 files changed

+60
-1
lines changed

2 files changed

+60
-1
lines changed

lib/internal/v8/startup_snapshot.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,8 @@ function setDeserializeMainFunction(callback, data) {
9090

9191
// This should be in sync with run_main_module.js until we make that
9292
// a built-in main function.
93-
prepareMainThreadExecution(true);
93+
// TODO(joyeecheung): make a copy of argv[0] and insert it as argv[1].
94+
prepareMainThreadExecution(false);
9495
markBootstrapComplete();
9596
callback(data);
9697
});

test/parallel/test-snapshot-argv1.js

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
'use strict';
2+
3+
// This tests snapshot JS API using the example in the docs.
4+
5+
require('../common');
6+
const assert = require('assert');
7+
const { spawnSync } = require('child_process');
8+
const tmpdir = require('../common/tmpdir');
9+
const path = require('path');
10+
const fs = require('fs');
11+
12+
tmpdir.refresh();
13+
const blobPath = path.join(tmpdir.path, 'snapshot.blob');
14+
const code = `
15+
require('v8').startupSnapshot.setDeserializeMainFunction(() => {
16+
console.log(JSON.stringify(process.argv));
17+
});
18+
`;
19+
{
20+
fs.writeFileSync(path.join(tmpdir.path, 'entry.js'), code, 'utf8');
21+
const child = spawnSync(process.execPath, [
22+
'--snapshot-blob',
23+
blobPath,
24+
'--build-snapshot',
25+
'entry.js',
26+
], {
27+
cwd: tmpdir.path
28+
});
29+
if (child.status !== 0) {
30+
console.log(child.stderr.toString());
31+
console.log(child.stdout.toString());
32+
assert.strictEqual(child.status, 0);
33+
}
34+
const stats = fs.statSync(path.join(tmpdir.path, 'snapshot.blob'));
35+
assert(stats.isFile());
36+
}
37+
38+
{
39+
const child = spawnSync(process.execPath, [
40+
'--snapshot-blob',
41+
blobPath,
42+
'argv1',
43+
'argv2',
44+
], {
45+
cwd: tmpdir.path,
46+
env: {
47+
...process.env,
48+
}
49+
});
50+
51+
const stdout = JSON.parse(child.stdout.toString().trim());
52+
assert.deepStrictEqual(stdout, [
53+
process.execPath,
54+
'argv1',
55+
'argv2',
56+
]);
57+
assert.strictEqual(child.status, 0);
58+
}

0 commit comments

Comments
 (0)