Skip to content

Commit b883156

Browse files
fix(scripts): scenarios run where localhost resolves to IPv6 first (#34121)
Co-authored-by: Claude Fable 5.2 <[email protected]>
1 parent 3a8c250 commit b883156

3 files changed

Lines changed: 61 additions & 13 deletions

File tree

‎docs/operations/scenarios.md‎

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,19 @@ cd scripts && pnpm exec e2e run scenarios/replay-turn-streams.e2e.ts --headed
7070

7171
e2e starts the dev stack through [`start-app.ts`](../../scripts/scenarios/start-app.ts) on a free
7272
port, runs the tests, and stops it. Results land in `scripts/.e2e`: `report.json`, `summary.md`,
73-
and per test a `trace.md`, its screenshots, and a video. The first run downloads e2e's browser.
73+
and per test a `trace.md`, its screenshots, and a video. `vp run scenarios --target web` runs one
74+
target; flags go straight after the script name, without `--`.
75+
76+
The first run downloads e2e's browser (about 120 MB). To fetch it ahead of time, such as in a
77+
machine image, run this from the repository root after `vp i`:
78+
79+
```sh
80+
node scripts/node_modules/playwright-core/cli.js install --only-shell chromium ffmpeg
81+
```
82+
83+
e2e opens the web app at `127.0.0.1`, and the start script tells the web dev server to bind that
84+
address. It does not use `localhost`, which some machines resolve to `::1` first.
85+
7486
`e2e run --help` lists the rest: `--grep`, `--watch`, `--last-failed`, `--changed-since`.
7587

7688
Run one at a time per checkout: a run recreates the checkout's scenario T3 home and its `.e2e`

‎scripts/e2e.config.ts‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,11 @@ export const T3_HOME = resolved.home;
4646
export const MOBILE_T3_HOME = `${T3_HOME}-mobile`;
4747
export const MOBILE = resolved.mobile;
4848
const startApp = resolved.startApp;
49+
/**
50+
* The one address e2e opens the web app at and the app is told to bind. Never `localhost`: the
51+
* runner and the app can resolve that name to different addresses.
52+
*/
53+
const LOOPBACK = "127.0.0.1";
4954

5055
/**
5156
* Leases a device from the Simlock pool, whose gateway speaks agent-device's lease protocol.
@@ -131,12 +136,12 @@ export default {
131136
name: "web",
132137
engine: web({ viewport: { width: 1280, height: 800 } }),
133138
app: {
134-
url: "http://127.0.0.1:0",
139+
url: `http://${LOOPBACK}:0`,
135140
// Vite answers before the server does; this path is proxied to the server.
136-
readyUrl: "http://127.0.0.1:{port}/.well-known/t3/environment",
141+
readyUrl: `http://${LOOPBACK}:{port}/.well-known/t3/environment`,
137142
command: {
138143
executable: resolved.node,
139-
args: [startApp, "web", T3_HOME, "{port}"],
144+
args: [startApp, "web", T3_HOME, LOOPBACK, "{port}"],
140145
cwd: APP_DIR,
141146
startupTimeout: 300_000,
142147
log: ".e2e/app.log",

‎scripts/scenarios/start-app.ts‎

Lines changed: 40 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -141,17 +141,21 @@ const startApp = Effect.fn("startApp")(function* (input: {
141141
];
142142

143143
if (input.surface === "web") {
144-
const [webPort = ""] = input.ports;
145-
const serverPort = yield* net.reserveLoopbackPort();
144+
// e2e opens the app at this host, so the web server is told to bind exactly it. Left to its
145+
// default, `localhost`, it binds ::1 alone on machines that resolve the name to IPv6 first.
146+
const [host = "", webPort = ""] = input.ports;
147+
const origin = `http://${host}:${webPort}`;
148+
const serverPort = String(yield* net.reserveLoopbackPort());
146149
const token = yield* (yield* SecureRandom.SecureRandom).uuidV4();
147-
const runner = yield* start({
150+
const ports = ["--port", serverPort, "--dev-url", origin];
151+
const webEnv = { ...env, T3CODE_PORT_OFFSET: String(Number(webPort) - BASE_WEB_PORT) };
152+
const server = yield* start({
148153
cwd: checkout,
149154
command: node,
150-
args: devRunner("dev", ["--port", String(serverPort)]),
155+
args: devRunner("dev:server", ports),
151156
env: {
152-
...env,
153-
T3CODE_PORT_OFFSET: String(Number(webPort) - BASE_WEB_PORT),
154-
T3CODE_DEV_ALLOWED_ORIGINS: `http://127.0.0.1:${webPort}`,
157+
...webEnv,
158+
T3CODE_DEV_ALLOWED_ORIGINS: origin,
155159
// Makes the startup pairing link reusable, so every test can sign in with it.
156160
T3CODE_DEV_AUTH_TOKEN: `${token}${token}`.replaceAll("-", ""),
157161
},
@@ -165,7 +169,32 @@ const startApp = Effect.fn("startApp")(function* (input: {
165169
.pipe(Effect.ignore);
166170
},
167171
});
168-
return yield* runner.exitCode;
172+
const web = yield* start({
173+
cwd: checkout,
174+
command: node,
175+
args: devRunner("dev:web", [...ports, "--", "--host", host]),
176+
env: webEnv,
177+
onLine: Console.log,
178+
});
179+
// A web server that came up on another loopback address would leave e2e waiting out its
180+
// whole startup timeout; say so at once instead.
181+
const boundElsewhere = Effect.gen(function* () {
182+
const elsewhere = host === "::1" ? "127.0.0.1" : "::1";
183+
if (yield* net.hasListenerOnHost(Number(webPort), host)) return yield* Effect.never;
184+
if (!(yield* net.hasListenerOnHost(Number(webPort), elsewhere))) {
185+
return yield* new ScenarioError({ message: "The web server is not listening yet." });
186+
}
187+
yield* Console.error(
188+
`The web server listens on ${elsewhere}:${webPort}, not on ${host}: it ignored --host.`,
189+
);
190+
return 1;
191+
}).pipe(
192+
Effect.retry({
193+
schedule: Schedule.spaced("1 second"),
194+
while: (error) => error._tag === "ScenarioError",
195+
}),
196+
);
197+
return yield* Effect.raceAll([server.exitCode, web.exitCode, boundElsewhere]);
169198
}
170199

171200
// The device is another machine: the server listens on every interface, and Metro serves the
@@ -212,7 +241,9 @@ const command = Command.make(
212241
),
213242
home: Argument.String("t3-home").pipe(Argument.withDescription("T3 home to recreate.")),
214243
ports: Argument.String("port").pipe(
215-
Argument.withDescription("web: <web-port>. mobile: <server-port> <metro-port> <host>."),
244+
Argument.withDescription(
245+
"web: <host> <web-port>. mobile: <server-port> <metro-port> <host>.",
246+
),
216247
Argument.variadic(),
217248
),
218249
},

0 commit comments

Comments
 (0)