Repository navigation
Conversation
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.
|
Warning Review limit reachedYou'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. View limit details
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. Comment |
|
Updated 2:05 AM PT - Oct 22nd, 2026
❌ @robobun, your commit 6715a57 has some failures in 🧪 To try this PR locally: bunx bun-pr 46449That installs a local version of the PR into your bun-46449 --bun |
|
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 replMain prints 2, 42, 2: Up after the editor evaluation recalls |
| // 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"))?; |
There was a problem hiding this comment.
🟡 (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.
Problem
bun replnever adds code evaluated from.editormode to the history. After.editor,let a = 7, Ctrl+D,a * 6,.save f.jswrites onlya * 6..load f.jsthen fails withReferenceError: a is not defined.src/runtime/cli/repl.rs:2361) evaluateseditor_bufferand never callsHistory::add. No issue reports this. repl: clear the typed line on Ctrl+C in every mode, evaluate it on Ctrl+D in editor mode #46083 lists it under "Not changed here".Fix
self.history.add(strings::trim(&code, b"\n"))beforeevaluate_and_print. No other source line changes.handle_enterstores a...continuation.test/js/bun/repl/repl.test.ts, 5 new tests, 4 fail on main. The whole file passes.Background
editor_bufferuntil Ctrl+D, then evaluates them as one input. Up,.history,.saveand the history file readHistory.evaluate_and_print:.loadfiles run through it.Downsides
.editor:History::addis no longer inlined, so an accepted Enter runs 16 more instructions (993 to 1009). Release text grows 256 bytes..is read as a command (repl: decide command or code in one place, and take only full command names in a multi-line input #46364).// don'topens a continuation (REPL doesn't know how to handle regex literals #37326).Notes
Origin. No user reported this and no issue exists. The body of #46083 lists it under "Not changed here": "Code evaluated from
.editoris not added to the history." It hands the item to a follow-up change, and this PR is that change. The arm has had nohistory.addsince the first native REPL (#26304,src/repl.zig:1576against:1762). No test or comment makes the omission a decision.docs/runtime/repl.mdxsends longer multi-line input to.editor(line 105). It documents.saveas 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 repl1 + 1,.editor,7 * 6, Ctrl+D, Up, Enter.editor,let a = 7,a * 6, Ctrl+D,.historylet a = 7anda * 6.editor,let a = 7, Ctrl+D,a * 6,.save f.js, then.load f.jsin a new sessionReferenceError: a is not defined.editor,7 * 6, Ctrl+D,.exit, then read~/.bun_repl_history7 * 6Entry shape. One entry per editor run, with the outer newlines trimmed and the inner ones kept. It is the call
handle_entermakes 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 recallsa * 6and the next Up recallslet a = 7. After.editor,99 + 1, Ctrl+C, Up recalls99 + 1. That shape does not fit here:bun replhas one list for recall and for.save.History::addskips a blank line and a line equal to the entry before it. So per-line entries make.savelose blank lines and the second of two equal lines. Node keeps a second list for.save.The tests
the evaluated input is one entryandup arrow recalls the whole evaluated inputpin the shape.Tests (
test/js/bun/repl/repl.test.ts, twohistory of .editor modeblocks)the evaluated input is one entry:.historynumbers it once and.savewrites 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.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.Measurements. Release builds of main 9634520 and of that commit with this change, in one build directory with one toolchain, Linux x64. Only
repl.rsdiffers.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::addhas two callers now and is no longer inlined intoRepl::run_with_vm. It is a 459 B function.run_with_vmgoes from 14,350 to 14,043 B. Thereplmodule has 33 function symbols and 37,912 B on main, 34 and 38,064 B here.replmodule, from aqemu-x86_64exec 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).replmodule: an accepted prompt Enter makes 4mallocand 6freein both builds. An editor submit makes 4malloc(main: 3, the new one is the entry) and 6freein both.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 oneopenat,fchmod,writeandcloseat exit.perf,valgrind,straceandbloatyare 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_enterapply. Before this change no.editorcode reached them through the history. Each row ran on both release builds..editor,// don't,7 * 6, Ctrl+D prints 42. Up, Enter opens a...continuation, becauseis_incomplete_codecounts the quote in the comment (REPL doesn't know how to handle regex literals #37326, repl: recognize regex literals and comments in continuation detection #37327). The same text typed at the prompt does this on main..editor,.5 + .5, Ctrl+D prints 1. Up, Enter printsUnknown command: .5(repl: decide command or code in one place, and take only full command names in a multi-line input #46364). Typed at the prompt, main prints the same..editor,{ a: 1 } // c, Ctrl+D prints the object. Up, Enter prints1(replMode: decide between an object literal and a block in the parser #43770). Typed at the prompt, main prints the same..editor, Up, Enter, Ctrl+D runs a recalled block again as the editor ran it..then runs as a command when the user recalls it and presses Enter. For[1]and.map(x => x)the second row printsUnknown command: .map(x.Not changed here
reset_position()line. Without their other changes it lost the draft in.editor, text, Up, Ctrl+D, Down. With the text100 + 1and a last Enter, main prints 101 and that draft printed nothing. After a run that is not empty,History::addalready leaves the cursor at the newest entry..is not an entry. That includes.copy <expr>, which evaluates typed code. The content of a.loadfile is not an entry either..savewrites every entry, so a recorded.save xline would land in its own saved file..savealso writes the entries of earlier sessions..helpand the docs call it a save of the history.[1,and2]draws> [1,and2]. Each later key leaves one more> [1,row.Relation to open PRs
git merge-file --diff3against 16 open PR heads that editrepl.rs: repl: pump event loop while waiting for stdin so timers and IPC fire #30560, repl: interrupt a running evaluation with Ctrl+C #33411, repl: recognize regex literals and comments in continuation detection #37327, repl: drop Key variants that were decoded but never handled #37413, repl: keep multi-line history entries on one line of the history file #38023, repl: ignore non-regular history files, and never replace a history file that was not read #38217, repl: bind page up and page down to first and last history entry #38748, replMode: decide between an object literal and a block in the parser #43770, repl: evaluate input the REPL transform does not take through each caller's result path (-e/-pexit code,.copy,_) #45267, repl: clear the typed line on Ctrl+C in every mode, evaluate it on Ctrl+D in editor mode #46083, repl: keep the line on Down with no newer entry, reset the history cursor at every line start #46084, repl: redraw a recalled multi-line history entry in place #46095, repl: append each history entry to the history file when it is accepted #46109, repl: evaluate a last line that has no newline at the end of piped input #46126, repl: read non-terminal stdin as lines of text and take bracketed pastes as text #46248, repl: decide command or code in one place, and take only full command names in a multi-line input #46364. More open PRs touch these files, and I did not check all of them..editor,let a = 7Enter,a * 6, Ctrl+D prints 42..historythen lists one entry with both lines. The PR that lands second can add that test.fn finish_editor, which returns(). The add has to move there by hand, withbun_core::handle_oomor aResultreturn. By a read of that code, the three piped tests fail if the port is dropped.handle_enterand this arm is not in this PR. Three open PRs (repl: clear the typed line on Ctrl+C in every mode, evaluate it on Ctrl+D in editor mode #46083, repl: keep the line on Down with no newer entry, reset the history cursor at every line start #46084, repl: read non-terminal stdin as lines of text and take bracketed pastes as text #46248) edit the arm.Self-review, not taken
.saveand.historytests pin.test.todofor the recalled-entry rows above. repl: recognize regex literals and comments in continuation detection #37327 and repl: decide command or code in one place, and take only full command names in a multi-line input #46364 carry the tests for those checks.Platform. No
cfgarm 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 outsideskipIf(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 --checkonrepl.rsandprettier --checkon 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