@devries-cameron20 / t3code-1 / commits / 215bd59a98
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

fix(server): Windows uninstall fails when its cleanup shell cannot start (#38043)

Co-authored-by: Grok 4.7 <noreply@x.ai> Co-authored-by: Julius Marminge <julius0216@outlook.com>

Wout Stiens committed Nov 17, 2026 at 11:02 UTC 215bd59a9851d439b3c04e5532d2d584d6883142
4 files changed +107 -14
apps/server/src/cli/uninstall.test.ts
+65 -1
@@ -1,3 +1,5 @@
1 +// @effect-diagnostics nodeBuiltinImport:off - The test observes a real cleanup-shell spawn failure.
2 +import * as NodeChildProcess from "node:child_process";
3 import * as NodeServices from "@effect/platform-node/NodeServices";
4 import { assert, it } from "@effect/vitest";
5 import * as Config from "effect/Config";
@@ -10,7 +12,7 @@ import * as TestConsole from "effect/testing/TestConsole";
12 import { Command } from "effect/cli";
13 import * as HostProcess from "@t3tools/shared/HostProcess";
14 import * as NetService from "@t3tools/shared/Net";
13 -import { afterEach, vi } from "vite-plus/test";
15 +import { afterEach, expect, vi } from "vite-plus/test";
16
17 import packageJson from "../../package.json" with { type: "json" };
18 import * as BootService from "../cloud/bootService.ts";
@@ -22,8 +24,70 @@ import {
24 uninstallCommand,
25 } from "./uninstall.ts";
26
27 +vi.mock("node:child_process", { spy: true });
28 +
29 afterEach(() => vi.restoreAllMocks());
30
31 +it.live("reports failure when the Windows cleanup shell cannot start", () =>
32 + Effect.gen(function* () {
33 + const fs = yield* FileSystem.FileSystem;
34 + const path = yield* Path.Path;
35 + const baseDir = yield* fs.makeTempDirectoryScoped({ prefix: "t3-uninstall-shell-" });
36 + const runtimeDir = path.join(baseDir, "runtime");
37 + yield* fs.makeDirectory(runtimeDir);
38 + vi.spyOn(BootService, "layer").mockReturnValue(
39 + Layer.succeed(
40 + BootService.BootService,
41 + BootService.BootService.of({
42 + status: Effect.succeed({
43 + supported: false,
44 + installed: false,
45 + current: false,
46 + unitPath: "",
47 + logPath: "",
48 + }),
49 + withStopped: (operation) => operation,
50 + restart: Effect.die("unexpected restart"),
51 + install: () => Effect.die("unexpected install"),
52 + uninstall: Effect.die("unexpected uninstall"),
53 + }),
54 + ),
55 + );
56 + const actual = yield* Effect.promise(() =>
57 + vi.importActual<typeof import("node:child_process")>("node:child_process"),
58 + );
59 + const spawn = actual.spawn;
60 + const failedSpawn = new Promise<Error>((resolve) => {
61 + vi.spyOn(NodeChildProcess, "spawn").mockImplementation((...args) => {
62 + const child = spawn(...args);
63 + // Observe the real spawn error without letting it terminate the test runner.
64 + child.once("error", resolve);
65 + return child;
66 + });
67 + });
68 + const result = yield* Command.runWith(uninstallCommand, { version: packageJson.version })([
69 + "--base-dir",
70 + baseDir,
71 + "--yes",
72 + ]).pipe(
73 + Effect.result,
74 + Effect.provideService(HostProcess.Environment, {
75 + ComSpec: path.join(baseDir, "missing-cmd.exe"),
76 + }),
77 + Effect.provideService(HostProcess.Platform, "win32"),
78 + Effect.provideService(HostProcess.IsExecutable, false),
79 + Effect.provide(
80 + Layer.mergeAll(
81 + ConfigProvider.layer(ConfigProvider.fromEnv({ env: {} })),
82 + Layer.fresh(TestConsole.layer),
83 + ),
84 + ),
85 + );
86 + yield* Effect.promise(() => failedSpawn);
87 + expect(result._tag, "an unavailable cleanup shell must fail uninstall").toBe("Failure");
88 + }).pipe(Effect.scoped, Effect.provide(Layer.mergeAll(NodeServices.layer, NetService.layer))),
89 +);
90 +
91 it.layer(Layer.mergeAll(NodeServices.layer, NetService.layer))("t3 uninstall command", (it) => {
92 it.effect("names another home's service it leaves installed", () =>
93 Effect.gen(function* () {
apps/server/src/cli/uninstall.ts
+8 -7
@@ -22,6 +22,7 @@ import {
22 } from "../cloud/pinnedRuntime.ts";
23 import { projectLocationFlags, resolveCliAuthConfig } from "./config.ts";
24 import * as CliService from "./service.ts";
25 +import { awaitDetachedSpawn } from "../process/windowsVerbatimSpawn.ts";
26 import { findWindowsShim } from "./update.ts";
27
28 export class CliUninstallError extends Schema.TaggedError<CliUninstallError>()(
@@ -277,13 +278,13 @@ const runUninstall = Effect.fn("cli.uninstall.run")(function* (input: {
278 if (platform === "win32") {
279 const runtimeDir = plan.runtimeDir;
280 const comspec = environment["ComSpec"] ?? environment["COMSPEC"] ?? "cmd.exe";
280 - yield* Effect.try({
281 - try: () => spawnWindowsRemoval(comspec, runtimeDir).unref(),
282 - catch: () =>
283 - new CliUninstallError({
284 - reason: `Could not schedule removal of ${runtimeDir}. Delete it yourself once this window is closed.`,
285 - }),
286 - });
281 + const notScheduled = () =>
282 + new CliUninstallError({
283 + reason: `Could not schedule removal of ${runtimeDir}. Delete it yourself once this window is closed.`,
284 + });
285 + yield* awaitDetachedSpawn(() => spawnWindowsRemoval(comspec, runtimeDir)).pipe(
286 + Effect.mapError(notScheduled),
287 + );
288 yield* Console.log(`${runtimeDir} will be removed once t3 exits.`);
289 } else {
290 yield* fs
apps/server/src/process/windowsVerbatimSpawn.test.ts new
+15
@@ -0,0 +1,15 @@
1 +import { it } from "@effect/vitest";
2 +import * as Effect from "effect/Effect";
3 +import { expect } from "vite-plus/test";
4 +
5 +import { awaitDetachedSpawn } from "./windowsVerbatimSpawn.ts";
6 +
7 +it.effect("reports a synchronous spawn exception as a failure", () =>
8 + Effect.gen(function* () {
9 + const error = new Error("invalid spawn options");
10 + const result = yield* awaitDetachedSpawn(() => {
11 + throw error;
12 + }).pipe(Effect.result);
13 + expect(result).toMatchObject({ _tag: "Failure", failure: error });
14 + }),
15 +);
apps/server/src/process/windowsVerbatimSpawn.ts
+19 -6
@@ -1,6 +1,7 @@
1 // @effect-diagnostics nodeBuiltinImport:off - Effect's spawner has no windowsVerbatimArguments option.
2 import * as NodeChildProcess from "node:child_process";
3
4 +import * as Cause from "effect/Cause";
5 import * as Context from "effect/Context";
6 import * as Effect from "effect/Effect";
7
@@ -9,13 +10,16 @@ export type WindowsVerbatimSpawner = (
10 args: ReadonlyArray<string>,
11 ) => Effect.Effect<void, Error>;
12
12 -const spawnDetachedVerbatim: WindowsVerbatimSpawner = (command, args) =>
13 +/** Succeeds once a detached child has started and no longer holds this process open; fails if it cannot start. */
14 +export const awaitDetachedSpawn = (spawn: () => NodeChildProcess.ChildProcess) =>
15 Effect.callback<void, Error>((resume) => {
14 - const child = NodeChildProcess.spawn(command, args, {
15 - detached: true,
16 - stdio: "ignore",
17 - windowsVerbatimArguments: true,
18 - });
16 + let child: NodeChildProcess.ChildProcess;
17 + try {
18 + child = spawn();
19 + } catch (error) {
20 + resume(Effect.fail(error instanceof Error ? error : new Cause.UnknownError(error)));
21 + return;
22 + }
23 child.once("error", (error) => resume(Effect.fail(error)));
24 child.once("spawn", () => {
25 child.unref();
@@ -23,6 +27,15 @@ const spawnDetachedVerbatim: WindowsVerbatimSpawner = (command, args) =>
27 });
28 });
29
30 +const spawnDetachedVerbatim: WindowsVerbatimSpawner = (command, args) =>
31 + awaitDetachedSpawn(() =>
32 + NodeChildProcess.spawn(command, args, {
33 + detached: true,
34 + stdio: "ignore",
35 + windowsVerbatimArguments: true,
36 + }),
37 + );
38 +
39 export const WindowsVerbatimSpawn = Context.Reference<WindowsVerbatimSpawner>(
40 "t3/process/WindowsVerbatimSpawn",
41 { defaultValue: () => spawnDetachedVerbatim },