From e5397a6a157ae475b28ef8015ce9577e85e39a54 Mon Sep 17 00:00:00 2001 From: Anna Henningsen Date: Mon, 6 May 2019 21:54:32 +0200 Subject: [PATCH 1/2] repl: do not run --eval code if there is none `getOptionValue('--eval')` always returns a string, so it is never loose-equal to `null`. Running eval makes some modifications to the global object, including setting `module` to a different value, which we want to avoid if possible. Refs: https://github.com/nodejs/node/pull/27278 Fixes: https://github.com/nodejs/node/issues/27575 --- lib/internal/main/repl.js | 5 ++--- test/parallel/test-repl-cli-eval.js | 22 ++++++++++++++++++++++ 2 files changed, 24 insertions(+), 3 deletions(-) create mode 100644 test/parallel/test-repl-cli-eval.js diff --git a/lib/internal/main/repl.js b/lib/internal/main/repl.js index 58afb2be9879fa..b38102a15482fd 100644 --- a/lib/internal/main/repl.js +++ b/lib/internal/main/repl.js @@ -46,10 +46,9 @@ cliRepl.createInternalRepl(process.env, (err, repl) => { // If user passed '-e' or '--eval' along with `-i` or `--interactive`, // evaluate the code in the current context. -const source = getOptionValue('--eval'); -if (source != null) { +if (getOptionValue('[has_eval_string]')) { evalScript('[eval]', - source, + getOptionValue('--eval'), getOptionValue('--inspect-brk'), getOptionValue('--print')); } diff --git a/test/parallel/test-repl-cli-eval.js b/test/parallel/test-repl-cli-eval.js new file mode 100644 index 00000000000000..6069a20957bd25 --- /dev/null +++ b/test/parallel/test-repl-cli-eval.js @@ -0,0 +1,22 @@ +'use strict'; +const common = require('../common'); +const child_process = require('child_process'); +const assert = require('assert'); + +// Regression test for https://github.com/nodejs/node/issues/27575: +// module.id === '' in the REPL. + +for (const extraFlags of [[], ['-e', '42']]) { + const flags = ['--interactive', ...extraFlags]; + const proc = child_process.spawn(process.execPath, flags, { + stdio: ['pipe', 'pipe', 'inherit'] + }); + proc.stdin.write('module.id\n.exit\n'); + + let stdout = ''; + proc.stdout.setEncoding('utf8'); + proc.stdout.on('data', (chunk) => stdout += chunk); + proc.stdout.on('end', common.mustCall(() => { + assert(stdout.includes(''), `stdout: ${stdout}`); + })); +} From 9ee91763e52ea702bb0d4997e32432e49782bc9a Mon Sep 17 00:00:00 2001 From: Anna Henningsen Date: Tue, 7 May 2019 00:44:08 +0200 Subject: [PATCH 2/2] lib: restore `global.module` after --eval code is run Refs: https://github.com/nodejs/node/pull/27278 --- lib/internal/process/execution.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/lib/internal/process/execution.js b/lib/internal/process/execution.js index c95567c506237f..5eda7541c8e4e1 100644 --- a/lib/internal/process/execution.js +++ b/lib/internal/process/execution.js @@ -55,6 +55,7 @@ function evalScript(name, body, breakFirstLine, printResult) { const { kVmBreakFirstLineSymbol } = require('internal/util'); const cwd = tryGetCwd(); + const origModule = global.module; // Set e.g. when called from the REPL. const module = new CJSModule(name); module.filename = path.join(cwd, name); @@ -79,6 +80,9 @@ function evalScript(name, body, breakFirstLine, printResult) { const { kStdout, print } = require('internal/util/print'); print(kStdout, result); } + + if (origModule !== undefined) + global.module = origModule; } const exceptionHandlerState = { captureFn: null };