Skip to content

repl: evaluate a last line that has no newline at the end of piped input - #46126

Open
robobun wants to merge 4 commits into
mainfrom
robobun/164103bc/repl-end-of-input
Open

robobun wants to merge 4 commits into
mainfrom
robobun/164103bc/repl-end-of-input

Conversation

@robobun

@robobun robobun commented Oct 20, 2026 •

Copy link
Copy Markdown
Collaborator

Problem

  • bun repl drops the last line of piped input when it has no newline. echo -n 'console.log(1)' | bun repl prints nothing and exits 0.
  • Cause: at the end of stdin, the key loop in run_with_vm (src/runtime/cli/repl.rs:2346) leaves. The line in line_editor.buffer is lost.

Fix

Background

  • The REPL handles stdin as keys in one loop. read_byte returns None at the end of stdin and after a failed read. stdin_at_eof tells them apart.
  • On a terminal, a read of no bytes can be a hang-up or a read timeout. The line does not run there.
  • Considered: a direct handle_enter call at the end of input. The second call site stops its inlining: +12 instructions per Enter.

Downsides

  • A writer that dies mid-line looks like a clean end. Its cut line runs if it parses, as in node -i.
  • A last line that leaves a bracket open writes one more newline, and still no error.
  • Not covered: .editor input with no Ctrl+D at its end, and a terminal in line mode (bun repl | cat).
Notes

Origin. A fuzz run on the REPL's stdin found it. There is no GitHub issue. The end-of-input arm is unchanged since the first native REPL (#26304, Bun 1.3.10).

Repro. Each input has no final newline. main is a release build of c1df64b.

