Skip to content

Commit 4ecb6fc

Browse files
t3dotggclaude
andcommitted
fix(connect): match the suggested serve command to how connect was run
`npx t3@nightly connect` used to end with "Start the server with `t3 serve`" — a command that fails on machines without a global install. Detect the package runner (npx / pnpm dlx / bunx) from the CLI entry path and the nightly channel from the running version, and render the suggestion to match: `npx t3@nightly serve`, `bunx t3 serve`, or plain `t3 serve` for stable installs. Detection fails closed to `t3 serve`. Co-Authored-By: Claude Fable 5 <[email protected]>
1 parent a8e05cb commit 4ecb6fc

3 files changed

Lines changed: 146 additions & 4 deletions

File tree

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

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ import * as ServerEnvironment from "../environment/ServerEnvironment.ts";
4646
import * as ExternalLauncher from "../process/externalLauncher.ts";
4747
import { readPersistedServerRuntimeState } from "../serverRuntimeState.ts";
4848
import { projectLocationFlags, resolveCliAuthConfig } from "./config.ts";
49+
import { resolveCliCommand } from "./invocation.ts";
4950
import {
5051
bootServiceLayer,
5152
offerServiceDuringOnboarding,
@@ -526,10 +527,11 @@ const connectLinkCommand = Command.make("link", {
526527
yield* Console.log("T3 Connect\n");
527528
const linked = yield* linkEnvironmentForConnect(flags);
528529
if (linked) {
530+
const serveCommand = yield* resolveCliCommand("serve");
529531
yield* Console.log(
530532
flags.publishOnly
531533
? `✓ Authorized${connectedAs(linked.identity)}\n\nNext\n Start T3 to publish agent activity (no managed tunnel).`
532-
: `✓ Authorized${connectedAs(linked.identity)}\n\nNext\n Start the server with \`t3 serve\` to make this machine reachable.`,
534+
: `✓ Authorized${connectedAs(linked.identity)}\n\nNext\n Start the server with \`${serveCommand}\` to make this machine reachable.`,
533535
);
534536
}
535537
}),
@@ -691,10 +693,15 @@ export const connectCommand = Command.make("connect", {
691693
// Connect itself already succeeded; a boot-service failure must not
692694
// fail the command, just tell the user what happened and move on.
693695
const background = yield* recoverServiceOnboardingOffer(offerServiceDuringOnboarding);
696+
if (background) {
697+
yield* Console.log(
698+
"\n✓ Background service ready\n\nT3 Code will stay reachable after you log out.",
699+
);
700+
return;
701+
}
702+
const serveCommand = yield* resolveCliCommand("serve");
694703
yield* Console.log(
695-
background
696-
? "\n✓ Background service ready\n\nT3 Code will stay reachable after you log out."
697-
: "\nNext\n Start the server with `t3 serve` to make this machine reachable.",
704+
`\nNext\n Start the server with \`${serveCommand}\` to make this machine reachable.`,
698705
);
699706
}),
700707
),
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import { assert, it } from "@effect/vitest";
2+
3+
import { detectCliRunner, formatCliCommand, suggestedPackageSpec } from "./invocation.ts";
4+
5+
it("detects package runners from their cache entry paths", () => {
6+
assert.equal(detectCliRunner("/home/theo/.npm/_npx/abc123/node_modules/t3/dist/bin.mjs"), "npx");
7+
assert.equal(
8+
detectCliRunner(
9+
"C:\\Users\\theo\\AppData\\Local\\npm-cache\\_npx\\abc\\node_modules\\t3\\dist\\bin.mjs",
10+
),
11+
"npx",
12+
);
13+
assert.equal(
14+
detectCliRunner("/home/theo/.cache/pnpm/dlx/abc/node_modules/t3/dist/bin.mjs"),
15+
"pnpm dlx",
16+
);
17+
assert.equal(
18+
detectCliRunner("/home/theo/.local/share/pnpm/.pnpm/dlx/abc/node_modules/t3/dist/bin.mjs"),
19+
"pnpm dlx",
20+
);
21+
assert.equal(detectCliRunner("/home/theo/.bun/install/cache/[email protected]/dist/bin.mjs"), "bunx");
22+
assert.equal(detectCliRunner("/tmp/bunx-1000-t3@latest/node_modules/t3/dist/bin.mjs"), "bunx");
23+
});
24+
25+
it("treats stable installs as direct invocations", () => {
26+
assert.isNull(detectCliRunner("/usr/local/lib/node_modules/t3/dist/bin.mjs"));
27+
assert.isNull(detectCliRunner("/home/theo/Code/work/t3code/apps/server/dist/bin.mjs"));
28+
assert.isNull(detectCliRunner("/home/theo/.t3/runtime/0.0.31/node_modules/t3/dist/bin.mjs"));
29+
assert.isNull(detectCliRunner(""));
30+
});
31+
32+
it("re-suggests the nightly channel only for nightly builds", () => {
33+
assert.equal(suggestedPackageSpec("0.0.31-nightly.20260729"), "t3@nightly");
34+
assert.equal(suggestedPackageSpec("0.0.31"), "t3");
35+
});
36+
37+
it("formats serve suggestions to match the launching command", () => {
38+
assert.equal(
39+
formatCliCommand({
40+
subcommand: "serve",
41+
entryPath: "/home/theo/.npm/_npx/abc/node_modules/t3/dist/bin.mjs",
42+
version: "0.0.31-nightly.20260729",
43+
}),
44+
"npx t3@nightly serve",
45+
);
46+
assert.equal(
47+
formatCliCommand({
48+
subcommand: "serve",
49+
entryPath: "/tmp/bunx-1000-t3@latest/node_modules/t3/dist/bin.mjs",
50+
version: "0.0.31",
51+
}),
52+
"bunx t3 serve",
53+
);
54+
assert.equal(
55+
formatCliCommand({
56+
subcommand: "serve",
57+
entryPath: "/usr/local/lib/node_modules/t3/dist/bin.mjs",
58+
version: "0.0.31-nightly.20260729",
59+
}),
60+
"t3 serve",
61+
);
62+
});

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

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
import * as Effect from "effect/Effect";
2+
3+
import { HostProcessArguments } from "@t3tools/shared/hostProcess";
4+
5+
import packageJson from "../../package.json" with { type: "json" };
6+
7+
export type CliRunner = "npx" | "pnpm dlx" | "bunx";
8+
9+
/**
10+
* How the CLI was launched, judged by where its entry script lives. Each
11+
* package runner executes out of a distinctive cache/temp layout:
12+
*
13+
* npx ~/.npm/_npx/<hash>/node_modules/...
14+
* pnpm dlx ~/.cache/pnpm/dlx/... or $PNPM_HOME/.pnpm/dlx/...
15+
* bunx ~/.bun/install/cache/... or $TMPDIR/bunx-<uid>-<spec>/...
16+
*
17+
* Global installs and repo checkouts match none of these and return null.
18+
* Detection is best-effort; callers must fail closed to a plain `t3` command.
19+
*/
20+
export function detectCliRunner(entryPath: string): CliRunner | null {
21+
if (entryPath.includes("/_npx/") || entryPath.includes("\\_npx\\")) {
22+
return "npx";
23+
}
24+
if (entryPath.includes("/pnpm/dlx/") || entryPath.includes("/.pnpm/dlx/")) {
25+
return "pnpm dlx";
26+
}
27+
if (
28+
entryPath.includes("/.bun/install/cache/") ||
29+
entryPath.includes("/bunx-") ||
30+
entryPath.includes("\\bunx-")
31+
) {
32+
return "bunx";
33+
}
34+
return null;
35+
}
36+
37+
/**
38+
* The `t3` package spec to suggest. The literal spec the user typed (e.g.
39+
* `t3@nightly`) is resolved away before our process starts, so re-derive it
40+
* from the running version: nightly builds re-suggest the nightly channel,
41+
* anything else suggests the bare package.
42+
*/
43+
export function suggestedPackageSpec(version: string): string {
44+
return version.includes("-nightly.") ? "t3@nightly" : "t3";
45+
}
46+
47+
/**
48+
* Render a `t3 <subcommand>` suggestion that matches how this process was
49+
* launched, so copy/pasting it actually works: `npx t3 connect` suggests
50+
* `npx t3 serve`, a global install suggests `t3 serve`, and a nightly build
51+
* keeps the `@nightly` tag.
52+
*/
53+
export function formatCliCommand(input: {
54+
readonly subcommand: string;
55+
readonly entryPath: string;
56+
readonly version: string;
57+
}): string {
58+
const runner = detectCliRunner(input.entryPath);
59+
if (runner === null) {
60+
return `t3 ${input.subcommand}`;
61+
}
62+
return `${runner} ${suggestedPackageSpec(input.version)} ${input.subcommand}`;
63+
}
64+
65+
/** `formatCliCommand` against this process's real entry path and version. */
66+
export const resolveCliCommand = (subcommand: string) =>
67+
Effect.map(HostProcessArguments, (processArguments) =>
68+
formatCliCommand({
69+
subcommand,
70+
entryPath: processArguments[1] ?? "",
71+
version: packageJson.version,
72+
}),
73+
);

0 commit comments

Comments
 (0)