Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions apps/server/src/cli/pair.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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", () =>
Expand Down
23 changes: 16 additions & 7 deletions apps/server/src/cli/pair.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -61,12 +62,16 @@ export class NoRunningServerError extends Schema.TaggedError<NoRunningServerErro
"NoRunningServerError",
{
checkedStatePaths: Schema.Array(Schema.String),
inLinkedWorktree: Schema.Boolean,
},
) {
override get message(): string {
return [
"No running T3 Code server found.",
...this.checkedStatePaths.map((statePath) => ` 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");
}
Expand Down Expand Up @@ -228,18 +233,22 @@ const discoverPairTarget = Effect.fn("pair.discoverPairTarget")(function* (
explicitBaseDir: string | undefined,
) {
const bases: Array<string> = [];
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);
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
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<string> = [];
Expand Down Expand Up @@ -274,7 +283,7 @@ const discoverPairTarget = Effect.fn("pair.discoverPairTarget")(function* (
} satisfies DiscoveredPairTarget;
}
}
return yield* new NoRunningServerError({ checkedStatePaths });
return yield* new NoRunningServerError({ checkedStatePaths, inLinkedWorktree });
});

/**
Expand Down
Loading