Skip to content

fix(shortcuts): keep key shortcuts working in Vite HMR sessions - #6214

Merged
edusperoni merged 3 commits into
mainfrom
fix/vite-dev-shortcuts
Nov 24, 2026
Merged

edusperoni merged 3 commits into
mainfrom
fix/vite-dev-shortcuts

Conversation

@edusperoni

@edusperoni edusperoni commented Nov 24, 2026 •

Copy link
Copy Markdown
Collaborator

PR Checklist

What is the current behavior?

With the interactive key shortcuts on (NS_COMMAND_SHORTCUTS=1, the default from 9.2) in a Vite HMR session:

  • The Vite dev server shares the terminal's stdin. vite serve is spawned with stdio: "inherit", so it reads the same TTY as the CLI's raw-mode shortcut handler. Each keypress goes to whichever process reads first: r can land in Vite's own readline shortcuts, which wait for Enter, instead of restarting the app. Vite also prints its own "press h + enter to show help" hint, which is misleading here. (While the CLI's shortcuts are off, which is today's default, Vite's own shortcuts are the only ones and work as intended.)
  • After R (or pausing with w), every shortcut stops working. A Node child whose stdout/stderr is the terminal puts back, on exit, the terminal mode it saw at startup. The dev server starts before the shortcuts attach, so it saw cooked mode. When R or w stops it, the terminal drops out of raw mode. From then on every key is echoed by the terminal and only reaches the CLI after Enter, so it looks as if the shortcut table were empty.

