Skip to content
TypeScript

fix(dev): share dev servers on the loopback Vite actually binds (#9324)

Julius Marminge66419a1d1b80
diff --git a/scripts/lib/dev-share.test.ts b/scripts/lib/dev-share.test.ts
index b2dfba3585e..2b133e456c4 100644
--- a/scripts/lib/dev-share.test.ts
+++ b/scripts/lib/dev-share.test.ts
@@ -27,11 +27,16 @@ const encode = (value: string) => Stream.make(new TextEncoder().encode(value));
* test set the outcome of the `off` (pre-clear) and `serve` calls separately —
* they are the same subcommand and are told apart by the trailing `off`.
*/
-const spawnerLayer = (input: { readonly off?: CallResult; readonly serve?: CallResult }) =>
+const spawnerLayer = (input: {
+ readonly off?: CallResult;
+ readonly serve?: CallResult;
+ readonly calls?: Array<ReadonlyArray<string>>;
+}) =>
Layer.succeed(
ChildProcessSpawner.ChildProcessSpawner,
ChildProcessSpawner.make((command) => {
const args = "args" in command ? (command.args as ReadonlyArray<string>) : [];
+ input.calls?.push(args);
const result: CallResult = args.includes("status")
? { exitCode: 0 }
: args.includes("off")
@@ -102,6 +107,20 @@ describe("shareDevServer", () => {
}),
);
+ // Vite binds `localhost`, which modern Node resolves to `::1` first, so a
+ // 127.0.0.1 target would proxy to a loopback nothing listens on.
+ it.effect("proxies to the localhost name Vite binds, not 127.0.0.1", () =>
+ Effect.gen(function* () {
+ const calls: Array<ReadonlyArray<string>> = [];
+ yield* shareDevServer({ webPort: 5788 }).pipe(
+ Effect.provide(spawnerLayer({ off: { exitCode: 0 }, calls })),
+ );
+
+ const serveCall = calls.find((args) => args.includes("--bg"));
+ assert.deepEqual(serveCall, ["serve", "--bg", "--https=5788", "http://localhost:5788"]);
+ }),
+ );
+
// The stale-mapping clear runs before serve, so a failure here leaves the
// port serving nothing. Saying only "serve failed" would let an operator
// assume their previous mapping survived.
diff --git a/scripts/lib/dev-share.ts b/scripts/lib/dev-share.ts
index 0f843b3ba91..e3fb884eb83 100644
--- a/scripts/lib/dev-share.ts
+++ b/scripts/lib/dev-share.ts
@@ -195,7 +195,17 @@ export const shareDevServer = Effect.fn("devShare.shareDevServer")(function* (in
});
}
- yield* ensureTailscaleServe({ localPort: input.webPort, servePort: input.webPort }).pipe(
+ // Proxy to the hostname Vite binds rather than the package default of
+ // 127.0.0.1. Vite listens on `localhost`, which Node 17+ resolves to `::1`
+ // first, so it only binds the IPv6 loopback and a 127.0.0.1 target has
+ // nothing behind it (tailscale answers 502). Passing `localhost` lets the
+ // tailscale proxy resolve it the same way Node did. Not a literal `[::1]`:
+ // tailscale rejects that form.
+ yield* ensureTailscaleServe({
+ localPort: input.webPort,
+ servePort: input.webPort,
+ localHost: "localhost",
+ }).pipe(
Effect.mapError((error) => {
const explanation = explainCommandFailure(error);
return new DevServeFailedError({