build(desktop): write the native driver's types from the crate

Merged
OK
14h ago
+1393-98
  • The desktop engine's TypeScript view of the native driver was written by hand: DesktopDriver in driver.ts and an unchecked cast in the loader. Nothing tied it to the crate except a list of export names in a unit test.
  • build-native.ts --types now writes packages/desktop/src/native.ts from the crate's #[napi] items, the way napi build --dts does, and DesktopDriver is typeof that file. A renamed or added option in Rust fails tsc in the engine and in the fake driver. Options that reach a call through a spread are typed with satisfies so they are checked too.
  • CI's native job builds with --types and fails on a diff against the committed file.
  • The loader stays ours (checkout build first, EngineError contract). Only the type generator of @napi-rs/cli is used.
  • A release build takes no new dependency: the script imports @napi-rs/cli only under --types.
flowchart LR
  A["crates/e2e-desktop-napi<br/>#[napi] fns and objects"] -- "build-native.ts --types" --> B["src/native.ts<br/>(committed)"]
  B -- "typeof" --> C[DesktopDriver]
  C --> D[surface.ts calls]
  C --> E[FakeDriver in tests]
  A -. "CI: git diff --exit-code" .-> B
// packages/desktop/src/driver.ts
import type * as native from './native.ts';
export type DesktopDriver = typeof native & { readonly version: string };

Not covered, for review

  • What a call resolves with is still written by hand (Envelope, Launched, RawWindow in driver.ts, RawSnapshot in nodes.ts). The crate sends those through serde_json::Value, so NAPI-RS cannot see them. The ts_return_type attributes named types that existed nowhere (NativeEnvelope<NativeLaunched>); they now name the real ones, which native.ts imports, so at least the name is checked.
  • Follow-up, agreed with @kdy1 in the comments: move the payloads to #[napi(object)] structs (keys become camelCase, nodes.ts and surface.ts change). Not in this PR.

Verified

Ran it locally: yes

  • Drift is caught: renamed claim_ms to claim_millis in lib.rs, rebuilt with --types, and the engine no longer typechecks.
$ diff native.good.ts packages/desktop/src/native.ts
<   claimMs: number
>   claimMillis: number
$ pnpm --filter @e2e-dev/desktop typecheck
src/surface.ts(410,9): error TS2353: Object literal may only specify known properties, and 'claimMs' does not exist in type 'LaunchOptions'.
  • The file is stable: three builds in a row (arm64 cold, arm64 with a warm cargo cache, x64) wrote the same bytes.
  • Without --types the script loads with no node_modules and leaves native.ts alone, which is how release.yml runs it. With --types and no install it stops in a second, before cargo.
  • Renaming focusedRef, sheetId, content, path or physical in native.ts fails tsc at each spread site in surface.ts.
  • Desktop testbed on the rebuilt addon, macOS 27, pnpm test:desktop-testbed: 31 of 31 pass.
  • Unit tests with the real addon (REQUIRE_DESKTOP_NATIVE=1): 276 pass.

Summary by CodeRabbit

  • Developer Experience
    • Desktop TypeScript declarations now cover native operations, options, and results, improving editor support and type checking.
    • Desktop driver types align with the native module’s exported types, including pointer targets and nullable options.
  • Build & Reliability
    • Native builds can generate TypeScript declarations, and macOS CI checks that generated declarations match the committed version.
  • Documentation
    • Desktop native-build instructions now explain declaration generation, clarify where return types are documented, and note the workspace installation requirement.