Skip to content

Commit 8cbcf2e

Browse files
committed
refactor(tailscale): retain upstream shared-dev loopback handling
1 parent a853c2a commit 8cbcf2e

7 files changed

Lines changed: 28 additions & 73 deletions

File tree

‎apps/web/vite.config.ts‎

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import "vite-plus/test/config";
99
import { defineConfig, type Connect, type Plugin } from "vite-plus";
1010
import pkg from "./package.json" with { type: "json" };
1111

12-
import { DEV_PROXIED_PATH_PREFIXES, resolveWebDevServerHost } from "@t3tools/shared/devProxy";
12+
import { DEV_PROXIED_PATH_PREFIXES } from "@t3tools/shared/devProxy";
1313

1414
import { loadRepoEnv } from "../../scripts/lib/public-config";
1515
import { thirdPartyLicensesPlugin } from "../../scripts/lib/third-party-licenses";
@@ -29,9 +29,7 @@ const isSingleOriginDev = process.env.T3CODE_SINGLE_ORIGIN_DEV === "1";
2929

3030
const port = Number(process.env.PORT ?? 5733);
3131
const explicitHost = process.env.HOST?.trim();
32-
const sharedBindHost = process.env.T3CODE_WEB_BIND_HOST?.trim();
33-
const host = resolveWebDevServerHost({ explicitHost, sharedBindHost });
34-
const explicitHmrHost = sharedBindHost ? undefined : explicitHost;
32+
const host = explicitHost || "localhost";
3533
const configuredWsUrl = isSingleOriginDev ? undefined : process.env.VITE_WS_URL?.trim();
3634
const configuredHttpUrl = isSingleOriginDev ? undefined : process.env.VITE_HTTP_URL?.trim();
3735
const configuredRelayUrl = repoEnv.VITE_T3CODE_RELAY_URL?.trim() || "";
@@ -264,11 +262,11 @@ export default defineConfig(() => {
264262
// page origin, which is what makes HMR work over Tailscale/LAN instead of
265263
// failing an attempt against the wrong machine's localhost first.
266264
// (Vite 8 logs connection state via console.debug — enable "Verbose".)
267-
...(explicitHmrHost
265+
...(explicitHost
268266
? {
269267
hmr: {
270268
protocol: "ws",
271-
host: explicitHmrHost,
269+
host: explicitHost,
272270
clientPort: port,
273271
},
274272
}

‎packages/shared/src/devProxy.test.ts‎

Lines changed: 0 additions & 39 deletions
This file was deleted.

‎packages/shared/src/devProxy.ts‎

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,6 @@
1010
*/
1111
export const DEV_PROXIED_PATH_PREFIXES = ["/api", "/oauth", "/.well-known", "/ws"] as const;
1212

13-
export const SHARED_DEV_LOOPBACK_HOST = "127.0.0.1";
14-
15-
export function resolveWebDevServerHost(input: {
16-
readonly explicitHost: string | undefined;
17-
readonly sharedBindHost: string | undefined;
18-
}): string {
19-
return input.sharedBindHost || input.explicitHost || "localhost";
20-
}
21-
2213
export function isDevProxiedPath(pathname: string): boolean {
2314
return DEV_PROXIED_PATH_PREFIXES.some(
2415
(prefix) => pathname === prefix || pathname.startsWith(`${prefix}/`),

‎scripts/dev-runner.test.ts‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1115,7 +1115,6 @@ it.layer(NodeServices.layer)("dev-runner", (it) => {
11151115
Effect.gen(function* () {
11161116
const env = yield* shareSpawnedEnv({ ambientBundledDev: undefined });
11171117
assert.equal(env?.T3CODE_BUNDLED_DEV, "1");
1118-
assert.equal(env?.T3CODE_WEB_BIND_HOST, "127.0.0.1");
11191118
}),
11201119
);
11211120

@@ -1152,7 +1151,6 @@ it.layer(NodeServices.layer)("dev-runner", (it) => {
11521151
);
11531152

11541153
assert.equal(captured?.T3CODE_BUNDLED_DEV, undefined);
1155-
assert.equal(captured?.T3CODE_WEB_BIND_HOST, "");
11561154
}),
11571155
);
11581156
});

‎scripts/dev-runner.ts‎

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import * as NodeOS from "node:os";
55
import * as NodeRuntime from "@effect/platform-node/NodeRuntime";
66
import * as NodeServices from "@effect/platform-node/NodeServices";
77
import * as NetService from "@t3tools/shared/Net";
8-
import { SHARED_DEV_LOOPBACK_HOST } from "@t3tools/shared/devProxy";
98
import { resolveGitWorktreePath, resolveWorktreeT3Home } from "@t3tools/shared/devHome";
109
import { HostProcessEnvironment, HostProcessWorkingDirectory } from "@t3tools/shared/hostProcess";
1110
import { resolveSpawnCommand } from "@t3tools/shared/shell";
@@ -323,10 +322,6 @@ export function createDevRunnerEnv({
323322
devUrl?.toString() ??
324323
`http://${isDesktopMode ? DESKTOP_DEV_LOOPBACK_HOST : "localhost"}:${webPort}`,
325324
};
326-
// Internal runner-to-Vite setting. An explicit empty value prevents a
327-
// repo env file from changing ordinary dev binding; --share replaces it
328-
// only after the exact Tailscale mapping has been acquired.
329-
output.T3CODE_WEB_BIND_HOST = "";
330325

331326
if (configuredBaseDir !== undefined) {
332327
output.T3CODE_HOME = resolvedBaseDir;
@@ -768,11 +763,6 @@ export function runDevRunnerWithInput(input: DevRunnerCliInput) {
768763
);
769764

770765
if (shared) {
771-
// Tailscale proxies to IPv4 loopback. Bind Vite to that exact same
772-
// address instead of `localhost`, whose address family varies by
773-
// host. This is separate from HOST so remote HMR still derives its
774-
// endpoint from the browser's shared origin.
775-
env.T3CODE_WEB_BIND_HOST = SHARED_DEV_LOOPBACK_HOST;
776766
// The app is reached from the tailnet origin. Vite already allows
777767
// *.ts.net hosts; the backend needs the origin for credentialed
778768
// requests that bypass the proxy (desktop renderer, direct calls).

‎scripts/lib/dev-share.test.ts‎

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ describe("unshareDevServer", () => {
8080
Effect.provide(
8181
spawnerLayer({
8282
off: { exitCode: 0 },
83-
serveStatus: serveStatus("http://127.0.0.1:5788"),
83+
serveStatus: serveStatus("http://localhost:5788"),
8484
}),
8585
),
8686
);
@@ -101,7 +101,7 @@ describe("unshareDevServer", () => {
101101
Effect.provide(
102102
spawnerLayer({
103103
off: { exitCode: 1, stderr: "permission denied" },
104-
serveStatus: serveStatus("http://127.0.0.1:5788"),
104+
serveStatus: serveStatus("http://localhost:5788"),
105105
}),
106106
),
107107
);
@@ -136,6 +136,18 @@ describe("shareDevServer", () => {
136136
}),
137137
);
138138

139+
// Vite binds `localhost`, which modern Node resolves to `::1` first, so a
140+
// 127.0.0.1 target would proxy to a loopback nothing listens on.
141+
it.effect("proxies to the localhost name Vite binds, not 127.0.0.1", () =>
142+
Effect.gen(function* () {
143+
const calls: Array<ReadonlyArray<string>> = [];
144+
yield* shareDevServer({ webPort: 5788 }).pipe(Effect.provide(spawnerLayer({ calls })));
145+
146+
const serveCall = calls.find((args) => args.includes("--bg"));
147+
assert.deepEqual(serveCall, ["serve", "--bg", "--https=5788", "http://localhost:5788"]);
148+
}),
149+
);
150+
139151
it.effect("reports a serve failure without claiming another mapping was removed", () =>
140152
Effect.gen(function* () {
141153
const error: DevShareError = yield* shareDevServer({ webPort: 5788 }).pipe(

‎scripts/lib/dev-share.ts‎

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import {
2020
type TailscaleServeError,
2121
type TailscaleStderrDiagnostic,
2222
} from "@t3tools/tailscale";
23-
import { SHARED_DEV_LOOPBACK_HOST } from "@t3tools/shared/devProxy";
2423
import * as Effect from "effect/Effect";
2524
import * as Schema from "effect/Schema";
2625
import type { ChildProcessSpawner } from "effect/unstable/process";
@@ -109,7 +108,7 @@ export type DevShareError =
109108
| DevServeFailedError;
110109

111110
/**
112-
* Removes any mapping for `webPort`, reporting whether the port is now clear.
111+
* Removes the matching localhost mapping for `webPort`, preserving other handlers.
113112
*
114113
* Runs uninterruptibly: this is called from a finalizer on the way out of an
115114
* interrupted program, and cancelling the cleanup subprocess would leave
@@ -129,7 +128,7 @@ export const unshareDevServer = (
129128
ChildProcessSpawner.ChildProcessSpawner
130129
> =>
131130
disableTailscaleServe({
132-
localHost: SHARED_DEV_LOOPBACK_HOST,
131+
localHost: "localhost",
133132
localPort: webPort,
134133
servePort: webPort,
135134
}).pipe(
@@ -159,7 +158,7 @@ export interface DevShareResult {
159158

160159
/**
161160
* Publishes `webPort` on the tailnet at the same port number and returns the
162-
* resulting HTTPS URL. Idempotent: re-running replaces any existing mapping.
161+
* resulting HTTPS URL. Reuses the matching handler and refuses other mappings.
163162
*/
164163
export const shareDevServer = Effect.fn("devShare.shareDevServer")(function* (input: {
165164
readonly webPort: number;
@@ -171,10 +170,16 @@ export const shareDevServer = Effect.fn("devShare.shareDevServer")(function* (in
171170
return yield* new TailnetNameMissingError();
172171
}
173172

173+
// Proxy to the hostname Vite binds rather than the package default of
174+
// 127.0.0.1. Vite listens on `localhost`, which Node 17+ resolves to `::1`
175+
// first, so it only binds the IPv6 loopback and a 127.0.0.1 target has
176+
// nothing behind it (tailscale answers 502). Passing `localhost` lets the
177+
// tailscale proxy resolve it the same way Node did. Not a literal `[::1]`:
178+
// tailscale rejects that form.
174179
yield* ensureTailscaleServe({
175-
localHost: SHARED_DEV_LOOPBACK_HOST,
176180
localPort: input.webPort,
177181
servePort: input.webPort,
182+
localHost: "localhost",
178183
}).pipe(
179184
Effect.mapError((error) => {
180185
const explanation = explainCommandFailure(error);

0 commit comments

Comments
 (0)