Repository navigation
fix(shortcuts): keep key shortcuts working in Vite HMR sessions - #6214
Conversation
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.
|
No actionable comments were generated in the recent review. Walkthrough
Merge Risk:
|
…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.
PR Checklist
I have read the CLA and I hereby sign the CLA).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:vite serveis spawned withstdio: "inherit", so it reads the same TTY as the CLI's raw-mode shortcut handler. Each keypress goes to whichever process reads first:rcan 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.)R(or pausing withw), 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. WhenRorwstops 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 servegets the terminal's stdin only when the CLI won't read the keys itself. The newcommandShortcutsOwnTerminal()(lib/services/key-shortcuts.ts) decides this at spawn time, because the dev server starts before the shortcuts attach. It uses the same conditions asKeyShortcutService.attachfor a TTY session:NS_COMMAND_SHORTCUTSis on, stdin is a TTY that supports raw mode, andkeyShortcutsEnabled()is true."ignore". The CLI owns the keys, and Vite's ownh/r/u/o/c/q+ Enter shortcuts and its hint are not available. Vite's output still streams to the terminal.ns runspawned byns start, which gets its keys over IPC.KeyShortcutServicere-enters raw mode after every shortcut action, as long as it is still attached to a TTY. It clears raw mode first (the samesetRawMode(false)/setRawMode(true)pairattachuses), because Node skipssetRawMode(true)while it believes the terminal is already raw.Verified on an iOS simulator with a Vue app (Vite HMR):
R,w(pause/resume) andBall leave the terminal raw, and?works afterwards.Rand afterB.New unit tests:
"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, withNS_KEY_SHORTCUTS=false, or with a non-TTY stdin even when forced on.Note for the stacked Vite PRs: #6208 edits the same
stdioline instartViteDevServer(it adds an"ipc"entry), so whichever of the two lands second needs a trivial rebase. stdin keeps thecommandShortcutsOwnTerminal()gate.Summary by CodeRabbit