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
This commit is contained in:
1 parent
935e7c6cea
commit
0cdb35e73b
4 files changed
+113
-19
No files matched your search
@@ -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<SpawnCall> = [];
|
||||
const originalValues = new Map<string, string | undefined>();
|
||||
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;
|
||||
|
||||
@@ -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* (
|
||||
|
||||
@@ -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();
|
||||
|
||||
@@ -1,10 +1,19 @@
|
||||
import { spawnSync, type ChildProcess as NodeChildProcess } from "node:child_process";
|
||||
|
||||
type KillableChildProcess = Pick<NodeChildProcess, "kill" | "pid">;
|
||||
type TaskkillResult = {
|
||||
readonly status: number | null;
|
||||
readonly error?: Error;
|
||||
};
|
||||
type TaskkillRunner = (
|
||||
command: string,
|
||||
args: ReadonlyArray<string>,
|
||||
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.
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user