From 69714ab1c44f45f7949484ab7f4574f3ad9894ce Mon Sep 17 00:00:00 2001 From: Joyee Cheung Date: Fri, 8 Feb 2019 11:22:37 +0800 Subject: [PATCH] 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: https://github.com/nodejs/node/issues/25967 PR-URL: https://github.com/nodejs/node/pull/26000 Reviewed-By: Anna Henningsen Reviewed-By: Richard Lau Reviewed-By: Minwoo Jung --- lib/internal/main/check_syntax.js | 11 ++++-- lib/internal/main/run_main_module.js | 4 +- .../test-preload-print-process-argv.js | 38 +++++++++++++++++++ 3 files changed, 48 insertions(+), 5 deletions(-) create mode 100644 test/parallel/test-preload-print-process-argv.js diff --git a/lib/internal/main/check_syntax.js b/lib/internal/main/check_syntax.js index 5d98701132f0e8..392fadb99ff668 100644 --- a/lib/internal/main/check_syntax.js +++ b/lib/internal/main/check_syntax.js @@ -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. @@ -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]'); }); diff --git a/lib/internal/main/run_main_module.js b/lib/internal/main/run_main_module.js index 97a4e33be2e629..634abe493ea60e 100644 --- a/lib/internal/main/run_main_module.js +++ b/lib/internal/main/run_main_module.js @@ -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(); diff --git a/test/parallel/test-preload-print-process-argv.js b/test/parallel/test-preload-print-process-argv.js new file mode 100644 index 00000000000000..ace20dfb3947c4 --- /dev/null +++ b/test/parallel/test-preload-print-process-argv.js @@ -0,0 +1,38 @@ +'use strict'; + +// This tests that process.argv is the same in the preloaded module +// and the user module. + +const common = require('../common'); + +const tmpdir = require('../common/tmpdir'); +const assert = require('assert'); +const { spawnSync } = require('child_process'); +const fs = require('fs'); + +if (!common.isMainThread) { + common.skip('Cannot chdir to the tmp directory in workers'); +} + +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]));