Skip to content

Commit

Permalink
process: normalize process.argv before user code execution
Browse files Browse the repository at this point in the history
And make sure that `process.argv` from the preloaded modules
is the same as the one in the main module.

Refs: #25967
  • Loading branch information
joyeecheung committed Feb 9, 2019
1 parent 8d6105b commit 5ea16b0
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 5 deletions.
11 changes: 8 additions & 3 deletions lib/internal/main/check_syntax.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,6 @@ const {
stripShebang, stripBOM
} = require('internal/modules/cjs/helpers');

// TODO(joyeecheung): not every one of these are necessary
prepareMainThreadExecution();
markBootstrapComplete();

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

// TODO(joyeecheung): not every one of these are necessary
prepareMainThreadExecution();
markBootstrapComplete();

checkScriptSyntax(source, filename);
} else {
// TODO(joyeecheung): not every one of these are necessary
prepareMainThreadExecution();
markBootstrapComplete();

readStdin((code) => {
checkScriptSyntax(code, '[stdin]');
});
Expand Down
4 changes: 2 additions & 2 deletions lib/internal/main/run_main_module.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ const {
prepareMainThreadExecution
} = require('internal/bootstrap/pre_execution');

prepareMainThreadExecution();

// Expand process.argv[1] into a full path.
const path = require('path');
process.argv[1] = path.resolve(process.argv[1]);

prepareMainThreadExecution();

const CJSModule = require('internal/modules/cjs/loader');

markBootstrapComplete();
Expand Down
34 changes: 34 additions & 0 deletions test/parallel/test-preload-print-process-argv.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
'use strict';

// This tests that process.argv is the same in the preloaded module
// and the user module.

require('../common');

const tmpdir = require('../common/tmpdir');
const assert = require('assert');
const { spawnSync } = require('child_process');
const fs = require('fs');

tmpdir.refresh();

process.chdir(tmpdir.path);
fs.writeFileSync(
'preload.js',
'console.log(JSON.stringify(process.argv));',
'utf-8');

fs.writeFileSync(
'main.js',
'console.log(JSON.stringify(process.argv));',
'utf-8');

const child = spawnSync(process.execPath, ['-r', './preload.js', 'main.js']);

if (child.status !== 0) {
console.log(child.stderr.toString());
assert.strictEqual(child.status, 0);
}

const lines = child.stdout.toString().trim().split('\n');
assert.deepStrictEqual(JSON.parse(lines[0]), JSON.parse(lines[1]));

0 comments on commit 5ea16b0

Please sign in to comment.