Skip to content

Commit 6dfb1a8

Browse files
fix(server): t3 uninstall names another home's service it leaves installed (pingdotgg#35684)
1 parent 7d51718 commit 6dfb1a8

2 files changed

Lines changed: 83 additions & 4 deletions

File tree

‎apps/server/src/cli/uninstall.test.ts‎

Lines changed: 67 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,78 @@
11
import * as NodeServices from "@effect/platform-node/NodeServices";
22
import { assert, it } from "@effect/vitest";
33
import * as Config from "effect/Config";
4+
import * as ConfigProvider from "effect/ConfigProvider";
45
import * as Effect from "effect/Effect";
56
import * as FileSystem from "effect/FileSystem";
7+
import * as Layer from "effect/Layer";
68
import * as Path from "effect/Path";
9+
import * as TestConsole from "effect/testing/TestConsole";
10+
import { Command } from "effect/cli";
711
import * as HostProcess from "@t3tools/shared/HostProcess";
12+
import * as NetService from "@t3tools/shared/Net";
13+
import { afterEach, vi } from "vite-plus/test";
814

9-
import { findOwnedLauncher, findRecordedLaunchers, spawnWindowsRemoval } from "./uninstall.ts";
15+
import packageJson from "../../package.json" with { type: "json" };
16+
import * as BootService from "../cloud/bootService.ts";
17+
import {
18+
findOwnedLauncher,
19+
findRecordedLaunchers,
20+
spawnWindowsRemoval,
21+
uninstallCommand,
22+
} from "./uninstall.ts";
23+
24+
afterEach(() => vi.restoreAllMocks());
25+
26+
it.layer(Layer.mergeAll(NodeServices.layer, NetService.layer))("t3 uninstall command", (it) => {
27+
it.effect("names another home's service it leaves installed", () =>
28+
Effect.gen(function* () {
29+
const fs = yield* FileSystem.FileSystem;
30+
const baseDir = yield* fs.makeTempDirectoryScoped({ prefix: "t3-uninstall-cli-test-" });
31+
const uninstalls: Array<true> = [];
32+
vi.spyOn(BootService, "layer").mockReturnValue(
33+
Layer.succeed(
34+
BootService.BootService,
35+
BootService.BootService.of({
36+
status: Effect.succeed({
37+
supported: true,
38+
installed: true,
39+
current: true,
40+
installedBaseDir: "/home/me/.t3",
41+
unitPath: "/home/me/.config/systemd/user/t3code.service",
42+
logPath: "/home/me/.t3/userdata/logs/boot-service.log",
43+
}),
44+
withStopped: (operation) => operation,
45+
restart: Effect.die("unexpected restart"),
46+
install: () => Effect.die("unexpected install"),
47+
uninstall: Effect.sync(() => {
48+
uninstalls.push(true);
49+
return true;
50+
}),
51+
}),
52+
),
53+
);
54+
55+
const output = yield* Command.runWith(uninstallCommand, { version: packageJson.version })([
56+
"--base-dir",
57+
baseDir,
58+
"--yes",
59+
]).pipe(
60+
Effect.andThen(TestConsole.logLines),
61+
Effect.map((lines) => lines.join("\n")),
62+
Effect.provideService(HostProcess.Environment, {}),
63+
Effect.provideService(HostProcess.IsExecutable, false),
64+
Effect.provide(
65+
Layer.mergeAll(
66+
ConfigProvider.layer(ConfigProvider.fromEnv({ env: {} })),
67+
Layer.fresh(TestConsole.layer),
68+
),
69+
),
70+
);
71+
assert.deepStrictEqual(uninstalls, []);
72+
assert.include(output, "--base-dir '/home/me/.t3'");
73+
}),
74+
);
75+
});
1076

1177
it.layer(NodeServices.layer)("t3 uninstall launcher", (it) => {
1278
it.effect("claims only a launcher that points into this home's runtime tree", () =>

‎apps/server/src/cli/uninstall.ts‎

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ export class CliUninstallError extends Schema.TaggedError<CliUninstallError>()(
3333

3434
export interface UninstallPlan {
3535
readonly service: boolean;
36+
readonly otherHomeService: string | undefined;
3637
readonly versionsDir: string;
3738
readonly launchers: ReadonlyArray<string>;
3839
readonly runtimeDir: string | undefined;
@@ -147,15 +148,18 @@ const planUninstall = Effect.fn("cli.uninstall.plan")(function* (input: {
147148
const path = yield* Path.Path;
148149
const service = yield* BootService.BootService;
149150
const status = yield* service.status;
151+
const installedBaseDir =
152+
status.supported && status.installed ? status.installedBaseDir : undefined;
150153
const servesThisHome =
151-
status.installedBaseDir !== undefined &&
152-
path.resolve(status.installedBaseDir) === path.resolve(input.baseDir);
154+
installedBaseDir !== undefined &&
155+
path.resolve(installedBaseDir) === path.resolve(input.baseDir);
153156
const versionsDir = pinnedRuntimeVersionsDir(path, input.baseDir);
154157
const runtimeDir = path.dirname(versionsDir);
155158
const launchedAs = (yield* HostProcess.IsExecutable) ? yield* resolveLauncherPath : undefined;
156159
const launchedFrom = yield* findOwnedLauncher({ launchedAs, versionsDir });
157160
const plan: UninstallPlan = {
158-
service: status.supported && status.installed && servesThisHome,
161+
service: servesThisHome,
162+
otherHomeService: servesThisHome ? undefined : installedBaseDir,
159163
versionsDir,
160164
launchers: [
161165
...new Set([
@@ -195,6 +199,13 @@ export const uninstallCommand = Command.make("uninstall", {
195199
),
196200
);
197201

202+
const logOtherHomeService = (plan: UninstallPlan) =>
203+
plan.otherHomeService === undefined
204+
? Effect.void
205+
: Console.log(
206+
`The T3 Code service serves ${plan.otherHomeService}, so it was not removed. Pass \`${BootService.formatBootServiceBaseDirOption(plan.otherHomeService)}\` to remove it.`,
207+
);
208+
198209
const runUninstall = Effect.fn("cli.uninstall.run")(function* (input: {
199210
readonly baseDir: string;
200211
readonly assumeYes: boolean;
@@ -212,6 +223,7 @@ const runUninstall = Effect.fn("cli.uninstall.run")(function* (input: {
212223
" This t3 runs from a Node script, so it was installed by npm or built from source. Remove it the same way (`npm uninstall -g t3`, or delete the checkout).",
213224
);
214225
}
226+
yield* logOtherHomeService(plan);
215227
return;
216228
}
217229

@@ -286,4 +298,5 @@ const runUninstall = Effect.fn("cli.uninstall.run")(function* (input: {
286298
}
287299
yield* Console.log("");
288300
yield* Console.log("t3 is uninstalled. Thanks for trying T3 Code.");
301+
yield* logOtherHomeService(plan);
289302
});

0 commit comments

Comments
 (0)