Skip to content

Commit 0165a76

Browse files
joyeecheungMoLow
authored andcommitted
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 a0b9853 commit 0165a76

2 files changed

Lines changed: 60 additions & 1 deletion

File tree

‎lib/internal/v8/startup_snapshot.js‎

Lines changed: 2 additions & 1 deletion
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
});
Lines changed: 58 additions & 0 deletions
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+
constassert=require('assert');
7+
const{ spawnSync }=require('child_process');
8+
consttmpdir=require('../common/tmpdir');
9+
constpath=require('path');
10+
constfs=require('fs');
11+
12+
tmpdir.refresh();
13+
constblobPath=path.join(tmpdir.path,'snapshot.blob');
14+
constcode=`
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+
constchild=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+
conststats=fs.statSync(path.join(tmpdir.path,'snapshot.blob'));
35+
assert(stats.isFile());
36+
}
37+
38+
{
39+
constchild=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+
conststdout=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)