Skip to content

Commit 37e1c79

Browse files
t3dotggclaude
andcommitted
Apply the bind host to the server port only
--host/T3CODE_HOST configures the backend. Vite takes its bind address from HOST, which the runner sets for desktop alone, so the web port stays on loopback. Probing it against the backend's interface could reject a port that was free where Vite would actually bind, shifting offsets or failing startup for no reason. The checker now takes the port's role. Passed explicitly rather than inferred from the port number, which stops discriminating once a large T3CODE_PORT_OFFSET pushes the web port past the server base. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
1 parent 2869a9e commit 37e1c79

2 files changed

Lines changed: 46 additions & 8 deletions

File tree

‎scripts/dev-runner.test.ts‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -570,6 +570,31 @@ it.layer(NodeServices.layer)("dev-runner", (it) => {
570570
assert.deepStrictEqual(devPortProbeHosts("127.0.0.1"), ["127.0.0.1", "::1"]);
571571
}),
572572
);
573+
574+
// Only the backend honours --host/T3CODE_HOST. Vite reads HOST (set for
575+
// desktop only), so judging the web port against the backend's interface
576+
// would reject ports for a server that never binds there.
577+
it.effect("passes the port role so only the server port sees the bind host", () =>
578+
Effect.gen(function* () {
579+
const probed: Array<{ port: number; role: string | undefined }> = [];
580+
581+
yield* resolveModePortOffsets({
582+
mode: "dev",
583+
startOffset: 0,
584+
hasExplicitServerPort: false,
585+
hasExplicitDevUrl: false,
586+
checkPortAvailability: (port, role) => {
587+
probed.push({ port, role });
588+
return Effect.succeed(true);
589+
},
590+
});
591+
592+
assert.deepStrictEqual(probed, [
593+
{ port: 13_773, role: "server" },
594+
{ port: 5733, role: "web" },
595+
]);
596+
}),
597+
);
573598
});
574599

575600
describe("resolveModePortOffsets", () => {

‎scripts/dev-runner.ts‎

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,15 @@ const MODE_ARGS = {
5555
} as const satisfies Record<string, ReadonlyArray<string>>;
5656

5757
type DevMode = keyof typeof MODE_ARGS;
58-
type PortAvailabilityCheck<R = never> = (port: number) => Effect.Effect<boolean, never, R>;
58+
/**
59+
* `role` matters because only the backend honours `--host`/`T3CODE_HOST`; the
60+
* web port is always loopback. Passed explicitly rather than inferred from the
61+
* port number, which stops distinguishing them under a large port offset.
62+
*/
63+
type PortAvailabilityCheck<R = never> = (
64+
port: number,
65+
role?: "server" | "web",
66+
) => Effect.Effect<boolean, never, R>;
5967

6068
const DEV_RUNNER_MODES = Object.keys(MODE_ARGS) as Array<DevMode>;
6169

@@ -392,6 +400,12 @@ export function checkPortAvailabilityOnHosts<R>(
392400
* another interface, that interface decides whether the bind actually
393401
* succeeds — probing only loopback would hand back a port that is free here
394402
* and taken there, and the server would fail to start.
403+
*
404+
* `configuredHost` applies to the *backend* only. Vite takes its bind address
405+
* from `HOST`, which the runner sets for desktop alone, so the web port stays
406+
* on loopback and must not be judged against the backend's interface —
407+
* a port free on loopback but busy on that interface would otherwise be
408+
* rejected for a server that was never going to bind there.
395409
*/
396410
export function devPortProbeHosts(configuredHost: string | undefined): ReadonlyArray<string> {
397411
const host = configuredHost?.trim();
@@ -403,13 +417,12 @@ export function devPortProbeHosts(configuredHost: string | undefined): ReadonlyA
403417

404418
const makeDefaultCheckPortAvailability =
405419
(configuredHost: string | undefined): PortAvailabilityCheck<NetService.NetService> =>
406-
(port) =>
420+
(port, role) =>
407421
Effect.gen(function* () {
408422
const net = yield* NetService.NetService;
409-
return yield* checkPortAvailabilityOnHosts(
410-
port,
411-
devPortProbeHosts(configuredHost),
412-
(candidatePort, host) => net.canListenOnHost(candidatePort, host),
423+
const hosts = role === "web" ? DEV_PORT_PROBE_HOSTS : devPortProbeHosts(configuredHost);
424+
return yield* checkPortAvailabilityOnHosts(port, hosts, (candidatePort, host) =>
425+
net.canListenOnHost(candidatePort, host),
413426
);
414427
});
415428

@@ -447,10 +460,10 @@ export function findFirstAvailableOffset<R = NetService.NetService>({
447460

448461
const checks: Array<Effect.Effect<boolean, never, R>> = [];
449462
if (requireServerPort) {
450-
checks.push(checkPort(serverPort));
463+
checks.push(checkPort(serverPort, "server"));
451464
}
452465
if (requireWebPort) {
453-
checks.push(checkPort(webPort));
466+
checks.push(checkPort(webPort, "web"));
454467
}
455468

456469
if (checks.length === 0) {

0 commit comments

Comments
 (0)