Skip to content

Commit 448b8a9

Browse files
t3dotggclaude
andauthored
fix(server): t3 pair in a worktree no longer falls back to the live install (#22855)
Co-authored-by: Claude Opus 5.5 <[email protected]>
1 parent 066c8fc commit 448b8a9

2 files changed

Lines changed: 57 additions & 7 deletions

File tree

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

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { AuthStandardClientScopes } from "@t3tools/contracts";
99
import * as NetService from "@t3tools/shared/Net";
1010
import * as HostProcess from "@t3tools/shared/HostProcess";
1111
import { assert, describe, expect, it } from "@effect/vitest";
12+
import * as ConfigProvider from "effect/ConfigProvider";
1213
import * as Effect from "effect/Effect";
1314
import * as Layer from "effect/Layer";
1415
import * as Sink from "effect/Sink";
@@ -287,6 +288,46 @@ describe("t3 pair", () => {
287288
).pipe(Effect.provide(NodeServices.layer)),
288289
);
289290

291+
// regression: #10516 minting opens the target database with this checkout's
292+
// migrations, so a worktree must never fall back to the live install.
293+
it.effect("does not fall back to the shared home from a linked worktree", () =>
294+
withDescriptorServer((origin) =>
295+
Effect.gen(function* () {
296+
const root = NodeFS.mkdtempSync(NodePath.join(NodeOS.tmpdir(), "t3-pair-worktree-test-"));
297+
const worktree = NodePath.join(root, "worktree");
298+
NodeFS.mkdirSync(worktree);
299+
NodeFS.writeFileSync(
300+
NodePath.join(worktree, ".git"),
301+
`gitdir: ${NodePath.join(root, "repo", ".git", "worktrees", "worktree")}\n`,
302+
);
303+
const sharedHome = NodePath.join(root, "shared");
304+
yield* persistServerRuntimeState({
305+
path: NodePath.join(sharedHome, "userdata", "server-runtime.json"),
306+
state: yield* makePersistedServerRuntimeState({
307+
config: { host: "127.0.0.1", devUrl: undefined },
308+
port: Number(new URL(origin).port),
309+
}),
310+
});
311+
312+
const error = yield* provideCliTestLayers(
313+
runCli(["pair"]).pipe(
314+
Effect.provideService(HostProcess.WorkingDirectory, worktree),
315+
Effect.provide(
316+
ConfigProvider.layer(ConfigProvider.fromEnv({ env: { T3CODE_HOME: sharedHome } })),
317+
),
318+
Effect.flip,
319+
),
320+
);
321+
322+
assert.include(String(error), "No running T3 Code server found.");
323+
assert.notInclude(String(error), sharedHome);
324+
assert.deepEqual(NodeFS.readdirSync(NodePath.join(sharedHome, "userdata")), [
325+
"server-runtime.json",
326+
]);
327+
}),
328+
).pipe(Effect.provide(NodeServices.layer)),
329+
);
330+
290331
// Without the capability `tailscale serve` blocks on an enablement URL, so
291332
// pairing must stop at status instead of spawning it.
292333
it.effect("explains a tailnet without HTTPS serve instead of running tailscale serve", () =>

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

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
PortSchema,
66
} from "@t3tools/contracts";
77
import { resolveWorktreeT3Home } from "@t3tools/shared/devHome";
8+
import * as HostProcess from "@t3tools/shared/HostProcess";
89
import { DEFAULT_SIGNAL_EXPORT } from "@t3tools/shared/observability";
910
import * as OtelEnvironment from "@t3tools/shared/otelEnvironment";
1011
import {
@@ -61,12 +62,16 @@ export class NoRunningServerError extends Schema.TaggedError<NoRunningServerErro
6162
"NoRunningServerError",
6263
{
6364
checkedStatePaths: Schema.Array(Schema.String),
65+
inLinkedWorktree: Schema.Boolean,
6466
},
6567
) {
6668
override get message(): string {
6769
return [
6870
"No running T3 Code server found.",
6971
...this.checkedStatePaths.map((statePath) => ` checked ${statePath}`),
72+
...(this.inLinkedWorktree
73+
? ["In a linked worktree, pass --base-dir to pair with a server outside it."]
74+
: []),
7075
"Start one with `npx t3 serve`, or connect this machine with T3 Connect: `npx t3 connect`.",
7176
].join("\n");
7277
}
@@ -228,18 +233,22 @@ const discoverPairTarget = Effect.fn("pair.discoverPairTarget")(function* (
228233
explicitBaseDir: string | undefined,
229234
) {
230235
const bases: Array<string> = [];
236+
let inLinkedWorktree = false;
231237
if (explicitBaseDir !== undefined && explicitBaseDir.trim().length > 0) {
232238
bases.push(yield* resolveBaseDir(explicitBaseDir));
233239
} else {
234-
// Same precedence as dev-runner: inside a linked worktree its own `.t3`
235-
// outranks the shared home, so `t3 pair` in a worktree pairs with the dev
236-
// server under test rather than the daily-driver install.
237-
const worktreeHome = yield* resolveWorktreeT3Home(process.cwd());
240+
// Same precedence as dev-runner: inside a linked worktree only its own
241+
// `.t3` counts. Minting opens the target database with this checkout's
242+
// migrations, so falling back to the shared home could migrate the live
243+
// install with unreleased schema.
244+
const worktreeHome = yield* resolveWorktreeT3Home(yield* HostProcess.WorkingDirectory);
238245
if (worktreeHome !== undefined) {
239246
bases.push(worktreeHome);
247+
inLinkedWorktree = true;
248+
} else {
249+
const envHome = yield* Config.String("T3CODE_HOME").pipe(Config.option);
250+
bases.push(yield* resolveBaseDir(Option.getOrUndefined(envHome)));
240251
}
241-
const envHome = yield* Config.String("T3CODE_HOME").pipe(Config.option);
242-
bases.push(yield* resolveBaseDir(Option.getOrUndefined(envHome)));
243252
}
244253

245254
const checkedStatePaths: Array<string> = [];
@@ -274,7 +283,7 @@ const discoverPairTarget = Effect.fn("pair.discoverPairTarget")(function* (
274283
} satisfies DiscoveredPairTarget;
275284
}
276285
}
277-
return yield* new NoRunningServerError({ checkedStatePaths });
286+
return yield* new NoRunningServerError({ checkedStatePaths, inLinkedWorktree });
278287
});
279288

280289
/**

0 commit comments

Comments
 (0)