Skip to content

Commit f4df41a

Browse files
Add dev runner for deterministic multi-instance port offsets
- Route `dev*` scripts through `scripts/dev-runner.mjs` to compute shared server/web port offsets - Support `T3CODE_DEV_INSTANCE` (hashed or numeric) and explicit `T3CODE_PORT_OFFSET` - Document multi-instance usage and allow server `VITE_DEV_SERVER_URL` override
1 parent 48b1f7c commit f4df41a

5 files changed

Lines changed: 143 additions & 5 deletions

File tree

‎README.md‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@ bun run dev
4949
# Desktop development
5050
bun run dev:desktop
5151

52+
# Desktop development on an isolated port set
53+
T3CODE_DEV_INSTANCE=feature-xyz bun run dev:desktop
54+
5255
# Production
5356
bun run build
5457
bun run start
@@ -67,6 +70,16 @@ npx t3
6770
- `bun run typecheck` — Strict TypeScript checks for all packages.
6871
- `bun run test` — Runs workspace tests.
6972

73+
### Running multiple dev instances
74+
75+
Set `T3CODE_DEV_INSTANCE` to any value to deterministically shift all dev ports together.
76+
77+
- Default ports: server `3773`, web `5173`
78+
- Shifted ports: `base + offset` (offset is hashed from `T3CODE_DEV_INSTANCE`)
79+
- Example: `T3CODE_DEV_INSTANCE=branch-a bun run dev:desktop`
80+
81+
If you want full control instead of hashing, set `T3CODE_PORT_OFFSET` to a numeric offset.
82+
7083
## Runtime modes
7184

7285
T3 Code has a global runtime mode switch in the chat toolbar:

