From 3c38caab6302b0955236f283df366d6ea718f614 Mon Sep 17 00:00:00 2001 From: Theo Browne Date: Wed, 21 Oct 2026 04:57:49 -0700 Subject: [PATCH] fix(server): t3 pair in a worktree no longer falls back to the live install Inside a linked worktree, pair discovery only checks that worktree's .t3. Minting opens the target database with this checkout's migrations, so the old fallback to the shared home could migrate the installed app's database with unreleased schema. Explicit --base-dir still works. Closes #10516 Co-Authored-By: Claude Opus 5.5 --- apps/server/src/cli/pair.test.ts | 41 ++++++++++++++++++++++++++++++++ apps/server/src/cli/pair.ts | 23 ++++++++++++------ 2 files changed, 57 insertions(+), 7 deletions(-) diff --git a/apps/server/src/cli/pair.test.ts b/apps/server/src/cli/pair.test.ts index 1eeeee4aaba2..1dd8c2a9597f 100644 --- a/apps/server/src/cli/pair.test.ts +++ b/apps/server/src/cli/pair.test.ts @@ -9,6 +9,7 @@ import { AuthStandardClientScopes } from "@t3tools/contracts"; import * as NetService from "@t3tools/shared/Net"; import * as HostProcess from "@t3tools/shared/HostProcess"; import { assert, describe, expect, it } from "@effect/vitest"; +import * as ConfigProvider from "effect/ConfigProvider"; import * as Effect from "effect/Effect"; import * as Layer from "effect/Layer"; import * as Sink from "effect/Sink"; @@ -287,6 +288,46 @@ describe("t3 pair", () => { ).pipe(Effect.provide(NodeServices.layer)), ); + // regression: #10516 minting opens the target database with this checkout's + // migrations, so a worktree must never fall back to the live install. + it.effect("does not fall back to the shared home from a linked worktree", () => + withDescriptorServer((origin) => + Effect.gen(function* () { + const root = NodeFS.mkdtempSync(NodePath.join(NodeOS.tmpdir(), "t3-pair-worktree-test-")); + const worktree = NodePath.join(root, "worktree"); + NodeFS.mkdirSync(worktree); + NodeFS.writeFileSync( + NodePath.join(worktree, ".git"), + `gitdir: ${NodePath.join(root, "repo", ".git", "worktrees", "worktree")}\n`, + ); + const sharedHome = NodePath.join(root, "shared"); + yield* persistServerRuntimeState({ + path: NodePath.join(sharedHome, "userdata", "server-runtime.json"), + state: yield* makePersistedServerRuntimeState({ + config: { host: "127.0.0.1", devUrl: undefined }, + port: Number(new URL(origin).port), + }), + }); + + const error = yield* provideCliTestLayers( + runCli(["pair"]).pipe( + Effect.provideService(HostProcess.WorkingDirectory, worktree), + Effect.provide( + ConfigProvider.layer(ConfigProvider.fromEnv({ env: { T3CODE_HOME: sharedHome } })), + ), + Effect.flip, + ), + ); + + assert.include(String(error), "No running T3 Code server found."); + assert.notInclude(String(error), sharedHome); + assert.deepEqual(NodeFS.readdirSync(NodePath.join(sharedHome, "userdata")), [ + "server-runtime.json", + ]); + }), + ).pipe(Effect.provide(NodeServices.layer)), + ); + // Without the capability `tailscale serve` blocks on an enablement URL, so // pairing must stop at status instead of spawning it. it.effect("explains a tailnet without HTTPS serve instead of running tailscale serve", () => diff --git a/apps/server/src/cli/pair.ts b/apps/server/src/cli/pair.ts index 4522258c2712..78aecbf6c52c 100644 --- a/apps/server/src/cli/pair.ts +++ b/apps/server/src/cli/pair.ts @@ -5,6 +5,7 @@ import { PortSchema, } from "@t3tools/contracts"; import { resolveWorktreeT3Home } from "@t3tools/shared/devHome"; +import * as HostProcess from "@t3tools/shared/HostProcess"; import { DEFAULT_SIGNAL_EXPORT } from "@t3tools/shared/observability"; import * as OtelEnvironment from "@t3tools/shared/otelEnvironment"; import { @@ -61,12 +62,16 @@ export class NoRunningServerError extends Schema.TaggedError ` checked ${statePath}`), + ...(this.inLinkedWorktree + ? ["In a linked worktree, pass --base-dir to pair with a server outside it."] + : []), "Start one with `npx t3 serve`, or connect this machine with T3 Connect: `npx t3 connect`.", ].join("\n"); } @@ -228,18 +233,22 @@ const discoverPairTarget = Effect.fn("pair.discoverPairTarget")(function* ( explicitBaseDir: string | undefined, ) { const bases: Array = []; + let inLinkedWorktree = false; if (explicitBaseDir !== undefined && explicitBaseDir.trim().length > 0) { bases.push(yield* resolveBaseDir(explicitBaseDir)); } else { - // Same precedence as dev-runner: inside a linked worktree its own `.t3` - // outranks the shared home, so `t3 pair` in a worktree pairs with the dev - // server under test rather than the daily-driver install. - const worktreeHome = yield* resolveWorktreeT3Home(process.cwd()); + // Same precedence as dev-runner: inside a linked worktree only its own + // `.t3` counts. Minting opens the target database with this checkout's + // migrations, so falling back to the shared home could migrate the live + // install with unreleased schema. + const worktreeHome = yield* resolveWorktreeT3Home(yield* HostProcess.WorkingDirectory); if (worktreeHome !== undefined) { bases.push(worktreeHome); + inLinkedWorktree = true; + } else { + const envHome = yield* Config.String("T3CODE_HOME").pipe(Config.option); + bases.push(yield* resolveBaseDir(Option.getOrUndefined(envHome))); } - const envHome = yield* Config.String("T3CODE_HOME").pipe(Config.option); - bases.push(yield* resolveBaseDir(Option.getOrUndefined(envHome))); } const checkedStatePaths: Array = []; @@ -274,7 +283,7 @@ const discoverPairTarget = Effect.fn("pair.discoverPairTarget")(function* ( } satisfies DiscoveredPairTarget; } } - return yield* new NoRunningServerError({ checkedStatePaths }); + return yield* new NoRunningServerError({ checkedStatePaths, inLinkedWorktree }); }); /**