Skip to content

repl: clear the typed line on Ctrl+C in every mode, evaluate it on Ctrl+D in editor mode - #46083

Open
robobun wants to merge 1 commit into
mainfrom
robobun/92ebb905/repl-ctrl-c-clears-typed-line
Open

robobun wants to merge 1 commit into
mainfrom
robobun/92ebb905/repl-ctrl-c-clears-typed-line

Conversation

@robobun

@robobun robobun commented Oct 19, 2026 •

Copy link
Copy Markdown
Collaborator

Problem

  • In bun repl, Ctrl+C in a continuation or .editor mode keeps the line being typed. foo( Enter, bar, Ctrl+C, 2+2 Enter answers ReferenceError: bar2 is not defined.
  • Ctrl+D in .editor mode leaves that line at the next prompt.
  • Cause: handle_ctrl_c (src/runtime/cli/repl.rs:2621) clears line_editor only in its plain-prompt arm. The Ctrl+D editor arm (:2361) never reads it.

Fix

  • handle_ctrl_c clears the line editor before the match on the mode. The Ctrl+D editor arm appends the typed line to editor_buffer, then resets the history position.
  • Correct: the docs say .editor "buffers all input until you press Ctrl+D", and Node does both.
  • Verified: test/js/bun/repl/repl.test.ts, 10 new tests, 9 fail without the fix.
  • Self-reviewed: 11 concerns raised, 8 addressed. Rejected: a history reset on every Enter, which breaks Enter, Down replay.

Background

  • The REPL has a plain prompt, a continuation (multiline_buffer) and an editor mode (editor_buffer). line_editor holds the typed line. reset_position returns Up and Down to the newest entry.
  • Considered a clear in each Ctrl+C arm: Ctrl+D stays wrong. Considered one shared reset for every return to the prompt: same result, more work on Enter.

Downsides

  • Release build: Repl::run_with_vm +218 B. Ctrl+C at an empty prompt: 973 to 979 instructions. Editor Ctrl+D, empty line: 417 to 426.
  • A dot-command on the line that Ctrl+D takes is code. Node runs it as a command.
  • On piped stdin, a mid-line cancel now writes the > prompt.
Notes

Repro (piped stdin reads the same keys as a terminal: byte 0x03 is Ctrl+C, 0x04 is Ctrl+D)

Keys main this PR node 26.3
foo( Enter, bar, Ctrl+C, 2 + 2 Enter ReferenceError: bar2 is not defined 4 4
.editor, 1 Enter, bar, Ctrl+C, 3 + 3 Enter ReferenceError: bar3 is not defined 6 6
.editor, 1 + 1 Enter, 40 + 2, Ctrl+D 2, and 40 + 2 stays at the prompt 42 42

printf 'foo(\nbar\0032 + 2\n' | bun repl shows the first row. On a pty the fresh prompt reads > bar on main and > with this change.

What stays the same

  • ^C is echoed behind a line cancelled at the plain prompt. A line cancelled in a continuation or in editor mode gets no echo, as before.
  • Ctrl+C on an empty line prints the hint, and the second press in a row exits. A key in between starts the count again.
  • // Editor mode cancelled, and the history reset on Ctrl+C.
  • After Ctrl+C cancels a continuation or an editor input, two more presses exit. Before, the leftover line took one press more.

Not changed here

Each of the first five is handed to its own follow-up change. None of them is a GitHub issue.

Ctrl+D rule: Ctrl+D with text on the line behaves as Enter, then Ctrl+D. The line is added with its newline, blanks count as input (.editor, three spaces, Ctrl+D prints undefined, as blanks, Enter, Ctrl+D did before), and an empty line adds nothing. Editor mode never dispatched dot-commands on entered lines, so the typed line is not dispatched either.

Each clause of the Ctrl+D arm is pinned by a test (checked by removing the clause, then running the Ctrl+D tests)

  • without the emptiness guard: Ctrl+D in editor mode evaluates the line being typed fails (.editor, Ctrl+D prints undefined)
  • without the pushed newline: Ctrl+D in editor mode takes the line being typed the way Enter takes a line fails ({ a: 1 } // c prints 1)
  • without the history reset: Ctrl+D in editor mode resets the history position fails (Up recalls 100 + 11)
  • the clear in handle_ctrl_c and the append are pinned by the tests that fail on main

Measurements. Release builds of 6bb5dc1 and of the same tree with this change to repl.rs, built in one directory with one toolchain, Linux x64.

  • Size (llvm-nm -S): the repl module has 35 symbols in both builds, 38,982 B against 39,200 B. The +218 B are all in Repl::run_with_vm (15,360 to 15,578 B). No new symbol, no new host function. Text of the whole binary: +256 B. The stripped file is 107,235,040 B in both.
  • Ctrl+C at an empty prompt (gdb stepi over one iteration of the key loop, piped stdin, one key per read(2), 3 identical runs per build): 973 against 979 instructions, 108 conditional branches, 24 calls and 7 syscalls in both.
  • Ctrl+D in editor mode with an empty line, outside the evaluation (same method): 417 against 426 instructions, 50 against 52 conditional branches, 12 calls and 3 syscalls in both.
  • Allocator entries per cycle (gdb counters on the mi_* entry points, 50 against 100 cycles): bar Ctrl+C 0, foo( Enter .break Enter 2, foo( Enter bar Ctrl+C 1, the same in both builds. The evaluating scripts (1 Enter, and .editor 1+1 Enter Ctrl+D) make about 23 mi_malloc and 4 mi_realloc per cycle in both builds. They vary by about 0.2 per cycle from run to run. Ctrl+D with a typed line, the new path: 23 and 4.
  • Piped stdout (cmp): identical on the 27 piped inputs of the existing tests and on four scripts that do not hit the bug. Per cancelled continuation line the PR writes 3 write(2) and 4 bytes, main 2 and 2 (from /proc/<pid>/io).
  • Pty: for Ctrl+C in a continuation with a half line the REPL makes 12 write(2) (main 13). The terminal receives \r ESC[7C \r\n \r ESC[J > \r ESC[2C (main: ... > bar \r ESC[5C).

Platform. No cfg arm is touched. The six piped tests are not gated and run on Windows in CI. The four pty tests are in the existing describe.todoIf(isWindows) block. Nothing was run on Windows or macOS locally.

Suites run: bun bd test test/js/bun/repl/repl.test.ts (139 pass). With the released bun the same file fails 9 of the 10 new tests. The tenth pins the double Ctrl+C exit, which did not change.

@robobun

robobun commented Oct 19, 2026 •

Copy link
Copy Markdown
Collaborator Author
Updated 3:52 AM PT - Oct 21st, 2026

✅ @robobun, your commit cf3cfefb74bc26b9452184becd50d2e9bd1c8ec3 passed in Build #129874! 🎉


🧪   To try this PR locally:

bunx bun-pr 46083

That installs a local version of the PR into your bun-46083 executable, so you can run:

bun-46083 --bun

@robobun

robobun commented Oct 19, 2026

Copy link
Copy Markdown
Collaborator Author

Status

Reproduced on Linux x64, on a pty and on piped stdin, with the released build and with a debug build of main:

  • printf 'foo(\nbar\0032 + 2\n' | bun repl prints ReferenceError: bar2 is not defined. With this change it prints 4.
  • printf '.editor\n1 + 1\n40 + 2\004' | bun repl prints 2. With this change it prints 42.

Byte \003 is Ctrl+C and \004 is Ctrl+D. The fix and its tests are in this PR (#46083).

@claude claude Bot left a comment

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.

Code review found no issues

No high-confidence issues detected in this change.

@coderabbitai

coderabbitai Bot commented Oct 19, 2026 •

Copy link
Copy Markdown
Contributor

Warning

Review limit reached

You've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository.

Next included review available in 13 minutes.

Check out review usage here.

View limit details

Limit details: You’ve used all 10 included reviews currently available.

Learn how review limits work.

Review configuration:

⚙️ Run configuration
  • Configuration used: Repository: oven-sh/bun/.coderabbit.yaml
  • Review profile: ASSERTIVE
  • Plan: Advanced
  • Run ID: 507278b8-767b-4f11-8bef-b1de93c83ebc
📥 Commits

Reviewing files that changed from the base of the PR and between 71bc818 and cf3cfef.

📒 Files selected for processing: 2
  • src/runtime/cli/repl.rs
  • test/js/bun/repl/repl.test.ts
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@robobun

robobun commented Oct 20, 2026

Copy link
Copy Markdown
Collaborator Author

Overlap note: #46248 has the line_editor.clear() in handle_ctrl_c and the Ctrl+D change in .editor of this PR. A bracketed paste that waits on the line needs both. #46248 also makes a pipe hold text, not key presses. After it, the 6 piped tests of this PR that send 0x03 and 0x04 as keys need a pty.

…rl+D in editor mode

Ctrl+C in a continuation or in editor mode dropped the earlier lines but
left the line being typed in the line editor, so it stood at the next
prompt and the next input was appended to it. handle_ctrl_c now clears
the line editor once, before it looks at the mode.

Ctrl+D in editor mode evaluated the entered lines and also left the line
being typed at the next prompt. It now appends that line to the editor
input before the evaluation. It also resets the history position, as
Ctrl+C, .break and an evaluating Enter do when they return to the plain
prompt.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants