TypeScript
fix(cli): t3 update no longer says a service awaiting restart runs the new version (#29901)
Alex Southwell2b37449c8555
diff --git a/apps/server/src/cli/update.test.ts b/apps/server/src/cli/update.test.tsindex ecd837e37ac..7540c678f59 100644--- a/apps/server/src/cli/update.test.ts+++ b/apps/server/src/cli/update.test.ts@@ -10,12 +10,14 @@ import * as Layer from "effect/Layer";import * as Option from "effect/Option";import * as Path from "effect/Path";import * as TestClock from "effect/testing/TestClock";+import * as TestConsole from "effect/testing/TestConsole";import { HttpClient, HttpClientResponse } from "effect/http";import * as ChildProcessSpawner from "effect/process/ChildProcessSpawner";import { afterEach, vi } from "vite-plus/test";+import packageJson from "../../package.json" with { type: "json" };import * as BootService from "../cloud/bootService.ts";-import { prunePinnedRuntimes } from "../cloud/pinnedRuntime.ts";+import { pinnedRuntimePaths, prunePinnedRuntimes } from "../cloud/pinnedRuntime.ts";import * as ManagedPolicy from "../policy/ManagedPolicy.ts";import * as ProcessRunner from "../processRunner.ts";import {@@ -434,3 +436,96 @@ it.layer(NodeServices.layer)("t3 update launcher", (it) => {}).pipe(Effect.scoped, Effect.provideService(HostProcess.Platform, "linux")),);});++// Without terminals, update skips its restart prompt whatever runs the test.+const withoutTerminals = Effect.acquireRelease(+ Effect.sync(() =>+ [process.stdin, process.stdout].map((stream) => {+ const descriptor = Object.getOwnPropertyDescriptor(stream, "isTTY");+ Object.defineProperty(stream, "isTTY", { configurable: true, value: false });+ return { stream, descriptor };+ }),+ ),+ (saved) =>+ Effect.sync(() => {+ for (const { stream, descriptor } of saved) {+ if (descriptor === undefined) Reflect.deleteProperty(stream, "isTTY");+ else Object.defineProperty(stream, "isTTY", descriptor);+ }+ }),+);++it.layer(NodeServices.layer)("t3 update with a background service", (it) => {+ it.effect("names the version a service with a pending restart still runs", () =>+ Effect.gen(function* () {+ yield* withoutTerminals;+ const fs = yield* FileSystem.FileSystem;+ const path = yield* Path.Path;+ const baseDir = yield* fs.makeTempDirectoryScoped({ prefix: "t3-update-" });+ const version = packageJson.version;+ const runtime = pinnedRuntimePaths(path, baseDir, version, "linux");+ yield* fs.makeDirectory(runtime.versionDir, { recursive: true });+ yield* fs.writeFileString(runtime.entryPath, "");+ yield* fs.writeFileString(runtime.sentinelPath, `${version}\n`);+ const unitPath = path.join(baseDir, "t3code.service");+ const logPath = path.join(baseDir, "boot-service.log");+ // An earlier `t3 update` pointed the service at this version without restarting it.+ const service = Layer.mock(BootService.BootService, {+ status: Effect.succeed<BootService.BootServiceStatus>({+ supported: true,+ installed: true,+ current: false,+ installedVersion: version,+ installedBaseDir: baseDir,+ problems: ["restart-pending"],+ unitPath,+ logPath,+ }),+ install: () => Effect.succeed({ program: [runtime.entryPath], baseDir, unitPath, logPath }),+ });+ vi.spyOn(BootService, "layer").mockReturnValue(service);+ const runner = ProcessRunner.ProcessRunner.of({+ run: (input) =>+ input.args?.includes("--version") === true+ ? unrelatedLsof+ .run(input)+ .pipe(Effect.map((result) => ({ ...result, stdout: `t3 v${version}\n` })))+ : unrelatedLsof.run(input),+ });++ const output = yield* runUpdate({+ baseDir,+ logsDir: baseDir,+ serverRuntimeStatePath: path.join(baseDir, "server-runtime.json"),+ channel: undefined,+ requestedVersion: version,+ allowDowngrade: false,+ assumeYes: false,+ }).pipe(+ Effect.andThen(TestConsole.logLines),+ Effect.map((lines) => lines.join("\n")),+ Effect.provide(+ Layer.mergeAll(+ service,+ Layer.succeed(ProcessRunner.ProcessRunner, runner),+ ManagedPolicy.layerTest(),+ Layer.succeed(+ HttpClient.HttpClient,+ HttpClient.make(() => Effect.die("unexpected release lookup")),+ ),+ Layer.fresh(TestConsole.layer),+ ),+ ),+ );++ assert.include(+ output,+ `Background service still running the version before ${version}. Run \`t3 service restart\` when you are ready to switch it to ${version}.`,+ );+ }).pipe(+ Effect.scoped,+ Effect.provideService(HostProcess.Platform, "linux"),+ Effect.provideService(HostProcess.Environment, { PATH: "" }),+ ),+ );+});diff --git a/apps/server/src/cli/update.ts b/apps/server/src/cli/update.tsindex be36b1184d8..99bf380f1c0 100644--- a/apps/server/src/cli/update.ts+++ b/apps/server/src/cli/update.ts@@ -603,8 +603,15 @@ export const runUpdate = Effect.fn("cli.update.run")(function* (input: {} else if (serviceInstalled && serviceCurrent) {yield* Console.log(` Background service already on ${targetVersion}`);} else if (serviceInstalled) {+ // A pending restart means the service already points at serviceVersion but runs an older one.+ const running =+ serviceVersion === undefined+ ? "an unknown version"+ : restartPending+ ? `the version before ${serviceVersion}`+ : serviceVersion;yield* Console.log(- ` Background service still running ${serviceVersion ?? "an unknown version"}. Run \`t3 service restart\` when you are ready to switch it to ${targetVersion}.`,+ ` Background service still running ${running}. Run \`t3 service restart\` when you are ready to switch it to ${targetVersion}.`,);} else if (status.installed && !servesThisHome) {yield* Console.log(