From 095d91ee9081e0316c9f6428555f57b8626e693e Mon Sep 17 00:00:00 2001 From: Eduardo Speroni Date: Tue, 24 Nov 2026 15:58:02 -0300 Subject: [PATCH 1/3] fix(shortcuts): keep the terminal's keys away from the Vite dev server 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. --- .../bundler/bundler-compiler-service.ts | 8 +++-- .../bundler/bundler-compiler-service.ts | 34 +++++++++++++++++++ 2 files changed, 39 insertions(+), 3 deletions(-) diff --git a/lib/services/bundler/bundler-compiler-service.ts b/lib/services/bundler/bundler-compiler-service.ts index 96847f3c25..4dcadad09a 100644 --- a/lib/services/bundler/bundler-compiler-service.ts +++ b/lib/services/bundler/bundler-compiler-service.ts @@ -736,10 +736,12 @@ export class BundlerCompilerService const options: { [key: string]: any } = { cwd: projectData.projectDir, - // Inherit so the dev server's URLs/logs stream to the user as - // before. No IPC needed here — the build watcher provides the + // Output is inherited so the dev server's URLs/logs stream to the + // user. No IPC needed here — the build watcher provides the // bundle-complete IPC; the dev server is fetched over HTTP/ws. - stdio: "inherit", + // stdin must not be the terminal: Vite reads it for shortcuts of + // its own and would take the keys meant for the CLI's. + stdio: ["ignore", "inherit", "inherit"], env: { ...process.env, NATIVESCRIPT_BUNDLER_ENV: JSON.stringify(envData), diff --git a/test/services/bundler/bundler-compiler-service.ts b/test/services/bundler/bundler-compiler-service.ts index 0e3b1dfce9..cdf301d7bd 100644 --- a/test/services/bundler/bundler-compiler-service.ts +++ b/test/services/bundler/bundler-compiler-service.ts @@ -373,6 +373,40 @@ describe("BundlerCompilerService", () => { }); }); + describe("startViteDevServer", () => { + it("does not hand the dev server the terminal's stdin", async () => { + const spawned: { [key: string]: any }[] = []; + testInjector.resolve("childProcess").spawn = ( + _command: string, + _args: string[], + options: { [key: string]: any }, + ) => { + spawned.push(options); + return fakeChildProcess(321); + }; + const service = bundlerCompilerService; + service.getBundler = () => "vite"; + service.buildEnvData = () => ({}); + service.buildEnvCommandLineParams = async (): Promise => []; + service.getBundlerExecutablePath = () => "/project/vite.js"; + service.waitForPort = async () => true; + + await service.startViteDevServer( + { platformNameLowerCase: "ios" }, + { + projectDir: "/project", + bundlerConfigPath: "/project/vite.config.ts", + }, + { watch: true, hmr: true }, + ); + + assert.lengthOf(spawned, 1); + const { stdio } = spawned[0]; + assert.isArray(stdio); + assert.equal(stdio[0], "ignore"); + }); + }); + describe("compileWithWatch", () => { it("fails when the value set for bundlerConfigPath is not existant file", async () => { const bundlerConfigPath = "some path.js"; From c2d9915760c250b475719b7905b73bd45ef569b8 Mon Sep 17 00:00:00 2001 From: Eduardo Speroni Date: Tue, 24 Nov 2026 15:58:08 -0300 Subject: [PATCH 2/3] fix(shortcuts): re-enter raw mode after each shortcut action 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. --- lib/services/key-shortcuts.ts | 19 +++++++++++++-- test/services/key-shortcuts.ts | 43 ++++++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+), 2 deletions(-) diff --git a/lib/services/key-shortcuts.ts b/lib/services/key-shortcuts.ts index fb756331e6..36b21d8e99 100644 --- a/lib/services/key-shortcuts.ts +++ b/lib/services/key-shortcuts.ts @@ -98,6 +98,16 @@ export function findShortcut( return shortcut; } +/** + * Node skips `setRawMode(true)` while it believes the terminal is already + * raw, so the mode is cleared first to make the call reach the terminal even + * when something else has changed it since. + */ +function enterRawMode(stdin: NodeJS.ReadStream): void { + stdin.setRawMode(false); + stdin.setRawMode(true); +} + /** * Raw mode outlives the process that set it, so a CI runner or a redirected * stdin must never get it; `NS_KEY_SHORTCUTS=false` is the manual opt-out. @@ -428,8 +438,7 @@ export class KeyShortcutService implements IKeyShortcutService { return false; } - stdin.setRawMode(false); - stdin.setRawMode(true); + enterRawMode(stdin); stdin.resume(); stdin.on("data", this.onData); process.once("exit", this.onExit); @@ -592,6 +601,12 @@ export class KeyShortcutService implements IKeyShortcutService { } finally { this.running = false; if (process.stdin.setRawMode) { + // A Node child writing to the terminal, such as the Vite dev server + // an action stops, restores the terminal mode it started with when + // it exits; that can be cooked mode, where keys wait for Enter. + if (this.attached && process.stdin.isTTY) { + enterRawMode(process.stdin); + } process.stdin.resume(); } } diff --git a/test/services/key-shortcuts.ts b/test/services/key-shortcuts.ts index a87a462dff..e51354b35a 100644 --- a/test/services/key-shortcuts.ts +++ b/test/services/key-shortcuts.ts @@ -19,10 +19,16 @@ import { class FakeStdin extends EventEmitter { public isTTY: boolean = true; public rawMode: boolean = null; + /** The mode the terminal is really in, which another process can change. */ + public terminalRaw: boolean = false; public resumeCount: number = 0; public pauseCount: number = 0; public setRawMode(value: boolean): any { + // Like Node, only a change from the mode it last set reaches the terminal. + if (value !== this.rawMode) { + this.terminalRaw = value; + } this.rawMode = value; return this; } @@ -697,6 +703,43 @@ describe("key shortcuts", () => { assert.equal(stdin.pauseCount, 1); }); + it("takes the terminal back into raw mode after an action", async () => { + service.attach({ + shortcuts: [ + { + key: "R", + description: "Restart", + action: () => { + // A child that shared the terminal exits and restores the + // cooked mode it started with. + stdin.terminalRaw = false; + }, + }, + ], + }); + assert.isTrue(stdin.terminalRaw); + + await press("R"); + + assert.isTrue(stdin.terminalRaw); + }); + + it("leaves the terminal alone after an action that detached", async () => { + service.attach({ + shortcuts: [ + { + key: "x", + description: "Detach", + action: () => service.detach(), + }, + ], + }); + + await press("x"); + + assert.isFalse(stdin.terminalRaw); + }); + it("tears down when the process exits", () => { const before = new Set(process.listeners("exit")); service.attach({ shortcuts: [] }); From 4cf0d212a7dcc313434a0744da8202ab81c837a4 Mon Sep 17 00:00:00 2001 From: Eduardo Speroni Date: Tue, 24 Nov 2026 16:14:25 -0300 Subject: [PATCH 3/3] fix(shortcuts): give the dev server the terminal unless the CLI's shortcuts 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. --- .../bundler/bundler-compiler-service.ts | 12 ++- lib/services/key-shortcuts.ts | 20 ++++- .../bundler/bundler-compiler-service.ts | 80 +++++++++++++++---- test/services/key-shortcuts.ts | 56 +++++++++++++ 4 files changed, 150 insertions(+), 18 deletions(-) diff --git a/lib/services/bundler/bundler-compiler-service.ts b/lib/services/bundler/bundler-compiler-service.ts index 4dcadad09a..5d3d071ecc 100644 --- a/lib/services/bundler/bundler-compiler-service.ts +++ b/lib/services/bundler/bundler-compiler-service.ts @@ -39,6 +39,7 @@ import { resolvePackagePath, resolvePackageJSONPath, } from "../../helpers/package-path-helper"; +import { commandShortcutsOwnTerminal } from "../key-shortcuts"; // todo: move out of here interface IBundlerMessage { @@ -739,9 +740,14 @@ export class BundlerCompilerService // Output is inherited so the dev server's URLs/logs stream to the // user. No IPC needed here — the build watcher provides the // bundle-complete IPC; the dev server is fetched over HTTP/ws. - // stdin must not be the terminal: Vite reads it for shortcuts of - // its own and would take the keys meant for the CLI's. - stdio: ["ignore", "inherit", "inherit"], + // Vite reads a terminal stdin for shortcuts of its own, which would + // take the keys meant for the CLI's; otherwise its shortcuts are the + // only ones the terminal has. + stdio: [ + commandShortcutsOwnTerminal() ? "ignore" : "inherit", + "inherit", + "inherit", + ], env: { ...process.env, NATIVESCRIPT_BUNDLER_ENV: JSON.stringify(envData), diff --git a/lib/services/key-shortcuts.ts b/lib/services/key-shortcuts.ts index 36b21d8e99..b826a2e2ed 100644 --- a/lib/services/key-shortcuts.ts +++ b/lib/services/key-shortcuts.ts @@ -5,6 +5,7 @@ import { RunOnDeviceEvents } from "../constants"; import { IChildProcess } from "../common/declarations"; import { Injector } from "../common/di/injector"; import { + commandShortcutsEnabled, envSwitchIsOn, IKeyShortcutService, KeyContextBase, @@ -125,6 +126,23 @@ export function keyShortcutsEnabled(): boolean { return !!process.stdin.isTTY; } +function isRawModeTerminal(stdin: NodeJS.ReadStream): boolean { + return !!stdin.isTTY && typeof stdin.setRawMode === "function"; +} + +/** + * Whether a command's shortcuts, once attached, read the keys straight off + * the terminal. Answerable before the attach, so a child spawned earlier can + * be kept off a terminal the CLI is about to read. + */ +export function commandShortcutsOwnTerminal(): boolean { + return ( + commandShortcutsEnabled() && + isRawModeTerminal(process.stdin) && + keyShortcutsEnabled() + ); +} + /** * The names `devicePlatformsConstants` hands out, read off the constants * rather than spelled again here. @@ -420,7 +438,7 @@ export class KeyShortcutService implements IKeyShortcutService { ); const stdin = process.stdin; - if (!stdin.isTTY || typeof stdin.setRawMode !== "function") { + if (!isRawModeTerminal(stdin)) { if (typeof process.send !== "function") { this.releaseShortcuts(); return false; diff --git a/test/services/bundler/bundler-compiler-service.ts b/test/services/bundler/bundler-compiler-service.ts index cdf301d7bd..ba885a6ae0 100644 --- a/test/services/bundler/bundler-compiler-service.ts +++ b/test/services/bundler/bundler-compiler-service.ts @@ -374,8 +374,41 @@ describe("BundlerCompilerService", () => { }); describe("startViteDevServer", () => { - it("does not hand the dev server the terminal's stdin", async () => { - const spawned: { [key: string]: any }[] = []; + const env = ["NS_COMMAND_SHORTCUTS", "NS_KEY_SHORTCUTS"]; + let savedEnv: { [key: string]: string }; + let originalStdin: PropertyDescriptor; + let spawned: { [key: string]: any }[]; + + const useStdin = (isTTY: boolean): void => { + Object.defineProperty(process, "stdin", { + value: { isTTY, setRawMode: (): void => undefined }, + configurable: true, + }); + }; + + const devServerStdin = async (): Promise => { + await (bundlerCompilerService).startViteDevServer( + { platformNameLowerCase: "ios" }, + { + projectDir: "/project", + bundlerConfigPath: "/project/vite.config.ts", + }, + { watch: true, hmr: true }, + ); + assert.lengthOf(spawned, 1); + + return spawned[0].stdio[0]; + }; + + beforeEach(() => { + savedEnv = {}; + for (const name of env) { + savedEnv[name] = process.env[name]; + delete process.env[name]; + } + originalStdin = Object.getOwnPropertyDescriptor(process, "stdin"); + + spawned = []; testInjector.resolve("childProcess").spawn = ( _command: string, _args: string[], @@ -390,20 +423,39 @@ describe("BundlerCompilerService", () => { service.buildEnvCommandLineParams = async (): Promise => []; service.getBundlerExecutablePath = () => "/project/vite.js"; service.waitForPort = async () => true; + }); - await service.startViteDevServer( - { platformNameLowerCase: "ios" }, - { - projectDir: "/project", - bundlerConfigPath: "/project/vite.config.ts", - }, - { watch: true, hmr: true }, - ); + afterEach(() => { + Object.defineProperty(process, "stdin", originalStdin); + for (const name of env) { + if (savedEnv[name] === undefined) { + delete process.env[name]; + } else { + process.env[name] = savedEnv[name]; + } + } + }); - assert.lengthOf(spawned, 1); - const { stdio } = spawned[0]; - assert.isArray(stdio); - assert.equal(stdio[0], "ignore"); + it("keeps the dev server off the terminal the CLI's shortcuts read", async () => { + process.env.NS_COMMAND_SHORTCUTS = "1"; + process.env.NS_KEY_SHORTCUTS = "true"; + useStdin(true); + + assert.equal(await devServerStdin(), "ignore"); + }); + + it("hands the dev server the terminal while the CLI's shortcuts are off", async () => { + useStdin(true); + + assert.equal(await devServerStdin(), "inherit"); + }); + + it("hands the dev server its stdin when the CLI has no terminal to read", async () => { + process.env.NS_COMMAND_SHORTCUTS = "1"; + process.env.NS_KEY_SHORTCUTS = "true"; + useStdin(false); + + assert.equal(await devServerStdin(), "inherit"); }); }); diff --git a/test/services/key-shortcuts.ts b/test/services/key-shortcuts.ts index e51354b35a..decfe5667c 100644 --- a/test/services/key-shortcuts.ts +++ b/test/services/key-shortcuts.ts @@ -5,6 +5,7 @@ import { getContractName } from "../../lib/common/di/contract"; import { Injector } from "../../lib/common/di/injector"; import { KeyShortcutRegistryService } from "../../lib/services/key-shortcut-registry"; import { + commandShortcutsOwnTerminal, findShortcut, KeyContextBase, KeyShortcut, @@ -473,6 +474,61 @@ describe("key shortcuts", () => { }); }); + describe("commandShortcutsOwnTerminal", () => { + const env = [ + "NS_COMMAND_SHORTCUTS", + "NS_KEY_SHORTCUTS", + "CI", + "JENKINS_HOME", + ]; + let saved: { [key: string]: string }; + let stdin: FakeStdin; + let restoreStdin: () => void; + + beforeEach(() => { + saved = {}; + for (const name of env) { + saved[name] = process.env[name]; + delete process.env[name]; + } + process.env.NS_COMMAND_SHORTCUTS = "1"; + stdin = new FakeStdin(); + restoreStdin = swapStdin(stdin); + }); + + afterEach(() => { + restoreStdin(); + for (const name of env) { + if (saved[name] === undefined) { + delete process.env[name]; + } else { + process.env[name] = saved[name]; + } + } + }); + + it("holds for a command's shortcuts on a terminal", () => { + assert.isTrue(commandShortcutsOwnTerminal()); + }); + + it("does not hold while command shortcuts are off", () => { + delete process.env.NS_COMMAND_SHORTCUTS; + assert.isFalse(commandShortcutsOwnTerminal()); + }); + + it("does not hold when key shortcuts are switched off", () => { + process.env.NS_KEY_SHORTCUTS = "false"; + assert.isFalse(commandShortcutsOwnTerminal()); + }); + + it("does not hold for a stdin that is not a terminal, even when forced on", () => { + // A `ns run` spawned by `ns start`: its keys come over IPC. + process.env.NS_KEY_SHORTCUTS = "true"; + stdin.isTTY = false; + assert.isFalse(commandShortcutsOwnTerminal()); + }); + }); + describe("KeyShortcutService", () => { let stdin: FakeStdin; let restoreStdin: () => void;