Repository navigation
fix(napi-derive): only FnArgs unpacks tuple to variadic params - #3264
Conversation
`Function<(T1, T2), R>`, `FunctionRef<(T1, T2), R>`, and `ThreadsafeFunction<(T1, T2)>` were emitting variadic TypeScript params as if `FnArgs` had been used. Without `FnArgs`, the tuple is serialized to a single JS array argument at runtime, so the typegen now emits `arg: [T1, T2]` for those slots and reserves the variadic expansion for `FnArgs<(T1, T2)>` only. Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Run ID: ⛔ Files ignored due to path filters (1)
📒 Files selected for processing (1)
💤 Files with no reviewable changes (1)
📝 WalkthroughWalkthroughTightens backend type generation to distinguish Changes
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~22 minutes Possibly related PRs
Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
How to use the Graphite Merge QueueAdd either label to this PR to merge it via the merge queue:
You must have a Graphite account in order to use the merge queue. Sign up using this link. An organization admin has enabled the Graphite Merge Queue in this repository. Please do not merge from GitHub as this will restart CI on PRs being processed by the merge queue. This stack of pull requests is managed by Graphite. Learn more about stacking. |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 512c9676cc
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
There was a problem hiding this comment.
🧹 Nitpick comments (1)
crates/backend/src/typegen.rs (1)
722-740: Heads-up: the':'heuristic can mis-detect "already-named" types.
ty.contains(':')is used to skip wrapping whentylooks pre-named, but it also matches inline TS shapes like{ foo: number }orRecord<K, V>-derived structures that happen to print with a colon. In that case we'd emit(({ foo: number }) => R)instead of(arg: { foo: number }) => R, which is invalid as a TS function-type parameter. This mirrors the same pre-existing heuristic inhandle_threadsafe_function_type(line 560), so it's not a regression — just worth keeping in mind for a follow-up if/when inline object types start showing up here. Most user-facing types currently resolve to a bare identifier (struct/class name, primitive, alias), so this is a low-priority nit.🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@crates/backend/src/typegen.rs` around lines 722 - 740, The current heuristic in wrap_fn_like_arg using ty.contains(':') incorrectly treats inline object types as "already-named" and avoids adding an argument name; change the check to detect a true TypeScript identifier/qualified/generic name instead of looking for ':'—e.g., replace the colon test with a validation that ty matches a TS identifier or dotted/templated identifier pattern (letters/_, $, digits after first char, optional generics and dot-qualified parts) and only skip wrapping when that validation succeeds; apply the same fix where the same heuristic is used in handle_threadsafe_function_type so inline object shapes like "{ foo: number }" will be wrapped as "arg: { foo: number }" while genuine names like "MyType" or "Namespace.MyType<T>" remain unwrapped.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Nitpick comments:
In `@crates/backend/src/typegen.rs`:
- Around line 722-740: The current heuristic in wrap_fn_like_arg using
ty.contains(':') incorrectly treats inline object types as "already-named" and
avoids adding an argument name; change the check to detect a true TypeScript
identifier/qualified/generic name instead of looking for ':'—e.g., replace the
colon test with a validation that ty matches a TS identifier or dotted/templated
identifier pattern (letters/_, $, digits after first char, optional generics and
dot-qualified parts) and only skip wrapping when that validation succeeds; apply
the same fix where the same heuristic is used in handle_threadsafe_function_type
so inline object shapes like "{ foo: number }" will be wrapped as "arg: { foo:
number }" while genuine names like "MyType" or "Namespace.MyType<T>" remain
unwrapped.
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Run ID: d9e94ab4-0286-47c1-909c-d08e06e44fda
⛔ Files ignored due to path filters (1)
examples/napi/__tests__/__snapshots__/values.spec.ts.snapis excluded by!**/*.snap
📒 Files selected for processing (5)
crates/backend/src/typegen.rsexamples/napi/__tests__/__snapshots__/values.spec.ts.mdexamples/napi/index.d.ctsexamples/napi/src/function.rsexamples/napi/src/threadsafe_function.rs
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes and found 1 potential issue.
❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.
Reviewed by Cursor Bugbot for commit 512c967. Configure here.
The previous `ty.contains(':')` heuristic in `wrap_fn_like_arg` was too
broad — it skipped wrapping for any inner result containing a colon,
including legitimate function-type-notation like `(arg: number) => number`,
producing invalid TypeScript such as `((arg: number) => number) => R`
for `Function<Function<u32, u32>, R>`.
Replace the heuristic with a syntactic `is_fn_args_path(generic_ty)`
check, since `FnArgs<T>` is the only generic that already produces a
parameter binding form. Adds a regression fixture exercising
`Function<Function<u32, u32>, u32>`.
Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
The wasi-worker-browser template emitted `const errorOutputs = []` unconditionally, but the array is only used inside the `printErr` override and the `onError` handler — both of which are only generated when `errorEvent: true`. Without that flag the variable is declared but never read, which trips lint on regenerated outputs. Gate the declaration on the same `errorEvent` flag and regenerate the example's wasi-worker-browser.mjs. Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@examples/napi/wasi-worker-browser.mjs`:
- Line 6: Remove the unused top-level declaration errorOutputs (delete the line
"const errorOutputs = []") and also revert the unrelated
trailing-whitespace-only edits in the file so the diff only contains the
intended FnArgs/tuple typegen changes; ensure no other references to
errorOutputs remain and run lint to confirm no no-unused-vars violations.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Run ID: 8514344d-304a-4e2e-bc6a-ac47afd91934
⛔ Files ignored due to path filters (1)
examples/napi/__tests__/__snapshots__/values.spec.ts.snapis excluded by!**/*.snap
📒 Files selected for processing (8)
crates/backend/src/typegen.rsexamples/napi/__tests__/__snapshots__/values.spec.ts.mdexamples/napi/example.wasi-browser.jsexamples/napi/example.wasi.cjsexamples/napi/index.cjsexamples/napi/index.d.ctsexamples/napi/src/function.rsexamples/napi/wasi-worker-browser.mjs
✅ Files skipped from review due to trivial changes (2)
- examples/napi/example.wasi-browser.js
- examples/napi/index.cjs
🚧 Files skipped from review as they are similar to previous changes (2)
- examples/napi/index.d.cts
- examples/napi/src/function.rs
The wasi-worker template now only emits `const errorOutputs = []` when errorEvent is enabled (commit 73121e7). The snapshot for the default (errorEvent: false) case lagged behind the template change.


Function<(T1, T2), R>,FunctionRef<(T1, T2), R>, andThreadsafeFunction<(T1, T2)>were emitting variadic TypeScript paramsas if
FnArgshad been used. WithoutFnArgs, the tuple is serializedto a single JS array argument at runtime, so the typegen now emits
arg: [T1, T2]for those slots and reserves the variadic expansionfor
FnArgs<(T1, T2)>only.Close #3261
Co-Authored-By: Claude Opus 4.7 (1M context) [email protected]
Note
Medium Risk
Changes core Rust→TypeScript type generation for
Function/FunctionRef/ThreadsafeFunction, which can affect downstream typings and may surface new compile errors in consumer code. Runtime behavior is unchanged but mis-typed APIs may require adjustments.Overview
Fixes typegen for
Function<(T1, T2), R>,FunctionRef<(...) , R>, andThreadsafeFunction<(...)>so tuples are emitted as a single TypeScript argument (e.g.arg: [T1, T2]) instead of being incorrectly expanded into variadic params; onlyFnArgs<(...)>keeps variadic expansion.Adds new example exports/tests covering tuple-as-single-arg callbacks and nested function arguments, and updates generated snapshot typings accordingly. Separately, the WASI worker template now only declares
errorOutputswhenerrorEventhandling is enabled (removing an unused variable in the default template).Reviewed by Cursor Bugbot for commit 48bb33a. Bugbot is set up for automated code reviews on this repo. Configure here.
Summary by CodeRabbit
New Features
Chores