fix(electron): share held input across windows and tighten launch checks

MI
2h ago
+894-116

What

  • A key or mouse button held with electron.keyboard.down() or electron.mouse.down() now stays held across the app's windows. up() releases it whichever window is active, and closing or crashing every window it went down in releases it too. mouse.wheel() now sends the held buttons.
  • app.launchArguments and electron.launch({ launchArguments }) refuse the switches the engine owns in any spelling Chromium or Node accepts: one dash or two (or / on Windows), any letter case, surrounding whitespace. The list also gains --inspect-brk-node, --inspect-port, --debug-port and --inspect-publish-uid.
  • electron({ env }) and electron.launch({ env }) refuse an empty variable name and a name containing =.
  • docs/reference/electron.mdx describes the owned switches, the variable-name rule, how held keys and buttons behave across windows, that a held Shift does not shift a character passed to press or type (as in Playwright), and that window.reload() does not get past a beforeunload guard.

Why

An adversarial pass on @e2e-dev/electron after #2406 and #2460 (Electron 45.5.0) found three bugs:

  • Each window kept its own record of held keys and buttons, while electron.keyboard and electron.mouse send to whichever window is active. Hold Shift in window 1, activate window 2, call up('Shift'), and Shift stays held in window 1 for the rest of the test: back there, ArrowLeft selects. A mouse button behaved the same way.
  • ownedArgument matched only the two-dash lowercase spelling. Chromium also takes -user-data-dir=<dir>, and the app then kept its data in <dir>, outside electron.userDataDir, where app.clearState() and the end of the test never reach it.
  • checkEnv compared whole names with REFUSED_ENV, while the OS splits an environment entry at its first =. { 'NODE_OPTIONS=--title=x': 'y' } passed the check and started the app with NODE_OPTIONS set, which the docs say neither env layer can do.

How it works

  1. HeldInput (packages/electron/src/cdp/input.ts:78) records each held modifier, key and mouse button with the windows it went down in. Each launch makes one (packages/electron/src/app-process.ts:252), and every window of that launch shares it (packages/electron/src/page.ts:44), so down() and up() read and write the same state in every window (packages/electron/src/cdp/input.ts:143, :188, :314, :354). Mouse.wheel() sends the held buttons (packages/electron/src/cdp/input.ts:395).
  2. Presses that belong to one gesture stay with their window: the modifiers of a chord or a modified tap, longPress and drag pass { local: true } (packages/electron/src/actions.ts:379, :399, :443). Keyboard.press holds its key down on its own window only, and its release clears a held key in every window, as a press on a held key did on one page (packages/electron/src/cdp/input.ts:231). A chord pins a modifier the test already holds elsewhere (pinModifier, packages/electron/src/cdp/input.ts:121), so finishing the chord does not release it.
  3. A window that closes or crashes drops its own holds (packages/electron/src/page.ts:88, :132; reset() at packages/electron/src/cdp/input.ts:133 and :287). A key or button is released once no open window holds it.
  4. When a key event to a closing window fails, up() puts back the holds of the other windows that are still open. Each window carries a generation that closing or crashing raises (bumpGeneration, packages/electron/src/cdp/input.ts:95), and each release carries a number, so a failed up() neither revives a window that closed meanwhile nor undoes a newer up() (restoreOwners, packages/electron/src/cdp/input.ts:62). A failed mouse press or release rolls back the shared last button only when it is still the value that operation wrote, so a newer press in another window keeps it (packages/electron/src/cdp/input.ts:345, :381).
  5. ownedArgument trims the argument and matches one or two dashes in any case (on Windows also a leading /, which Chromium reads as a switch prefix there) across user-data-dir, inspect, inspect-brk, inspect-brk-node, inspect-port, debug-port, inspect-publish-uid, remote-debugging-port and remote-debugging-pipe (packages/electron/src/options.ts:67). Refusing is clearer than reordering the switches so the engine's copy wins, which would leave the inspector switches open.
  6. checkEnv refuses an empty name or one holding =, without quoting the name, since a mistyped KEY=value can hold a secret (packages/electron/src/options.ts:130). launchEnv leaves such names out as well (packages/electron/src/app-process.ts:658).

Overlapping open pull requests: #2439 and #2405 touch some of the same files, with no line conflicts.

Issue

None filed. Found by an adversarial testing pass on @e2e-dev/electron at 7c1b2b258.

Verified

Ran it locally: yes, on macOS arm64 (Electron 45.5.0, Node 26.13.1) and in a Linux container (node:26-bookworm under xvfb), at 3be7aeef7.

A scratch project ran four cases through the built CLI (node packages/e2e/dist/cli/bin.js run): 4 of 4 fail on main, 4 of 4 pass on this branch.

Casemain 7c1b2b258this PR 3be7aeef7
down('Shift') in window 1, up('Shift') in window 2, then ArrowLeft and Backspace on abc in window 1fails: ab instead of acpasses: ac
mouse.down() in window 1, mouse.up() in window 2, mouse.move() in window 1fails: buttons stays 1passes: buttons is 0
electron.launch({ launchArguments: ['-user-data-dir=<dir>'] })launches, app data in <dir>INVALID_ARGUMENT
electron.launch({ env: { 'NODE_OPTIONS=--title=repro': 'x' } })launches with NODE_OPTIONS setINVALID_ARGUMENT
main (7c1b2b258):
  × releases a held Shift across windows                               → expected "ab" to be "ac"
  × releases a held mouse button across windows                        → expect.toBeVisible failed
  × refuses one-dash -user-data-dir in electron.launch()               → toMatchObject requires an object, got "resolved"
  × refuses env variable names containing = in electron.launch()       → toMatchObject requires an object, got "resolved"

