Skip to content

Commit

Permalink
repl: do not run --eval code if there is none
Browse files Browse the repository at this point in the history
`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: nodejs#27278
PR-URL: nodejs#27587
Reviewed-By: Ruben Bridgewater <[email protected]>
Reviewed-By: Rich Trott <[email protected]>
Reviewed-By: John-David Dalton <[email protected]>
Reviewed-By: Colin Ihrig <[email protected]>
Reviewed-By: Gus Caplan <[email protected]>
Reviewed-By: Franziska Hinkelmann <[email protected]>
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Joyee Cheung <[email protected]>
  • Loading branch information
addaleax committed May 12, 2019
1 parent 1d31c68 commit f2a48c8
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
5 changes: 2 additions & 3 deletions lib/internal/main/repl.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
}
22 changes: 22 additions & 0 deletions test/parallel/test-repl-cli-eval.js
Original file line number Diff line number Diff line change
@@ -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 === '<repl>' 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('<repl>'), `stdout: ${stdout}`);
}));
}

0 comments on commit f2a48c8

Please sign in to comment.