build(desktop): write the native driver's types from the crate
Merged
OK
14h ago+1393+1393+0123456789012345678901234567890123456789-98-98-01234567890123456789
- The desktop engine's TypeScript view of the native driver was written by hand:
DesktopDriverindriver.tsand 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 --typesnow writespackages/desktop/src/native.tsfrom the crate's#[napi]items, the waynapi build --dtsdoes, andDesktopDriveristypeofthat file. A renamed or added option in Rust failstscin the engine and in the fake driver. Options that reach a call through a spread are typed withsatisfiesso they are checked too.- CI's
nativejob builds with--typesand fails on a diff against the committed file. - The loader stays ours (checkout build first,
EngineErrorcontract). Only the type generator of@napi-rs/cliis used. - A release build takes no new dependency: the script imports
@napi-rs/clionly 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,RawWindowindriver.ts,RawSnapshotinnodes.ts). The crate sends those throughserde_json::Value, so NAPI-RS cannot see them. Thets_return_typeattributes named types that existed nowhere (NativeEnvelope<NativeLaunched>); they now name the real ones, whichnative.tsimports, 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.tsandsurface.tschange). Not in this PR.
Verified
Ran it locally: yes
- Drift is caught: renamed
claim_mstoclaim_millisinlib.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
--typesthe script loads with nonode_modulesand leavesnative.tsalone, which is howrelease.ymlruns it. With--typesand no install it stops in a second, before cargo. - Renaming
focusedRef,sheetId,content,pathorphysicalinnative.tsfailstscat each spread site insurface.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.