TypeScript
fix(server): t3 service uninstall leaves another T3 home's service alone (#29872)
Alex Southwell99ccd4785af9
diff --git a/apps/server/src/cli/service.test.ts b/apps/server/src/cli/service.test.tsindex 4358165958a..7ab52e82a39 100644--- a/apps/server/src/cli/service.test.ts+++ b/apps/server/src/cli/service.test.ts@@ -32,6 +32,7 @@ function makeTestService() {const installOptions: Array<Parameters<BootService.BootService["Service"]["install"]>[0]> = [];const restarts: Array<true> = [];+ const uninstalls: Array<true> = [];const service = BootService.BootService.of({status: Effect.succeed(serviceStatus),withStopped: (operation) => operation,@@ -49,9 +50,12 @@ function makeTestService(logPath: serviceStatus.logPath,};}),- uninstall: Effect.succeed(false),+ uninstall: Effect.sync(() => {+ uninstalls.push(true);+ return serviceStatus.installed;+ }),});- return { service, installOptions, restarts };+ return { service, installOptions, restarts, uninstalls };}it.layer(Layer.mergeAll(NodeServices.layer, NetService.layer))("service commands", (it) => {@@ -194,4 +198,48 @@ it.layer(Layer.mergeAll(NodeServices.layer, NetService.layer))("service commandsexpect(installOptions).toEqual([]);}),);++ it.effect.each([+ { label: "this home's service", otherHome: false, unreadable: false, removed: [true] },+ { label: "another home's service", otherHome: true, unreadable: false, removed: [] },+ { label: "an unreadable service", otherHome: false, unreadable: true, removed: [true] },+ ])("uninstall removes only $label", (input) =>+ Effect.gen(function* () {+ const fs = yield* FileSystem.FileSystem;+ const baseDir = yield* fs.makeTempDirectoryScoped({ prefix: "t3-service-cli-test-" });+ const { service, uninstalls } = makeTestService({+ ...status,+ installedBaseDir: input.otherHome ? "/home/me/.t3" : baseDir,+ });+ vi.spyOn(BootService, "layer").mockReturnValue(+ Layer.succeed(+ BootService.BootService,+ input.unreadable+ ? {+ ...service,+ status: Effect.fail(new BootService.BootServiceInstallError({ cause: "EACCES" })),+ }+ : service,+ ),+ );++ const output = yield* Command.runWith(serviceCommand, { version: packageJson.version })([+ "uninstall",+ "--base-dir",+ baseDir,+ ]).pipe(+ Effect.andThen(TestConsole.logLines),+ Effect.map((lines) => lines.join("\n")),+ Effect.provideService(HostProcess.Environment, {}),+ Effect.provide(+ Layer.mergeAll(+ ConfigProvider.layer(ConfigProvider.fromEnv({ env: {} })),+ Layer.fresh(TestConsole.layer),+ ),+ ),+ );+ expect(uninstalls).toEqual(input.removed);+ expect(output).toContain(input.otherHome ? "another T3 home" : "Removed");+ }),+ );});diff --git a/apps/server/src/cli/service.ts b/apps/server/src/cli/service.tsindex b78ee31c9fa..4ba17aa17c6 100644--- a/apps/server/src/cli/service.ts+++ b/apps/server/src/cli/service.ts@@ -278,6 +278,22 @@ const serviceUninstallCommand = Command.make("uninstall", projectLocationFlags).flags,Effect.gen(function* () {const service = yield* BootService.BootService;+ const path = yield* Path.Path;+ const { baseDir } = yield* ServerConfig.ServerConfig;+ // An unreadable unit stays removable; only a unit known to serve another home is kept.+ const installedBaseDir = yield* service.status.pipe(+ Effect.map((status) => status.installedBaseDir),+ Effect.orElseSucceed(() => undefined),+ );+ if (+ installedBaseDir !== undefined &&+ path.resolve(installedBaseDir) !== path.resolve(baseDir)+ ) {+ yield* Console.log(+ "The installed service runs another T3 home, so it was not removed. Pass its --base-dir.",+ );+ return;+ }const removed = yield* service.uninstall;yield* Console.log(removed ? "Removed the T3 Code service." : "T3 Code service is not installed.",