diff --git a/apps/server/src/cloud/bootService.test.ts b/apps/server/src/cloud/bootService.test.ts index 4711c95ec9..76c0eae4ab 100644 --- a/apps/server/src/cloud/bootService.test.ts +++ b/apps/server/src/cloud/bootService.test.ts @@ -475,6 +475,7 @@ it.layer(NodeServices.layer)("BootService", (it) => { [ `npm install --prefix ${runtimeDir} --no-fund --no-audit t3@0.0.27`, "/bin/launchctl print-disabled gui/501", + "/bin/launchctl print gui/501/com.t3tools.t3code.server", "/bin/launchctl enable gui/501/com.t3tools.t3code.server", "/bin/launchctl bootout gui/501/com.t3tools.t3code.server", `/bin/launchctl bootstrap gui/501 ${launchAgentPath}`, @@ -574,6 +575,54 @@ it.layer(NodeServices.layer)("BootService", (it) => { }), ); + it.effect("leaves a previously stopped macOS LaunchAgent stopped after rollback", () => + Effect.gen(function* () { + const { dirs, fs, path } = yield* makeTestContext(); + const initialService = yield* BootService.make({ + baseDir: dirs.baseDir, + logsDir: dirs.logsDir, + cliVersion: "0.0.27", + host: makeHost(dirs.stableEntry), + }).pipe(Effect.provide(makeRecordingRunnerLayer([])), provideHostRefs(dirs.home, "darwin")); + yield* initialService.install; + + const launchAgentPath = path.join( + dirs.home, + "Library", + "LaunchAgents", + "com.t3tools.t3code.server.plist", + ); + const previousLaunchAgent = yield* fs.readFileString(launchAgentPath); + const commands: Array = []; + const repairService = yield* BootService.make({ + baseDir: dirs.baseDir, + logsDir: dirs.logsDir, + cliVersion: "0.0.28", + host: makeHost(dirs.stableEntry), + }).pipe( + Effect.provide( + makeRecordingRunnerLayer(commands, { + failWhen: (command, args) => + command === "/bin/launchctl" && + (args.includes("print") || args.includes("bootstrap")), + }), + ), + provideHostRefs(dirs.home, "darwin"), + ); + + const error = yield* repairService.install.pipe(Effect.flip); + + assert.isTrue(isCommandError(error)); + assert.equal(yield* fs.readFileString(launchAgentPath), previousLaunchAgent); + assert.lengthOf( + commands.filter( + ({ command, args }) => command === "/bin/launchctl" && args.includes("bootstrap"), + ), + 1, + ); + }), + ); + it.effect("stops an orphaned macOS LaunchAgent when its plist is already gone", () => Effect.gen(function* () { const { dirs } = yield* makeTestContext(); diff --git a/apps/server/src/cloud/bootService.ts b/apps/server/src/cloud/bootService.ts index d6e9ddf5d5..40a80c08c2 100644 --- a/apps/server/src/cloud/bootService.ts +++ b/apps/server/src/cloud/bootService.ts @@ -550,6 +550,7 @@ export const make = Effect.fn("cloud.boot_service.make")(function* (input: { : Option.none(); const previousLaunchAgentDisabled = platform === "darwin" ? yield* readLaunchAgentDisabled() : Option.none(); + const previousLaunchAgentLoaded = platform === "darwin" ? yield* isLaunchAgentLoaded() : false; yield* fs.makeDirectory(unitDir, { recursive: true }).pipe( Effect.andThen( @@ -561,7 +562,12 @@ export const make = Effect.fn("cloud.boot_service.make")(function* (input: { ), Effect.mapError((cause) => new BootServiceInstallError({ cause })), Effect.tapError(() => - rollbackFailedInstall(previousUnit, previousRuntimeLink, previousLaunchAgentDisabled), + rollbackFailedInstall( + previousUnit, + previousRuntimeLink, + previousLaunchAgentDisabled, + previousLaunchAgentLoaded, + ), ), ); @@ -603,7 +609,12 @@ export const make = Effect.fn("cloud.boot_service.make")(function* (input: { yield* runStep("enabling lingering for this user", "loginctl", ["enable-linger"]); }).pipe( Effect.tapError(() => - rollbackFailedInstall(previousUnit, previousRuntimeLink, previousLaunchAgentDisabled), + rollbackFailedInstall( + previousUnit, + previousRuntimeLink, + previousLaunchAgentDisabled, + previousLaunchAgentLoaded, + ), ), ); @@ -619,6 +630,7 @@ export const make = Effect.fn("cloud.boot_service.make")(function* (input: { previousUnit: Option.Option, previousRuntimeLink: Option.Option, previousLaunchAgentDisabled: Option.Option, + previousLaunchAgentLoaded: boolean, ) { if (platform === "darwin" && launchAgent !== null && userId !== null) { yield* runOptionalStep("/bin/launchctl", ["bootout", launchAgent.serviceTarget]); @@ -631,7 +643,7 @@ export const make = Effect.fn("cloud.boot_service.make")(function* (input: { yield* writeUnit(previousUnit.value).pipe(Effect.ignore); if (Option.getOrElse(previousLaunchAgentDisabled, () => false)) { yield* runOptionalStep("/bin/launchctl", ["disable", launchAgent.serviceTarget]); - } else { + } else if (previousLaunchAgentLoaded) { yield* runOptionalStep("/bin/launchctl", [ "bootstrap", `gui/${String(userId)}`,