Skip to content

Commit 688a0bd

Browse files
addaleaxtargos
authored andcommitted
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: #27278 PR-URL: #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]>
1 parent 3c70797 commit 688a0bd

File tree

2 files changed

+24
-3
lines changed

2 files changed

+24
-3
lines changed

lib/internal/main/repl.js

+2-3
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,9 @@ cliRepl.createInternalRepl(process.env, (err, repl) => {
4646

4747
// If user passed '-e' or '--eval' along with `-i` or `--interactive`,
4848
// evaluate the code in the current context.
49-
const source = getOptionValue('--eval');
50-
if (source != null) {
49+
if (getOptionValue('[has_eval_string]')) {
5150
evalScript('[eval]',
52-
source,
51+
getOptionValue('--eval'),
5352
getOptionValue('--inspect-brk'),
5453
getOptionValue('--print'));
5554
}

test/parallel/test-repl-cli-eval.js

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
'use strict';
2+
const common = require('../common');
3+
const child_process = require('child_process');
4+
const assert = require('assert');
5+
6+
// Regression test for https://github.com/nodejs/node/issues/27575:
7+
// module.id === '<repl>' in the REPL.
8+
9+
for (const extraFlags of [[], ['-e', '42']]) {
10+
const flags = ['--interactive', ...extraFlags];
11+
const proc = child_process.spawn(process.execPath, flags, {
12+
stdio: ['pipe', 'pipe', 'inherit']
13+
});
14+
proc.stdin.write('module.id\n.exit\n');
15+
16+
let stdout = '';
17+
proc.stdout.setEncoding('utf8');
18+
proc.stdout.on('data', (chunk) => stdout += chunk);
19+
proc.stdout.on('end', common.mustCall(() => {
20+
assert(stdout.includes('<repl>'), `stdout: ${stdout}`);
21+
}));
22+
}

0 commit comments

Comments
 (0)