From 4a343bc49615e440a7343c79bd1b1d14ad4e084d Mon Sep 17 00:00:00 2001 From: Kalven Schraut Date: Thu, 10 Sep 2026 18:12:18 -0500 Subject: [PATCH] fix(desktop): allow Neovim console access on older WSL --- .../scripts/neovim-terminal/transport.mjs | 5 +- .../neovim-terminal/transport.test.mjs | 70 +++++++++++++++++++ 2 files changed, 73 insertions(+), 2 deletions(-) diff --git a/apps/desktop/scripts/neovim-terminal/transport.mjs b/apps/desktop/scripts/neovim-terminal/transport.mjs index 1d88d3b0a53fd..6ee1bcfb1a3c0 100644 --- a/apps/desktop/scripts/neovim-terminal/transport.mjs +++ b/apps/desktop/scripts/neovim-terminal/transport.mjs @@ -151,11 +151,12 @@ export function wslArgs(route) { export function run(command, args, { input, timeout, cwd, capture = false, env } = {}) { return new Promise((resolve, reject) => { // Packaged Electron can inherit a pipe as fd 0 from PowerShell even in a - // terminal window. Open the console explicitly for the interactive child. + // terminal window. Open the console read/write for interactive children: + // WSL 2.2.4 rejects a read-only handle with Wsl/Service/E_ACCESSDENIED. const consoleInput = // oxlint-disable-next-line t3code/no-global-process-runtime -- Standalone packaged helper owns its platform-specific console handles. input === undefined && process.platform === "win32" - ? NodeFS.openSync("\\\\.\\CONIN$", "r") + ? NodeFS.openSync("\\\\.\\CONIN$", "r+") : undefined; let child; try { diff --git a/apps/desktop/scripts/neovim-terminal/transport.test.mjs b/apps/desktop/scripts/neovim-terminal/transport.test.mjs index 7697fc74e8b9b..e114fc4cb6228 100644 --- a/apps/desktop/scripts/neovim-terminal/transport.test.mjs +++ b/apps/desktop/scripts/neovim-terminal/transport.test.mjs @@ -34,6 +34,76 @@ const request = (overrides = {}) => ({ ...overrides, }); +// oxlint-disable-next-line t3code/no-global-process-runtime -- Exercises native console handles in the standalone Windows helper. +test.skipIf(process.platform !== "win32")( + "interactive children receive console input with read and write access", + async () => { + const directory = await NodeFSP.mkdtemp(NodePath.join(NodeOS.tmpdir(), "t3-console-access-")); + const powershell = NodePath.join( + process.env.SystemRoot, + "System32/WindowsPowerShell/v1.0/powershell.exe", + ); + const encode = (script) => Buffer.from(script, "utf16le").toString("base64"); + const checkAccess = encode(` +$ErrorActionPreference = 'Stop' +Add-Type @' +using System; +using System.Runtime.InteropServices; +public static class InputAccess { + [DllImport("kernel32.dll")] public static extern IntPtr GetStdHandle(int id); + [DllImport("kernel32.dll", SetLastError=true)] public static extern bool GetConsoleMode(IntPtr handle, out uint mode); + [DllImport("kernel32.dll", SetLastError=true)] public static extern bool FlushConsoleInputBuffer(IntPtr handle); +} +'@ +$handle = [InputAccess]::GetStdHandle(-10) +[uint32]$mode = 0 +if (-not [InputAccess]::GetConsoleMode($handle, [ref]$mode)) { throw 'Console read access is missing' } +if (-not [InputAccess]::FlushConsoleInputBuffer($handle)) { throw 'Console write access is missing' } +Write-Output 'console-read-write-ok' +`); + // Allocate a private console so the access check never flushes a user's input. + const launch = encode(` +$ErrorActionPreference = 'Stop' +Add-Type @' +using System.Runtime.InteropServices; +public static class PrivateConsole { + [DllImport("kernel32.dll")] public static extern bool FreeConsole(); + [DllImport("kernel32.dll")] public static extern bool AllocConsole(); +} +'@ +[PrivateConsole]::FreeConsole() | Out-Null +if (-not [PrivateConsole]::AllocConsole()) { throw 'Could not allocate test console' } +try { + $child = Start-Process -FilePath $env:T3_TEST_CONSOLE_RUNTIME -ArgumentList ('"' + $env:T3_TEST_CONSOLE_SCRIPT + '"') -NoNewWindow -Wait -PassThru + exit $child.ExitCode +} finally { [PrivateConsole]::FreeConsole() | Out-Null } +`); + try { + const script = NodePath.join(directory, "check.mjs"); + const transport = new URL("transport.mjs", import.meta.url).href; + await NodeFSP.writeFile( + script, + `import { run } from ${JSON.stringify(transport)}; +console.log(await run(${JSON.stringify(powershell)}, ['-NoProfile', '-EncodedCommand', ${JSON.stringify(checkAccess)}], { capture: true, timeout: 15000 })); +`, + ); + const output = await run(powershell, ["-NoProfile", "-EncodedCommand", launch], { + input: "", + capture: true, + timeout: 30000, + env: { + ...process.env, + T3_TEST_CONSOLE_RUNTIME: process.execPath, + T3_TEST_CONSOLE_SCRIPT: script, + }, + }); + NodeAssert.match(output, /console-read-write-ok/u); + } finally { + await NodeFSP.rm(directory, { recursive: true, force: true }); + } + }, +); + test("target bytes remain data across token and Windows Terminal grammar", () => { for (const name of [ "spaces here",