What is the new behavior?

  • vite serve gets the terminal's stdin only when the CLI won't read the keys itself. The new commandShortcutsOwnTerminal() (lib/services/key-shortcuts.ts) decides this at spawn time, because the dev server starts before the shortcuts attach. It uses the same conditions as KeyShortcutService.attach for a TTY session: NS_COMMAND_SHORTCUTS is on, stdin is a TTY that supports raw mode, and keyShortcutsEnabled() is true.
    • CLI shortcuts on: stdin is "ignore". The CLI owns the keys, and Vite's own h/r/u/o/c/q + Enter shortcuts and its hint are not available. Vite's output still streams to the terminal.
    • CLI shortcuts off (today's default), or no terminal: stdin is inherited exactly as before, so Vite's shortcuts keep working. This also covers an ns run spawned by ns start, which gets its keys over IPC.
  • KeyShortcutService re-enters raw mode after every shortcut action, as long as it is still attached to a TTY. It clears raw mode first (the same setRawMode(false) / setRawMode(true) pair attach uses), because Node skips setRawMode(true) while it believes the terminal is already raw.

Verified on an iOS simulator with a Vue app (Vite HMR):

  • R, w (pause/resume) and B all leave the terminal raw, and ? works afterwards.
  • HMR works after R and after B.
  • After Ctrl+C the shell is left in cooked mode.

New unit tests:

  • Dev-server stdin is "ignore" with CLI shortcuts on a TTY. It is inherited with shortcuts off, and inherited with no TTY.
  • commandShortcutsOwnTerminal() holds with command shortcuts on a TTY. It doesn't hold with command shortcuts off, with NS_KEY_SHORTCUTS=false, or with a non-TTY stdin even when forced on.
  • Raw mode is re-entered after an action that something else knocked out of raw mode.
  • Raw mode is not re-entered after an action that detached.

Note for the stacked Vite PRs: #6208 edits the same stdio line in startViteDevServer (it adds an "ipc" entry), so whichever of the two lands second needs a trivial rebase. stdin keeps the commandShortcutsOwnTerminal() gate.

Summary by CodeRabbit

  • Bug Fixes
    • Prevented the development server from taking over terminal input when command shortcuts are enabled in a compatible terminal, while preserving its existing output.
    • Improved keyboard shortcut handling so terminal input returns to the expected mode after an action. If the shortcut service is detached during the action, the terminal’s existing mode is preserved.

The HMR dev server (vite serve) inherited the CLI's stdin, so it read the same TTY as the raw-mode shortcut handler: each keypress went to whichever process read first, and Vite printed its own "press h + enter" hint. Give it no stdin; its output stays inherited.
A Node child whose stdout/stderr is the terminal restores, on exit, the terminal mode it saw at startup. The Vite dev server starts before the shortcuts attach, so when R (or w) stops it the terminal drops back to cooked mode: every later key is echoed by the terminal and only reaches the CLI after Enter, which looks like an empty shortcut table. Re-apply raw mode once an action finishes, clearing it first since Node ignores setRawMode(true) while it believes the terminal is already raw.
@coderabbitai

coderabbitai Bot commented Nov 24, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

The shortcut service now checks when command shortcuts own terminal input and manages raw mode during shortcut actions. The Vite dev server ignores stdin when the CLI owns terminal shortcuts and inherits it otherwise. Tests cover the ownership conditions and raw-mode behavior.

Changes: Terminal input handling

Layer / File(s) Summary
Shortcut ownership and raw-mode lifecycle
lib/services/key-shortcuts.ts, test/services/key-shortcuts.ts
The service checks whether command shortcuts own a raw-mode TTY. It clears and enables raw mode during attachment, then restores raw mode after dispatch if the service remains attached and stdin is a TTY. Tests cover ownership conditions, raw-mode restoration, and detachment.
Vite stdin configuration
lib/services/bundler/bundler-compiler-service.ts, test/services/bundler/bundler-compiler-service.ts
The Vite dev-server spawn options ignore stdin when command shortcuts own the terminal and inherit it otherwise. Tests cover enabled and disabled shortcuts, and TTY and non-TTY stdin.

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Bug fix

Suggested reviewers: nathanwalker

Merge Risk: Minimal · up to 4cf0d

This change keeps CLI key shortcuts working during Vite HMR sessions by giving Vite no stdin when the CLI owns the terminal. No concrete merge-blocking risk was found, and the change is low-risk to merge.

Pre-merge checks: Passed 4

Check name Status Explanation
Title check Passed The title names the main change: key shortcuts in the CLI should keep working during Vite HMR sessions. The fix matches the changeset. Vite's dev-server child now gets stdin ignored only when CLI comm…
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 Check skipped - CodeRabbit’s high-level summary is enabled.
Full details: Title check

Explanation

The title names the main change: key shortcuts in the CLI should keep working during Vite HMR sessions. The fix matches the changeset. Vite's dev-server child now gets stdin ignored only when CLI command shortcuts own the terminal, and raw mode is re-entered after shortcut actions. The 'fix(shortcuts)' prefix correctly describes the change as a bug fix to the shortcut service.

  • 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

A rabbit checks the keys at night,
And guards the terminal just right.
Raw mode rests, then wakes once more,
While Vite leaves stdin at the door.
The tests hop through each setting’s flight.

Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Repository UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 33bd3138-a974-429a-bf22-081ec11e258c
Commits

Reviewing files that changed from the base of the PR and between c2d9915 and 4cf0d21.

Files selected for processing: 4

  • lib/services/bundler/bundler-compiler-service.ts
  • lib/services/key-shortcuts.ts
  • test/services/bundler/bundler-compiler-service.ts
  • test/services/key-shortcuts.ts

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

@edusperoni
edusperoni marked this pull request as ready for review November 24, 2026 19:07
…rtcuts take it

Vite binds its own h/r/u/o/c/q shortcuts only when its stdin is a terminal, so keeping vite serve off stdin unconditionally removed them from the default session, where the CLI's shortcuts are off and nothing else reads the keys. Hand it no stdin only when the CLI will read the terminal itself: command shortcuts on, a raw-mode capable TTY, and key shortcuts not switched off. The same conditions KeyShortcutService.attach applies now live in commandShortcutsOwnTerminal(), decided at spawn time since the dev server starts before the attach.
@edusperoni
edusperoni merged commit c77ff7d into main Nov 24, 2026
15 checks passed
@edusperoni
edusperoni deleted the fix/vite-dev-shortcuts branch November 24, 2026 19:22
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.

1 participant