Input main this PR bun --interactive node 26.3
1+1 nothing 2 2 2
console.log("one"), console.log("two") one one, two one, two one, two
if (true) {, console.log("two"), } nothing two two two
foo( nothing nothing nothing nothing

The rule. An input with no final newline behaves as the same input with one. That covers a dot-command as the last line, a line of spaces (prints undefined, as Enter on it does), and a line that does not parse (prints its SyntaxError). Nothing is evaluated twice: after the synthesized Enter the line editor is empty, and the next read returns the end of input again.

What does not change. Every input that ends with a newline. The 28 inputs of the existing piped tests give the same stdout, stderr, exit code and history file on both builds.

Lines that nobody submitted (release build of this PR)

  • A read that fails (EAGAIN on a non-blocking stdin) with a half line read: the line ran in 0 of 20 runs.
  • A pty that goes away while SIGHUP is ignored, with a half-typed line: 0 of 20 runs, with stdout on the pty and with stdout in a file.
  • A read timeout on a non-canonical terminal (VMIN=0, bun repl | cat): 0 of 5 runs.

Measurements. Release builds of c1df64b and of this PR, one toolchain, Linux x64. Instructions are instructions:u from perf_event_open, as the slope between N and 2N keys, minimum of 3 runs, spread 0.03%.

  • Per key (Ctrl+A, 1M and 2M keys): 66.04 to 49.04. At one byte per read: 74.96 to 58.04. Enter on an empty line: 365.05 to 347.13. Enter in .editor mode: 468.23 to 434.45. An evaluated 1+1: 64.6k and 64.4k, inside the run-to-run spread of 0.5k. The loop head compiles to fewer instructions with the match: Repl::run_with_vm goes from 3,443 to 3,250 instructions.
  • read_byte: 40 to 42 instructions. The two new ones are the stores on the two arms that return None, after the syscall.
  • Allocator calls (gdb counters on the mi_* entry points, 200 lines): equal for an empty Enter, an .editor line and an evaluated line.
  • Syscalls (reads, writes, stdout bytes): 1+1\n is 26, 18, 110 on both. 1+1 goes from 26, 14, 105 to 27, 18, 110. The extra read is the second read of the end of input. A 1000-line script that ends with a newline: 84, 3015, 6995 on both.
  • Size: the repl module has 35 symbols in both builds, 38,982 and 38,411 bytes. .text of the whole binary: -512 bytes.

Relation to open PRs

For a maintainer to decide

  • .editor input that ends with no Ctrl+D on a pipe is still dropped. Node has no .editor without a terminal.
  • A terminal in line mode (stdin on a terminal, stdout redirected): a half line, then Ctrl+D twice, is still dropped. A first draft handled it with a poll for a hang-up. A read timeout on a non-canonical terminal then ran a half-typed line, so that arm is not here. The end of input on a terminal is also not sticky: after a synthesized Enter the next read waits for more keys.
  • A read of stdin that fails still ends the session with no message and exit code 0, as before. This PR only keeps such a read from submitting the pending line.
  • Non-terminal stdin is still decoded as keys (a Tab byte completes, 0x7f deletes). Node reads lines there.

Tests. The 8 tests that fail without the fix: five last lines with no newline, the transcript of a last line that leaves a call open, the clean-end half of the failed-read test, and the history file. The 4 that pass on both builds pin what must stay: empty stdin, no newline after .exit, a terminal that goes away, and Ctrl+D twice on a terminal in line mode. The line-mode test pins the terminal check on Linux: it fails on a build with the check removed. A hang-up on Linux fails the read, so the hang-up test reaches the check only where a hang-up reads as 0 bytes.

Platforms. Nothing was run on macOS or Windows locally. The failed-read test is skipped on Windows: fs.closeSync(0) does nothing there (libuv keeps fds 0 to 2 open), so no read fails. The other piped tests are not gated. The pty tests are in the existing describe.todoIf(isWindows) block.

At the end of a pipe or a file, a line that is still in the line editor
is submitted the way Enter submits it. A failed read and a terminal on
stdin submit nothing.
@robobun

robobun commented Oct 20, 2026 •

Copy link
Copy Markdown
Collaborator Author
Updated 1:56 PM PT - Oct 21st, 2026

✅ @robobun, your commit 8692289d3f3efd2a38207d80ea337ac31691998e passed in Build #130182! 🎉


🧪   To try this PR locally:

bunx bun-pr 46126

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

bun-46126 --bun

@robobun

robobun commented Oct 20, 2026

Copy link
Copy Markdown
Collaborator Author

Status

Reproduced on Linux x64 with the released build (1.4.3) and with a release build of main (c1df64b):

  • echo -n 'console.log(1)' | bun repl prints nothing and exits 0. With this change it prints 1.
  • printf 'console.log("one")\nconsole.log("two")' | bun repl prints only one. With this change it prints one and two.
  • printf 'if (true) {\nconsole.log("two")\n}' | bun repl prints nothing. With this change it prints two.

node -i and bun --interactive evaluate the last line in all three. The fix and its tests are in this PR (#46126).

@coderabbitai

coderabbitai Bot commented Oct 20, 2026 •

Copy link
Copy Markdown
Contributor

Review in Change Stack →

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration
  • Configuration used: Repository: oven-sh/bun/.coderabbit.yaml
  • Review profile: ASSERTIVE
  • Plan: Advanced
  • Run ID: 538a941f-7ec6-42bd-b6a9-795b7abfffb0
📥 Commits

Reviewing files that changed from the base of the PR and between 0ba1e86 and 8692289.

📒 Files selected for processing: 2
  • src/runtime/cli/repl.rs
  • test/js/bun/repl/repl.test.ts

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 0 remain after this review.

Walkthrough

The REPL now distinguishes confirmed stdin EOF from read errors. For non-TTY input, it processes a non-empty final line when stdin reaches EOF. Tests cover EOF handling and terminal hang-up.

Changes

REPL stdin EOF handling

Layer / File(s) Summary
Detect and handle stdin EOF
src/runtime/cli/repl.rs, test/js/bun/repl/repl.test.ts
The REPL marks zero-byte reads as confirmed EOF and submits a non-empty final line for non-TTY input. Tests cover empty and incomplete input, .exit, read errors, history persistence, and terminal hang-up behavior.

Suggested reviewers: cirospaciari

Priority: ➖ Normal

Merge Risk: ⚪ Minimal · up to 86922

No merge-blocking issue is established; the EOF change is ready for normal merge checks.

Pre-merge checks: Passed 4
Check name Status Explanation
Title check Passed The title clearly and concisely describes the main change: evaluating the final unterminated line from piped input.
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Description check Passed The description clearly explains the problem, implementation, behavior, limitations, and verification results. It does not use the template headings "What does this PR do?" and "How did you verify you…
Full details: Description check

Explanation

The description clearly explains the problem, implementation, behavior, limitations, and verification results. It does not use the template headings "What does this PR do?" and "How did you verify your code works?", but it provides the required information under equivalent sections.

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

Comment thread test/js/bun/repl/repl.test.ts Resolved
Comment thread src/runtime/cli/repl.rs Resolved
@robobun

robobun commented Oct 20, 2026

Copy link
Copy Markdown
Collaborator Author

Overlap note: #46248 changes the same loop. It reads a stdin that is not a raw terminal as lines of text, and it includes the end-of-input rule of this PR and its 11 tests. The failed-read test is skipped on Windows there, because the read after closeSync(0) does not fail on Windows (the two red Windows lanes of this PR). If #46248 merges, this PR is covered. One difference: on a terminal in line mode, a half line and then Ctrl+D twice runs the line in #46248, as in node -i | cat.

fs.closeSync(0) does nothing on Windows, so the read of stdin does not
fail there and the last line runs.
Comment thread src/runtime/cli/repl.rs Resolved

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

Beyond the inline finding, two things I checked on this push and ruled out: the synthesized Enter cannot spin at EOF, since every path through handle_enter (editor mode, dot-command, incomplete code, evaluation) either clears line_editor or sets running = false, so the next None from read_key takes the leave-and-break arm; and stdin_at_eof cannot be stale, because read_key returns None only via read_byte, which writes the flag on both of its None arms.

Extended reasoning...

The second push only adds a Windows skip to the failed-read test; the Rust change in src/runtime/cli/repl.rs is unchanged from the previously reviewed version. No security-sensitive surface is touched. The note records two concrete concerns examined from the diff and ruled out, in addition to the inline test-coverage finding.

Still open from earlier reviews (1):

  • Unresolved: 1 minor or pre-existing.

Comment thread test/js/bun/repl/repl.test.ts Resolved

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

LGTM. The two points from my earlier reviews are addressed by the latest commits: the failed-read test is now gated on Windows with the no-op closeSync(0) named, and the new repl | cat line-mode test gives Linux coverage for the !Output::is_stdin_tty() clause (raw mode is only set when both stdin and stdout are ttys, so Ctrl+D there is a real 0-byte read). The one inline note is pre-existing .editor behavior the description already lists as out of scope.

Extended reasoning...

The diff adds a stdin_at_eof flag set by read_byte and a loop arm in run_with_vm that synthesizes Key::Enter for a non-empty line at a clean, non-tty EOF, plus eleven tests in repl.test.ts. It touches no security-sensitive surface. Every handle_enter path either clears the line editor or sets running = false, so the synthesized Enter cannot loop, and bun_sys maps Windows BROKEN_PIPE/HANDLE_EOF to Ok(0) so the ungated piped tests hold there too. The change is about twenty lines of Rust with clear intent, the path has no CODEOWNER, and the only remaining finding is a pre-existing gap the author already documents.

Still open from earlier reviews (1):

  • Unresolved: 1 minor or pre-existing.

Comment thread src/runtime/cli/repl.rs Resolved

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