From 0cdb35e73b8485286444158d29207651eb3c4ba5 Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Tue, 31 Mar 2026 12:56:35 -0700 Subject: [PATCH] Skip WSL browser launch in containers - Treat container env detection consistently when resolving desktop launch options - Fall back to direct kill if `taskkill` exits nonzero or cannot spawn - Add coverage for the WSL container and taskkill fallback cases --- .../process/Layers/DesktopLauncher.test.ts | 68 +++++++++++++++++++ .../src/process/Layers/DesktopLauncher.ts | 21 +++--- apps/server/src/process/killTree.test.ts | 18 +++-- apps/server/src/process/killTree.ts | 25 +++++-- 4 files changed, 113 insertions(+), 19 deletions(-) diff --git a/apps/server/src/process/Layers/DesktopLauncher.test.ts b/apps/server/src/process/Layers/DesktopLauncher.test.ts index e6cc724987..8fb2950be9 100644 --- a/apps/server/src/process/Layers/DesktopLauncher.test.ts +++ b/apps/server/src/process/Layers/DesktopLauncher.test.ts @@ -620,6 +620,74 @@ it.effect("openBrowser falls back from WSL PowerShell to xdg-open", () => }).pipe(Effect.provide(NodeFileSystem.layer)), ); +it.effect("openBrowser skips WSL PowerShell when process env marks a container", () => + Effect.gen(function* () { + const fs = yield* FileSystem.FileSystem; + const dir = yield* fs.makeTempDirectoryScoped({ prefix: "t3-open-browser-wsl-container-" }); + yield* fs.makeDirectory(`${dir}/mnt/c/Windows/System32/WindowsPowerShell/v1.0`, { + recursive: true, + }); + yield* writeExecutable(`${dir}/mnt/c/Windows/System32/WindowsPowerShell/v1.0/powershell.exe`); + yield* writeExecutable(`${dir}/xdg-open`); + + const calls: Array = []; + const originalValues = new Map(); + const setEnv = (key: string, value: string | undefined) => { + if (!originalValues.has(key)) { + originalValues.set(key, process.env[key]); + } + + if (value === undefined) { + delete process.env[key]; + return; + } + + process.env[key] = value; + }; + const restoreEnv = () => { + for (const [key, value] of originalValues) { + if (value === undefined) { + delete process.env[key]; + } else { + process.env[key] = value; + } + } + }; + + try { + setEnv("PATH", `${dir}:${dir}/mnt/c/Windows/System32/WindowsPowerShell/v1.0`); + setEnv("WSL_DISTRO_NAME", "Ubuntu"); + setEnv("CONTAINER", "docker"); + + const open = yield* runOpen( + { + platform: "linux", + isWsl: true, + powerShellCommand: `${dir}/mnt/c/Windows/System32/WindowsPowerShell/v1.0/powershell.exe`, + }, + spawnHarness(calls), + readOpen, + ); + + yield* open.openBrowser("https://example.com"); + } finally { + restoreEnv(); + } + + assert.deepEqual(calls, [ + { + command: `${dir}/xdg-open`, + args: ["https://example.com"], + detached: true, + shell: false, + stdin: "ignore", + stdout: "ignore", + stderr: "ignore", + }, + ]); + }).pipe(Effect.provide(NodeFileSystem.layer)), +); + it.effect("openInEditor uses xdg-open first for WSL file-manager paths", () => Effect.gen(function* () { const fs = yield* FileSystem.FileSystem; diff --git a/apps/server/src/process/Layers/DesktopLauncher.ts b/apps/server/src/process/Layers/DesktopLauncher.ts index e276abad06..57061a4b14 100644 --- a/apps/server/src/process/Layers/DesktopLauncher.ts +++ b/apps/server/src/process/Layers/DesktopLauncher.ts @@ -524,29 +524,30 @@ export const make = Effect.fn("makeDesktopLauncher")(function* ( const fileSystem = yield* FileSystem.FileSystem; const pathService = yield* Path.Path; const spawner = yield* ChildProcessSpawner.ChildProcessSpawner; + const runtimeEnv = options.env ?? process.env; + const runtimePlatform = options.platform ?? process.platform; + const isWsl = options.isWsl ?? detectWsl(runtimePlatform, runtimeEnv); const isInsideContainer = options.isInsideContainer ?? - (typeof options.env?.CONTAINER === "string" || - typeof options.env?.container === "string" || - typeof options.env?.KUBERNETES_SERVICE_HOST === "string" + (typeof runtimeEnv.CONTAINER === "string" || + typeof runtimeEnv.container === "string" || + typeof runtimeEnv.KUBERNETES_SERVICE_HOST === "string" ? true : yield* fileSystem.exists("/.dockerenv").pipe(Effect.catch(() => Effect.succeed(false)))); const runtime: LaunchRuntime = { - platform: options.platform ?? process.platform, - env: options.env ?? process.env, - isWsl: - options.isWsl ?? detectWsl(options.platform ?? process.platform, options.env ?? process.env), + platform: runtimePlatform, + env: runtimeEnv, + isWsl, isInsideContainer, powerShellCandidates: options.powerShellCommand !== undefined ? [options.powerShellCommand] - : (options.isWsl ?? - detectWsl(options.platform ?? process.platform, options.env ?? process.env)) + : isWsl ? WSL_POWERSHELL_CANDIDATES : WINDOWS_POWERSHELL_CANDIDATES, - windowsPathExtensions: resolveWindowsPathExtensions(options.env ?? process.env), + windowsPathExtensions: resolveWindowsPathExtensions(runtimeEnv), }; const resolveCommand = Effect.fn("resolveCommand")(function* ( diff --git a/apps/server/src/process/killTree.test.ts b/apps/server/src/process/killTree.test.ts index 9e48e106f4..cae8823638 100644 --- a/apps/server/src/process/killTree.test.ts +++ b/apps/server/src/process/killTree.test.ts @@ -5,7 +5,7 @@ import { killChildProcessTree } from "./killTree"; describe("killChildProcessTree", () => { it("uses taskkill on Windows when a pid is available", () => { const kill = vi.fn(); - const spawnSyncImpl = vi.fn(); + const spawnSyncImpl = vi.fn(() => ({ status: 0 })); killChildProcessTree({ pid: 123, kill } as never, "SIGTERM", { platform: "win32", @@ -20,9 +20,7 @@ describe("killChildProcessTree", () => { it("falls back to direct kill when taskkill fails", () => { const kill = vi.fn(); - const spawnSyncImpl = vi.fn(() => { - throw new Error("taskkill unavailable"); - }); + const spawnSyncImpl = vi.fn(() => ({ status: 1 })); killChildProcessTree({ pid: 456, kill } as never, "SIGKILL", { platform: "win32", @@ -32,6 +30,18 @@ describe("killChildProcessTree", () => { expect(kill).toHaveBeenCalledWith("SIGKILL"); }); + it("falls back to direct kill when taskkill cannot be spawned", () => { + const kill = vi.fn(); + const spawnSyncImpl = vi.fn(() => ({ status: null, error: new Error("ENOENT") })); + + killChildProcessTree({ pid: 654, kill } as never, "SIGTERM", { + platform: "win32", + spawnSyncImpl, + }); + + expect(kill).toHaveBeenCalledWith("SIGTERM"); + }); + it("kills directly on non-Windows platforms", () => { const kill = vi.fn(); const spawnSyncImpl = vi.fn(); diff --git a/apps/server/src/process/killTree.ts b/apps/server/src/process/killTree.ts index dd78574718..44e017dc48 100644 --- a/apps/server/src/process/killTree.ts +++ b/apps/server/src/process/killTree.ts @@ -1,10 +1,19 @@ import { spawnSync, type ChildProcess as NodeChildProcess } from "node:child_process"; type KillableChildProcess = Pick; +type TaskkillResult = { + readonly status: number | null; + readonly error?: Error; +}; +type TaskkillRunner = ( + command: string, + args: ReadonlyArray, + options: { readonly stdio: "ignore" }, +) => TaskkillResult; interface KillChildProcessTreeOptions { readonly platform?: NodeJS.Platform; - readonly spawnSyncImpl?: typeof spawnSync; + readonly spawnSyncImpl?: TaskkillRunner; } /** @@ -19,10 +28,16 @@ export function killChildProcessTree( const platform = options.platform ?? process.platform; if (platform === "win32" && child.pid !== undefined) { try { - (options.spawnSyncImpl ?? spawnSync)("taskkill", ["/pid", String(child.pid), "/T", "/F"], { - stdio: "ignore", - }); - return; + const result = (options.spawnSyncImpl ?? spawnSync)( + "taskkill", + ["/pid", String(child.pid), "/T", "/F"], + { + stdio: "ignore", + }, + ); + if (!result.error && result.status === 0) { + return; + } } catch { // Fall through to direct kill when taskkill is unavailable. }