‎apps/server/package.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
"type": "module",
1111
"main": "./dist/index.mjs",
1212
"scripts": {
13-
"dev": "VITE_DEV_SERVER_URL=http://localhost:5173 bun run src/index.ts",
13+
"dev": "VITE_DEV_SERVER_URL=${VITE_DEV_SERVER_URL:-http://localhost:5173} bun run src/index.ts",
1414
"build": "tsdown && node scripts/bundle-client.mjs",
1515
"start": "node dist/index.mjs",
1616
"typecheck": "tsc --noEmit",

‎package.json‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@
66
"packages/*"
77
],
88
"scripts": {
9-
"dev": "T3CODE_LOG_WS_EVENTS=1 VITE_WS_URL=ws://localhost:3773 turbo run dev --ui=tui --filter=@t3tools/contracts --filter=@t3tools/web --filter=t3 --parallel",
10-
"dev:server": "turbo run dev --filter=t3",
11-
"dev:web": "VITE_WS_URL=ws://localhost:3773 turbo run dev --filter=@t3tools/web",
12-
"dev:desktop": "turbo run dev --filter=@t3tools/desktop --filter=@t3tools/web --parallel",
9+
"dev": "bun run scripts/dev-runner.mjs dev",
10+
"dev:server": "bun run scripts/dev-runner.mjs dev:server",
11+
"dev:web": "bun run scripts/dev-runner.mjs dev:web",
12+
"dev:desktop": "bun run scripts/dev-runner.mjs dev:desktop",
1313
"start": "turbo run start --filter=t3",
1414
"start:desktop": "turbo run start --filter=@t3tools/desktop",
1515
"build": "turbo run build",

‎scripts/dev-runner.mjs‎

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
import { spawn } from "node:child_process";
2+
3+
const BASE_SERVER_PORT = 3773;
4+
const BASE_WEB_PORT = 5173;
5+
const MAX_HASH_OFFSET = 3000;
6+
const MODE_ARGS = {
7+
dev: [
8+
"run",
9+
"dev",
10+
"--ui=tui",
11+
"--filter=@t3tools/contracts",
12+
"--filter=@t3tools/web",
13+
"--filter=t3",
14+
"--parallel",
15+
],
16+
"dev:server": ["run", "dev", "--filter=t3"],
17+
"dev:web": ["run", "dev", "--filter=@t3tools/web"],
18+
"dev:desktop": ["run", "dev", "--filter=@t3tools/desktop", "--filter=@t3tools/web", "--parallel"],
19+
};
20+
21+
function parseInteger(value, envName) {
22+
const parsed = Number(value);
23+
if (!Number.isInteger(parsed)) {
24+
throw new Error(`Invalid ${envName}: ${value}`);
25+
}
26+
return parsed;
27+
}
28+
29+
function hashSeed(seed) {
30+
let hash = 0x811c9dc5;
31+
for (let i = 0; i < seed.length; i += 1) {
32+
hash ^= seed.charCodeAt(i);
33+
hash = Math.imul(hash, 0x01000193);
34+
}
35+
return hash >>> 0;
36+
}
37+
38+
function resolveOffset() {
39+
const explicitOffset = process.env.T3CODE_PORT_OFFSET?.trim();
40+
if (explicitOffset) {
41+
const parsed = parseInteger(explicitOffset, "T3CODE_PORT_OFFSET");
42+
if (parsed < 0) {
43+
throw new Error(`Invalid T3CODE_PORT_OFFSET: ${explicitOffset}`);
44+
}
45+
return { offset: parsed, source: `T3CODE_PORT_OFFSET=${explicitOffset}` };
46+
}
47+
48+
const seed = process.env.T3CODE_DEV_INSTANCE?.trim();
49+
if (!seed) {
50+
return { offset: 0, source: "default ports" };
51+
}
52+
53+
if (/^\d+$/.test(seed)) {
54+
return { offset: Number(seed), source: `numeric T3CODE_DEV_INSTANCE=${seed}` };
55+
}
56+
57+
const offset = (hashSeed(seed) % MAX_HASH_OFFSET) + 1;
58+
return { offset, source: `hashed T3CODE_DEV_INSTANCE=${seed}` };
59+
}
60+
61+
function main() {
62+
const mode = process.argv[2];
63+
const isDryRun = process.argv.includes("--dry-run");
64+
if (!mode || !(mode in MODE_ARGS)) {
65+
const supportedModes = Object.keys(MODE_ARGS).join(", ");
66+
throw new Error(`Usage: bun scripts/dev-runner.mjs <mode>. Supported modes: ${supportedModes}`);
67+
}
68+
69+
const { offset, source } = resolveOffset();
70+
const serverPort = BASE_SERVER_PORT + offset;
71+
const webPort = BASE_WEB_PORT + offset;
72+
73+
if (serverPort > 65535 || webPort > 65535) {
74+
throw new Error(
75+
`Port offset too large (${offset}). Computed ports: server=${serverPort}, web=${webPort}`,
76+
);
77+
}
78+
79+
const env = {
80+
...process.env,
81+
T3CODE_PORT: String(serverPort),
82+
PORT: String(webPort),
83+
ELECTRON_RENDERER_PORT: String(webPort),
84+
VITE_WS_URL: `ws://localhost:${serverPort}`,
85+
VITE_DEV_SERVER_URL: `http://localhost:${webPort}`,
86+
};
87+
if (mode === "dev" && !env.T3CODE_LOG_WS_EVENTS) {
88+
env.T3CODE_LOG_WS_EVENTS = "1";
89+
}
90+
91+
console.info(
92+
`[dev-runner] mode=${mode} source=${source} serverPort=${serverPort} webPort=${webPort}`,
93+
);
94+
95+
if (isDryRun) {
96+
return;
97+
}
98+
99+
const command = process.platform === "win32" ? "turbo.cmd" : "turbo";
100+
const child = spawn(command, MODE_ARGS[mode], {
101+
stdio: "inherit",
102+
env,
103+
});
104+
105+
child.on("exit", (code, signal) => {
106+
if (signal) {
107+
process.kill(process.pid, signal);
108+
return;
109+
}
110+
process.exit(code ?? 0);
111+
});
112+
113+
child.on("error", (error) => {
114+
console.error("[dev-runner] failed to start turbo", error);
115+
process.exit(1);
116+
});
117+
}
118+
119+
try {
120+
main();
121+
} catch (error) {
122+
console.error("[dev-runner]", error);
123+
process.exit(1);
124+
}

‎turbo.json‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
"PORT",
55
"VITE_WS_URL",
66
"VITE_DEV_SERVER_URL",
7+
"ELECTRON_RENDERER_PORT",
78
"T3CODE_LOG_WS_EVENTS",
89
"T3CODE_MODE",
910
"T3CODE_PORT",

0 commit comments

Comments
 (0)