this PR (3be7aeef7):
  ✓ releases a held Shift across windows
  ✓ releases a held mouse button across windows
  ✓ refuses one-dash -user-data-dir in electron.launch()
  ✓ refuses env variable names containing = in electron.launch()

Checks at 3be7aeef7 on macOS: pnpm check passed; @e2e-dev/electron unit tests 123 of 123, integration tests 65 of 65; apps/electron-testbed 88 of 88, including the new cross-window test in tests/fixture.e2e.ts. In the Linux container: unit 123 of 123, integration 65 of 65 and testbed 88 of 88; the cross-window and beforeunload integration tests passed 3 more runs there on an earlier commit of this PR. The unit test that refuses / switches on Windows also passes with process.platform set to win32. CI's first run failed the cross-window integration test on Linux and Windows: Chromium there can drop a wheel event sent right after a text edit in a hidden window, with nothing held and in a single window too (reproduced in the container), so the test now sends the wheel before the edit. An adversarial pass on the built CLI found three more cases held: switches that only look like owned ones still reach the app, a window the main process opens after down('Shift') sees Shift held, and app.restart() starts with nothing held. On an earlier round's commit, 32 of 32 mutants of the changed code were killed and a secret-leak probe found nothing.

No video: the testbed's windows are hidden, and the change is input state, launch checks and reference prose, none of it on screen.

The benchmark below was measured on fb93f5f31; the later commits change failure-path rollback, the launch check and tests only. It runs one sign-in test per pair; its "87 tests" counts the tests the --grep skipped as matching. Behavior matched, and no timing moved past the floor. The wall clock's -5.1% is unresolved on a loaded machine (load 6.9), not a speed claim.

bench-ab: 7c1b2b25 (merge base with origin/main) vs fb93f5f3

apps/electron-testbed: e2e run --workers 1 --workers 1 tests/sign-in.e2e.ts --grep signs in through the preload bridge. 6 pairs in random order, resolved after 6 pairs. Threshold ±5% and 10 ms. darwin arm64, Apple M5 Pro x18, Node 26.13.1, load 6.9.

The suite's tests or config differ between base and head, so each side ran its own. A timing change can be the test change.

Behavior

These tests are left out of the timing rows below.

  • added tests/fixture.e2e.ts › electron.keyboard and a window's page › carries held keys and mouse buttons across windows, and releases them on up or window close (electron)
Timings

Every row but the process wall clock covers the 87 tests whose behavior matched.

Metricbase median (IQR)head median (IQR)change (95% CI)verdict
wall (process)964 ms (952 ms-998 ms)932 ms (915 ms-954 ms)-5.1% (-10.6% to +0.4%)unresolved
suite (sum of attempts)461 ms (456 ms-475 ms)468 ms (462 ms-471 ms)+0.9% (-11.8% to +3.5%)same, under the floor
action (events)78 ms (72 ms-81 ms)77 ms (75 ms-80 ms)+2.1% (-16.4% to +12.8%)same, under the floor
step app.open307 ms (306 ms-312 ms)306 ms (306 ms-307 ms)-0.5% (-14.4% to +0.5%)same, under the floor
step locator.fill35 ms (32 ms-38 ms)33 ms (32 ms-35 ms)-1.5% (-18.4% to +11.4%)same, under the floor
step expect.toHaveText18 ms (18 ms-20 ms)19 ms (18 ms-19 ms)+2.7% (-18.4% to +10.6%)same, under the floor
step locator.check16 ms (15 ms-17 ms)16 ms (15 ms-18 ms)-0.6% (-20.8% to +16.2%)same, under the floor
step locator.selectOption14 ms (13 ms-14 ms)14 ms (13 ms-15 ms)+3.5% (-9.9% to +16.8%)same, under the floor
step locator.tap14 ms (13 ms-15 ms)15 ms (14 ms-15 ms)+3.8% (-11.8% to +25.1%)same, under the floor
step expect.toBeChecked6 ms (6 ms-6 ms)6 ms (6 ms-7 ms)0.0% (-6.5% to +18.3%)same
step expect.toHaveValue6 ms (5 ms-6 ms)6 ms (6 ms-6 ms)+8.0% (-8.7% to +20.0%)same, under the floor
step electron.evaluate2 ms (1 ms-3 ms)2 ms (2 ms-3 ms)0.0% (-33.3% to +50.0%)same
Counters
Counterbasehead
events navigation0-10-1
tests skipped8687
Tests outside the threshold: 0 of 87

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

Checklist

  • Changeset added (pnpm changeset) for any change to a published package, or the change is testbed, docs, or CI only.
  • Breaking change: title carries !, the changeset body names what breaks and what replaces it, and the deprecation warning landed a window earlier (stability policy in CONTRIBUTING.md).
  • Docs updated in the same PR: the docs/**/*.mdx page for the behavior, and skills/e2e/ if the skill describes it.
  • Contract change: emitted .d.ts reviewed, tests/types/sdk-types.ts updated; wire change edits the schema and both fixtures.
  • Engine contract change (e2e/engine): changesets for e2e, @e2e-dev/web, and @e2e-dev/mobile.

🤖 Generated with Claude Code

Summary by CodeRabbit

  • New Features
    • Keyboard modifiers and mouse buttons held in one app window remain active across related windows. They are released when the input is released or all windows holding it close or crash. Mouse-wheel input also reflects currently held buttons.
  • Bug Fixes
    • Reloading a page no longer bypasses a cancelling beforeunload prompt.
    • Held modifiers no longer alter characters passed to press or type.
  • Validation
    • Launch options reject empty environment-variable names, names containing =, and engine-owned debugging switches.