Skip to content

Commit 614bb9f

Browse files
joyeecheungtargos
authored andcommitted
process: normalize process.argv before user code execution
And make sure that `process.argv` from the preloaded modules is the same as the one in the main module. Refs: #25967 PR-URL: #26000 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Richard Lau <[email protected]> Reviewed-By: Minwoo Jung <[email protected]>
1 parent 9a7e883 commit 614bb9f

File tree

3 files changed

+48
-5
lines changed

3 files changed

+48
-5
lines changed

lib/internal/main/check_syntax.js

+8-3
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,6 @@ const {
1717
stripShebang, stripBOM
1818
} = require('internal/modules/cjs/helpers');
1919

20-
// TODO(joyeecheung): not every one of these are necessary
21-
prepareMainThreadExecution();
22-
markBootstrapComplete();
2320

2421
if (process.argv[1] && process.argv[1] !== '-') {
2522
// Expand process.argv[1] into a full path.
@@ -31,8 +28,16 @@ if (process.argv[1] && process.argv[1] !== '-') {
3128
const fs = require('fs');
3229
const source = fs.readFileSync(filename, 'utf-8');
3330

31+
// TODO(joyeecheung): not every one of these are necessary
32+
prepareMainThreadExecution();
33+
markBootstrapComplete();
34+
3435
checkScriptSyntax(source, filename);
3536
} else {
37+
// TODO(joyeecheung): not every one of these are necessary
38+
prepareMainThreadExecution();
39+
markBootstrapComplete();
40+
3641
readStdin((code) => {
3742
checkScriptSyntax(code, '[stdin]');
3843
});

lib/internal/main/run_main_module.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@ const {
44
prepareMainThreadExecution
55
} = require('internal/bootstrap/pre_execution');
66

7-
prepareMainThreadExecution();
8-
97
// Expand process.argv[1] into a full path.
108
const path = require('path');
119
process.argv[1] = path.resolve(process.argv[1]);
1210

11+
prepareMainThreadExecution();
12+
1313
const CJSModule = require('internal/modules/cjs/loader');
1414

1515
markBootstrapComplete();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
'use strict';
2+
3+
// This tests that process.argv is the same in the preloaded module
4+
// and the user module.
5+
6+
const common = require('../common');
7+
8+
const tmpdir = require('../common/tmpdir');
9+
const assert = require('assert');
10+
const { spawnSync } = require('child_process');
11+
const fs = require('fs');
12+
13+
if (!common.isMainThread) {
14+
common.skip('Cannot chdir to the tmp directory in workers');
15+
}
16+
17+
tmpdir.refresh();
18+
19+
process.chdir(tmpdir.path);
20+
fs.writeFileSync(
21+
'preload.js',
22+
'console.log(JSON.stringify(process.argv));',
23+
'utf-8');
24+
25+
fs.writeFileSync(
26+
'main.js',
27+
'console.log(JSON.stringify(process.argv));',
28+
'utf-8');
29+
30+
const child = spawnSync(process.execPath, ['-r', './preload.js', 'main.js']);
31+
32+
if (child.status !== 0) {
33+
console.log(child.stderr.toString());
34+
assert.strictEqual(child.status, 0);
35+
}
36+
37+
const lines = child.stdout.toString().trim().split('\n');
38+
assert.deepStrictEqual(JSON.parse(lines[0]), JSON.parse(lines[1]));

0 commit comments

Comments
 (0)