Skip to content

repl: add code evaluated from .editor mode to the history - #46449

Open
robobun wants to merge 1 commit into
mainfrom
robobun/53f11ec2/repl-editor-history
Open

robobun wants to merge 1 commit into
mainfrom
robobun/53f11ec2/repl-editor-history

Conversation

@robobun

@robobun robobun commented Oct 22, 2026 •

Copy link
Copy Markdown
Collaborator

Problem

Fix

  • The arm calls self.history.add(strings::trim(&code, b"\n")) before evaluate_and_print. No other source line changes.
  • One editor run is one entry, as handle_enter stores a ... continuation.
  • Verified: test/js/bun/repl/repl.test.ts, 5 new tests, 4 fail on main. The whole file passes.
  • Self-reviewed: 15 concerns raised, 9 addressed in full, 6 in part (Notes).

Background

  • Editor mode collects lines in editor_buffer until Ctrl+D, then evaluates them as one input. Up, .history, .save and the history file read History.
  • Considered one entry per line at each Enter, as Node does: it records lines that Ctrl+C cancels. Considered the add inside evaluate_and_print: .load files run through it.

Downsides

Notes

Origin. No user reported this and no issue exists. The body of #46083 lists it under "Not changed here": "Code evaluated from .editor is not added to the history." It hands the item to a follow-up change, and this PR is that change. The arm has had no history.add since the first native REPL (#26304, src/repl.zig:1576 against :1762). No test or comment makes the omission a decision. docs/runtime/repl.mdx sends longer multi-line input to .editor (line 105). It documents .save as the export of the history (lines 120 and 153).

