Skip to content

repl: fix crash when bare 'import' is typed#63552

Open
WaqarNiaxi wants to merge 2 commits into
nodejs:mainfrom
WaqarNiaxi:fix/repl-import-crash-63551
Open

repl: fix crash when bare 'import' is typed#63552
WaqarNiaxi wants to merge 2 commits into
nodejs:mainfrom
WaqarNiaxi:fix/repl-import-crash-63551

Conversation

@WaqarNiaxi
Copy link
Copy Markdown

Typing a bare import keyword in the REPL followed by Enter caused
the process to crash with an uncaught SyntaxError from acorn. The
crash path was:

  1. V8 evaluates import\n and throws a SyntaxError:
    "Cannot use import statement outside a module".
  2. The REPL error handler calls toDynamicImport() to build a helpful
    hint message.
  3. toDynamicImport() called acornParse() without a try-catch.
  4. acorn threw SyntaxError: Unexpected token (1:6) because the bare
    import is not a complete import statement.
  5. The uncaught error propagated synchronously back through the
    keypress event chain and was re-thrown in emitKeypressEvents:74,
    crashing the process.

Fix: wrap the acornParse() call in toDynamicImport() in a try-catch.
On parse failure, return the original codeLine unchanged so the
error message is still usable.

Fixes: #63551"

@nodejs-github-bot nodejs-github-bot added needs-ci PRs that need a full CI run. repl Issues and PRs related to the REPL subsystem. labels May 25, 2026
Comment on lines +8 to +13
const proc = child_process.spawn(process.execPath, ['-i']);
proc.on('error', common.mustNotCall());
proc.on('exit', common.mustCall((code) => {
assert.strictEqual(code, 0);
}));
proc.stdin.write('import\n.exit\n');
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we avoid the spawn here? Something similar to

'use strict';
require('../common');
const repl = require('repl');
const replserver = new repl.REPLServer();
replserver._inTemplateLiteral = true;
// `null` gets treated like an empty string. (Should it? You have to do some
// strange business to get it into the REPL. Maybe it should really throw?)
replserver.emit('line', null);
replserver.emit('line', '.exit');

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Resolved

@codecov
Copy link
Copy Markdown

codecov Bot commented Jun 1, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 90.30%. Comparing base (2e3daf6) to head (3e297b7).
⚠️ Report is 71 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main   #63552      +/-   ##
==========================================
- Coverage   90.34%   90.30%   -0.04%     
==========================================
  Files         730      730              
  Lines      234359   235482    +1123     
  Branches    43923    44387     +464     
==========================================
+ Hits       211720   212648     +928     
- Misses      14361    14631     +270     
+ Partials     8278     8203      -75     
Files with missing lines Coverage Δ
lib/repl.js 94.03% <100.00%> (+0.01%) ⬆️

... and 203 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Signed-off-by: waqar bin zafar <waqarniazi51@gmail.com>
@WaqarNiaxi WaqarNiaxi force-pushed the fix/repl-import-crash-63551 branch from 3e297b7 to 78fa809 Compare June 1, 2026 09:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

needs-ci PRs that need a full CI run. repl Issues and PRs related to the REPL subsystem.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Using 'import' crashes the REPL

3 participants