Skip to content

Fix setRawMode on Windows for non-stdin TTYs - #32153

Open
edemaine wants to merge 1 commit into
oven-sh:mainfrom
edemaine:tty-raw-win
Open

Fix setRawMode on Windows for non-stdin TTYs#32153
edemaine wants to merge 1 commit into
oven-sh:mainfrom
edemaine:tty-raw-win

Conversation

@edemaine

Copy link
Copy Markdown

What does this PR do?

Fixes Windows tty.ReadStream#setRawMode() for reopened console input handles such as \\\\.\\CONIN$.

When stdin is a pipe, tools such as hunk should be able to reopen CONIN$ to recover interactive keyboard input. On Bun, creating new tty.ReadStream(fd) for that reopened console fd reported isTTY === true, but setRawMode(true) failed because the stream did not have a native TTY wrapper. This PR creates a Windows TTYWrap for non-stdin TTY read streams and routes raw-mode changes through it, while preserving Bun’s existing stdin path.

It also adds a Windows regression test covering fs.openSync("\\\\.\\CONIN$", "r+"), new tty.ReadStream(fd), and toggling raw mode on/off. The analogous test works in Node; see nodejs/node#63856

How did you verify your code works?

  • Built Bun locally on Windows:

    .\build\debug\bun-debug.exe --version
  • Ran the focused tty test:

    .\build\debug\bun-debug.exe test test/js/node/tty.test.ts
  • Verified the original pipe/interactive scenario using Hunk:

    git diff | C:\Users\edemaine\Projects\bun\build\debug\bun-debug.exe run ..\hunk\src\main.tsx -- patch -
  • Checked patch whitespace:

    git diff --check

I had issues running the full test suite, but I think those errors were unrelated.

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

Claude Code Review

This pull request is from a fork — automated review is disabled. A repository maintainer can comment @claude review to run a one-time review.

@coderabbitai

coderabbitai Bot commented Jun 12, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: ASSERTIVE

Plan: Pro

Run ID: 2905fb48-8063-4c41-a64a-265dd6512236

📥 Commits

Reviewing files that changed from the base of the PR and between bcad9b2 and feacc0f.

📒 Files selected for processing (2)
  • src/js/node/tty.ts
  • test/js/node/tty.test.ts

Walkthrough

Windows TTY support for reopened CONIN$ streams is enhanced. The module conditionally loads the native TTY binding on Windows, ReadStream constructor allocates a native wrapper for non-stdin TTY fds, setRawMode coordinates with the binding using numeric mode values, and a test verifies raw mode state transitions on reopened CONIN$ streams.

Changes

Windows TTY raw mode support

Layer / File(s) Summary
Windows TTY binding initialization
src/js/node/tty.ts
The module retrieves TTY from process.binding("tty_wrap") on Windows (undefined otherwise), establishing a conditional native binding for subsequent operations.
Windows ReadStream TTY wrapper allocation
src/js/node/tty.ts
ReadStream constructor now allocates and stores a native TTY wrapper in this.$bunNativePtr for Windows instances with non-stdin fds that are actual TTYs, enabling raw mode support.
Windows setRawMode native coordination
src/js/node/tty.ts
setRawMode ensures the native-readable stream is constructed via optional chaining and passes numeric uv_tty_mode_t values to the TTYWrap binding when the handle is a TTY, otherwise forwards the boolean flag.
Test for raw mode on reopened CONIN$ streams
test/js/node/tty.test.ts
New Windows-only test spawns a child process that opens \\.\CONIN$, wraps it in ReadStream, and validates that setRawMode correctly transitions isRaw state from false to true to false.
🚥 Pre-merge checks | ✅ 4
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately and concisely describes the main change: fixing setRawMode on Windows for non-stdin TTY streams.
Description check ✅ Passed The description includes both required sections with sufficient detail: explains what the PR does (fixes Windows setRawMode for reopened console handles) and how it was verified (local build testing, regression test, and integration testing with hunk).
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.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.


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 and usage tips.

@robobun

robobun commented Jul 31, 2026

Copy link
Copy Markdown
Collaborator

Cross-referencing #34788, which addresses the same setRawMode failed because it was called on something that is not a TTY failure for a tty.ReadStream on a reopened CONIN$ handle. That PR routes every Windows console-input fd (stdin and reopened CONIN$) through a single uv_tty_set_mode path so both end up with UV_TTY_MODE_RAW_VT, and is green on Windows CI. Linking so the two can be considered together and this contribution credited.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants