feat(config): accept a function for app.command.env (ENG-819)

Merged
SA
4d ago
+959-182

Builds on #2191 (services), which is merged.

What

app.command.env can be a sync or async function of the config's services:

export default defineConfig({
  services: { email },
  targets: [{ engine: web(), app: { url: 'http://127.0.0.1:0', command: {
    executable: 'node', args: ['app/server.mjs'],
    env: ({ services }) => ({ PORT: '{port}', EMAIL_API_URL: services.email.url }),
  } } }],
});
  • The function gets { services }, the same values a test sees. defineConfig types them from config.services.
  • It's called one time per run, after the services start and before any app command starts. It's still called when a running process is reused with reuseExisting, but that process doesn't get the variables and a notice is printed. Targets that share a function share one call.
  • {port} expands in the returned values. Every value must be a string. undefined values are type errors, and they're dropped at run time (a JavaScript config can return them), the same as in an env object.
  • If the function throws, or doesn't return an object of strings, the run fails with INVALID_CONFIG (exit 2). A config or infrastructure error it throws, like APP_UNREACHABLE, keeps its code. If it doesn't return within the command's startupTimeout, the run fails with APP_UNREACHABLE.
  • Commands that only read the config, such as e2e list, never call it.

Why

With an env object, the app can only get a service's address if it's fixed, like http://127.0.0.1:4010. With a function, a service can pick a free port or start a container and pass its address to the app.

How it works

  1. CommandConfig, TargetApp, Target, ProjectConfig, and E2EConfig take a Services type parameter for env. The function type is declared as a method so a defineConfig config stays assignable to E2EConfig (packages/core/src/types.ts:1554, with a note saying why). defineConfig keeps Config as its first type parameter and infers Services, its second, from config.services, so defineConfig<MyConfig>(…) still compiles (packages/e2e/src/config/define.ts:16).
  2. normalizeCommand keeps a function env as it is (packages/core/src/config/command.ts:109), and digestCommand leaves it out of the digest, so a worker that imports the config again gets the same digest (command.ts:173).
  3. The runner passes the copied service values to startDeclaredProcesses as { services }, beside the optional process pool (packages/e2e/src/run/runner.ts:1172). When a target has an env function, commandEnvs calls each distinct function once within startupTimeout and checks the result with returnedEnv (packages/core/src/run/provision.ts:307, :412, packages/core/src/config/app.ts:278). It checks the signal before each call (provision.ts:424). Each call goes through startEnvCall (provision.ts:383): a config or infrastructure error keeps its code, and anything else becomes INVALID_CONFIG. That includes a test error, like a service call passed a value it can't copy, and a service call's error, which comes back without its code.
  4. declaredCommands puts the returned variables in the command before it builds the process key, so targets with the same command and variables still share one app process (packages/core/src/run/declared-processes.ts:39, SpawnedCommand at command.ts:23).
  5. startDeclaredProcesses wraps the ready hook of each app process. When the process was reused and one of its targets has an env function, a notice is printed before the ready line (packages/core/src/run/provision.ts:329).
  6. Targets that share one env function share one call. The call records when it started and when it settled, so each target is held to its own startupTimeout from the start, in any target order (packages/core/src/run/provision.ts:462). The env call and the ready probe each get startupTimeout, with one default, DEFAULT_STARTUP_TIMEOUT_MS (packages/core/src/run/managed-process.ts:15).
  7. returnedEnv requires a plain object, so returning a Map fails with INVALID_CONFIG instead of starting the app without any variables (packages/core/src/config/app.ts:279). For a returned string, the error says got a string instead of quoting it, since a connection URL with a password is a likely mistake there (app.ts:280).
  8. In e2e mcp, the open_session summary shows env from a function for a command whose env is a function, because the variable names aren't known there (packages/e2e/src/mcp/app-lines.ts:40).

Tests cover each of these: a kept error code, a test error that becomes INVALID_CONFIG, a returned Map or string, both target orders for a shared function, and the reuseExisting notice, printed once for a reused process and never when the runner starts the app.

The env reference, the error rows, the services guide, and the skill pages now pass service addresses through an env function. The docs examples no longer name a port: the email service listens on port 0 and yields the address it got, and the emulate example uses port: 0.

Not covered yet

Two limits are out of scope for this PR:

  • With reuseExisting, the runner can't tell whether the running process got the variables, so it prints a notice and carries on. ENG-1540 tracks recording a hash of the app command when the runner starts it, and failing when a reused process doesn't match.
  • Service values reach the app only through app.command.env. Mobile and desktop apps take settings as launch arguments rather than environment variables, and apps started outside e2e don't have app.command. A possible addition is a launchArguments function on the mobile target's app, like the env function.

Verified

Ran it locally: yes

The built CLI at this PR's head (a9db39d) ran the docs example, whose env function reads services.email.url:

$ e2e run
 ✓ service "email" ready 2ms
 ✓ target "web" command at http://127.0.0.1:54374/ ready 90ms
   ✓ |web| tests/signup.e2e.ts > signing up sends one welcome email 386ms
      Tests  1 passed (1)

The new notice, from a config with reuseExisting: true run against an app started by hand on port 3456:

$ e2e run --config e2e.reuse.config.ts
 ✓ service "email" ready 3ms
ℹ target "web" command: reusing the process already serving http://127.0.0.1:3456/
ℹ target "web" command: the process at http://127.0.0.1:3456/ didn't get the variables from the target's env function. Restart it with them, or give fixed ports to the services it uses
 ✓ target "web" command at http://127.0.0.1:3456/ reused 8ms

Recording of the same e2e run --config e2e.reuse.config.ts run as above, against an app started by hand on port 3456. It shows the two notices, then the test fails as expected because the app wasn't given the email service's URL.

reuseExisting notice

Benchmark: pnpm bench:ab at the top of the stack (#2259, which includes this PR) against main, on two web benchmark files:

bench-ab: c3109278 (merge base with origin/main) vs b6f27edd

apps/web-benchmark: e2e run --workers 1 tests/control-inventory.e2e.ts tests/cart-totals.e2e.ts. 6 pairs in random order, resolved after 6 pairs. Threshold ±3% and 10 ms. darwin arm64, Apple M5 Pro x18, Node 24.22.1, load 4.8.

Behavior

Every test ran the same steps with the same outcome on every run of both builds.

Timings

Metricbase median (IQR)head median (IQR)change (95% CI)verdict
wall (process)4.53 s (4.48 s-4.54 s)4.50 s (4.41 s-4.61 s)-0.7% (-2.6% to +3.3%)unresolved
suite (sum of attempts)3.69 s (3.67 s-3.72 s)3.71 s (3.66 s-3.75 s)-1.0% (-2.2% to +2.3%)same
action (events)2.54 s (2.54 s-2.58 s)2.55 s (2.54 s-2.57 s)-0.1% (-2.2% to +0.9%)same, under the floor
step locator.longPress1.51 s (1.51 s-1.52 s)1.51 s (1.50 s-1.51 s)-0.2% (-1.1% to +0.4%)same, under the floor
step app.open740 ms (736 ms-743 ms)746 ms (726 ms-773 ms)-1.0% (-3.5% to +5.4%)same, under the floor
step locator.tap457 ms (447 ms-468 ms)470 ms (448 ms-479 ms)+0.5% (-5.3% to +5.6%)same, under the floor
step locator.check286 ms (276 ms-298 ms)296 ms (281 ms-297 ms)-0.2% (-4.6% to +7.9%)same, under the floor
step expect.toHaveText132 ms (129 ms-135 ms)130 ms (127 ms-134 ms)+1.1% (-6.6% to +4.3%)same, under the floor
step locator.scrollIntoView71 ms (69 ms-72 ms)72 ms (68 ms-72 ms)+0.0% (-7.1% to +7.3%)same, under the floor
step locator.secondaryTap70 ms (61 ms-79 ms)64 ms (62 ms-73 ms)-8.5% (-21.0% to +25.2%)same, under the floor
step locator.setInputFiles65 ms (64 ms-67 ms)63 ms (61 ms-65 ms)-3.2% (-6.2% to -0.7%)same, under the floor
step locator.doubleTap62 ms (61 ms-62 ms)63 ms (61 ms-63 ms)+0.8% (-16.7% to +3.3%)same, under the floor
step locator.uncheck38 ms (30 ms-45 ms)33 ms (32 ms-33 ms)-14.3% (-29.7% to +31.3%)same, under the floor
step app.back15 ms (14 ms-16 ms)14 ms (14 ms-15 ms)-6.5% (-12.3% to +10.2%)same, under the floor
step expect.toBeVisible3 ms (2 ms-3 ms)2 ms (2 ms-2 ms)-33.3% (-42.3% to +41.4%)same, under the floor
step expect.toBeChecked2 ms (2 ms-2 ms)3 ms (2 ms-3 ms)0.0% (0.0% to +73.2%)same
step expect.toHaveURL1 ms (0 ms-1 ms)0 ms (0 ms-1 ms)0.0% (0.0% to 0.0%)same

Counters

Every counter matched on every run of both builds.

Tests outside the threshold: 0 of 95

With 95 tests compared at 95%, about 4.8 could land outside by chance.