Repro. Piped stdin reads the same keys as a terminal: byte 0x04 is Ctrl+D, ESC [ A is Up.

printf '1 + 1\n.editor\n7 * 6\n\004\033[A\n' | HOME=$(mktemp -d) bun repl

Keys main this PR
1 + 1, .editor, 7 * 6, Ctrl+D, Up, Enter 2, 42, 2 2, 42, 42
.editor, let a = 7, a * 6, Ctrl+D, .history no entry one entry, let a = 7 and a * 6
.editor, let a = 7, Ctrl+D, a * 6, .save f.js, then .load f.js in a new session ReferenceError: a is not defined 42
.editor, 7 * 6, Ctrl+D, .exit, then read ~/.bun_repl_history no file 7 * 6

Entry shape. One entry per editor run, with the outer newlines trimmed and the inner ones kept. It is the call handle_enter makes for a continuation (repl.rs:2608), in the same order: add, then evaluate.

Node 26.3.0 (measured on a pty) makes each entered line its own entry at its Enter. After .editor, let a = 7, a * 6, Ctrl+D, Up recalls a * 6 and the next Up recalls let a = 7. After .editor, 99 + 1, Ctrl+C, Up recalls 99 + 1. That shape does not fit here:

  • bun repl has one list for recall and for .save. History::add skips a blank line and a line equal to the entry before it. So per-line entries make .save lose blank lines and the second of two equal lines. Node keeps a second list for .save.
  • It records the lines of an input that Ctrl+C cancels.

The tests the evaluated input is one entry and up arrow recalls the whole evaluated input pin the shape.

Tests (test/js/bun/repl/repl.test.ts, two history of .editor mode blocks)

  • Piped, on every platform:
    • the evaluated input is one entry: .history numbers it once and .save writes it.
    • the entry keeps its inner blank lines and drops the outer ones: the trim.
    • the entry is in the history file for the next session.
  • pty, inside describe.todoIf(isWindows):
    • up arrow recalls the whole evaluated input: Up, Enter runs both lines again and prints 20. The last line alone prints 10.
    • input cancelled with Ctrl+C is not an entry: a control, it passes on main.
  • The piped tests put 0x04 at the start of a line and use no arrow keys. So they hold when repl: read non-terminal stdin as lines of text and take bracketed pastes as text #46248 reads a pipe as lines of text.
  • Release build of main 9634520: 130 pass, 4 fail (the four new tests that are not the control). Release build of that commit with this change: 134 pass. Debug ASAN build of this branch: 134 pass.

Measurements. Release builds of main 9634520 and of that commit with this change, in one build directory with one toolchain, Linux x64. Only repl.rs differs.

  • Size (size, llvm-nm -S): text 107,351,751 to 107,352,007 B (+256). The stripped file is 107,525,856 B in both. History::add has two callers now and is no longer inlined into Repl::run_with_vm. It is a 459 B function. run_with_vm goes from 14,350 to 14,043 B. The repl module has 33 function symbols and 37,912 B on main, 34 and 38,064 B here.
  • Instructions inside the functions of the repl module, from a qemu-x86_64 exec trace with one instruction per block. Stdin comes from a file, and the count is (40 inputs - 20 inputs) / 20. An accepted prompt Enter goes from 993.45 to 1009.40 (+15.95). One editor run (.editor, one line, Ctrl+D) goes from 2008.45 to 2122.85 (+114.40).
  • Allocator calls made by the repl module: an accepted prompt Enter makes 4 malloc and 6 free in both builds. An editor submit makes 4 malloc (main: 3, the new one is the entry) and 6 free in both.
  • Syscalls on the history file (qemu-x86_64 -strace): a prompt-only session makes the same 5 in both builds. An editor-only session writes no file on main. Here it makes one openat, fchmod, write and close at exit.
  • History capacity: with 1000 seeded entries, one 50-line editor block and a restart keep 950 of them. Main keeps 1000, because it drops the block. The same 50 lines typed as a continuation keep 948 in both builds. repl: keep multi-line history entries on one line of the history file #38023 stores an entry on one line of the file.
  • perf, valgrind, strace and bloaty are not installed on the machine.

Recalled entries at the prompt. Ctrl+D evaluates the buffer as it is. Up, Enter enters the recalled text as a typed line, so the checks of handle_enter apply. Before this change no .editor code reached them through the history. Each row ran on both release builds.

Not changed here

Relation to open PRs

Self-review, not taken

Platform. No cfg arm is touched. Byte 4 is Ctrl+D on every platform (Key::from_byte). Nothing ran on Windows or macOS locally. On main no test sends 0x04 through a pipe on Windows, and no test outside skipIf(isWindows) reads the history file. The three piped tests are the first to do so.

Suites run. bun bd test test/js/bun/repl/repl.test.ts: 134 pass. One debug run of four had 5 timeouts in pty tests that do not use .editor (inline suggestions, wrapped input). The same binary passed twice after that. rustfmt --check on repl.rs and prettier --check on the test file are clean.


no test proof · iteration 0 · platform-specific test(s) that do not run on this machine, deferring to CI, which covers all platforms: test/js/bun/repl/repl.test.ts

The Ctrl+D arm for editor mode evaluated `editor_buffer` and never
called `History::add`. Up, `.history`, `.save` and the history file
did not have the code.

The arm now adds the buffer as one entry before the evaluation, with
the surrounding newlines trimmed. `handle_enter` does the same for a
continuation.
@coderabbitai

coderabbitai Bot commented Oct 22, 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 2 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: 23c31b71-e5dc-49c6-8921-f1cd506eb486
📥 Commits

Reviewing files that changed from the base of the PR and between 0a340f0 and 6715a57.

📒 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 22, 2026 •

Copy link
Copy Markdown
Collaborator Author
Updated 2:05 AM PT - Oct 22nd, 2026

❌ @robobun, your commit 6715a57 has some failures in Build #130643 (All Failures)


🧪   To try this PR locally:

bunx bun-pr 46449

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

bun-46449 --bun

@robobun

robobun commented Oct 22, 2026

Copy link
Copy Markdown
Collaborator Author

Status: the fix is in this PR, #46449.

Repro (Linux x64, main 9634520). Byte 0x04 is Ctrl+D and ESC [ A is Up:

printf '1 + 1\n.editor\n7 * 6\n\004\033[A\n' | HOME=$(mktemp -d) bun repl

Main prints 2, 42, 2: Up after the editor evaluation recalls 1 + 1. .history, .save and ~/.bun_repl_history do not have 7 * 6. With this PR the same keys print 2, 42, 42.

@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.

Nothing blocking. The comments below are optional suggestions. There is no need to push a fix for them before merging.

Comment thread src/runtime/cli/repl.rs
// Note: reshaped for borrowck — clone editor_buffer slice before evaluate
if !self.editor_buffer.is_empty() {
let code = core::mem::take(&mut self.editor_buffer);
self.history.add(strings::trim(&code, b"\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.

🟡 (optional) pre-existing class, widened: after a restart, users who wrote multi-line code in .editor get it back from Up as one fragment per line, not as the block this PR records. src/runtime/cli/repl.rs:2368 stores the whole block as one entry with inner "\n", but History::load at repl.rs:216 splits the history file on "\n", so the next session has N single-line entries. Recalling an unindented line such as .then(r => r.json()) and pressing Enter runs it as a command and prints Unknown command: .then(r (repl.rs:2536-2564). Fix: persist one history entry per file record (escape or encode inner newlines on save and decode on load, as #38023 proposes) and add a multi-line variant of the "next session" test.

Why this was flagged

A user runs .editor, types fetch(u) then .then(r => r.json()) on two lines, presses Ctrl+D and exits. src/runtime/cli/repl.rs:2368 adds the block as one entry fetch(u)\n.then(r => r.json()); History::save at repl.rs:247-250 writes it with its inner newline into ~/.bun_repl_history. In the next session History::load at repl.rs:216-220 splits the file on "\n" and pushes fetch(u) and .then(r => r.json()) as two entries. Up then recalls .then(r => r.json()) alone; Enter goes through handle_enter at repl.rs:2536, which treats a leading . as a command and prints Unknown command: .then(r at repl.rs:2564. On the base branch editor blocks never reach the file, so only ... continuation blocks were split this way; the PR routes every .editor block, which docs/runtime/repl.mdx:105 recommends for longer multi-line code, into the same lossy format. The new test at test/js/bun/repl/repl.test.ts:540 only persists the single-line input "7 * 6", so the split is not exercised.

Verification: src/runtime/cli/repl.rs:2368 self.history.add(strings::trim(&code, b"\n"))? stores the whole editor buffer as one entry with inner \n kept. History::load at :216-220 does for line in strings::split(&content, b"\n") { if !line.is_empty() { self.entries.push(...) } }, so the next session gets N single-line entries instead of the one block the PR records in-session.

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.

1 participant