fix(cli): NAPI_RS_FORCE_WASI only activates on 'true' or 'error'#3236
Conversation
The generated native.js template treated process.env.NAPI_RS_FORCE_WASI as a plain truthy-string check. In JavaScript any non-empty string is truthy, including 'false' and '0', so setting NAPI_RS_FORCE_WASI=false (intending to opt out of forced WASI) inadvertently triggered the WASI fallback — the same path that tries to load <pkg>.wasi.cjs and fails with ENOENT for packages that don't ship a WASI binding. Reported by lancedb/lancedb#3267, where downstream users hit: ENOENT: no such file or directory, open '.../@lancedb/lancedb/dist/lancedb.wasi.cjs' This patch extracts a single `forceWasi` boolean at the top of the generated script (equal to 'true' or 'error') and uses it consistently at the four call sites that previously did a truthy-string check. The existing `=== 'error'` branch at the bottom is preserved unchanged — it already did the right thing. This is a template change, so the fix is picked up the next time a downstream package runs `napi build`. No behavior change for existing users who had NAPI_RS_FORCE_WASI unset or set to 'true'/'error'. Related: PR napi-rs#2919 added the 'error' tri-state; this patch completes the tri-state handling by making the other two values ('true' and everything-else) behave as documented. Signed-off-by: Mukunda Katta <[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 selected for processing (1)
📝 WalkthroughWalkthroughThe PR refines WASI-loading control flow in Changes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes Possibly related PRs
Suggested reviewers
Poem
🚥 Pre-merge checks | ✅ 2 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (2 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 |
…e' (#3519) ## Summary Fixes the `NAPI_RS_FORCE_WASI=false` issue by upgrading `@napi-rs/cli` from `3.5.1` to `3.7.0`. Closes #3267 ## Root Cause In the `native.js` loader generated by `napi build`, the check was: ```js if (!nativeBinding || process.env.NAPI_RS_FORCE_WASI) { ``` In JavaScript, any non-empty string is truthy, so `NAPI_RS_FORCE_WASI=false` (a non-empty string) inadvertently triggered the WASI fallback path. This caused an `ENOENT` error when `lancedb.wasi.cjs` was not present. ## Fix `@napi-rs/[email protected]` ([napi-rs/napi-rs#3236](napi-rs/napi-rs#3236)) introduced a tri-state check in the template that generates `native.js`: **Before (generated by @napi-rs/[email protected]):** ```js if (!nativeBinding || process.env.NAPI_RS_FORCE_WASI) { ``` **After (generated by @napi-rs/[email protected]):** ```js const forceWasi = process.env.NAPI_RS_FORCE_WASI === 'true' || process.env.NAPI_RS_FORCE_WASI === 'error' if (!nativeBinding || forceWasi) { ``` Only the literal string `'true'` (or `'error'` for strict mode) now activates the WASI path. All other values, including `'false'`, `'0'`, or an unset variable, behave as if WASI is not forced. ## Changes - `nodejs/package.json`: bump `@napi-rs/cli` from `3.5.1` to `3.7.0` - `nodejs/package-lock.json` / `nodejs/pnpm-lock.yaml`: update lock files to match The fix is in the upstream napi-rs tool; the generated `native.js` is not committed to this repository and is produced at build time by `napi build`.
Why
The generated
native.jstemplate treatsprocess.env.NAPI_RS_FORCE_WASIas a plain truthy check. In JavaScript every non-empty string is truthy, so settingNAPI_RS_FORCE_WASI=false(intending to opt out) inadvertently triggers the WASI fallback — the same path that tries torequire('./<pkg>.wasi.cjs')and fails withENOENTfor packages that don't ship a WASI binding.Surfaced downstream in lancedb/lancedb#3267:
The existing
=== 'error'branch at line 256 already shows the right pattern — this PR applies it consistently at the other four sites.What
Extracts a single
forceWasiboolean at the top of the generated script:and uses it at all four call sites that previously did the truthy-string check. The
=== 'error'branch at the bottom is preserved unchanged — it already handled its value correctly.Behavior table
NAPI_RS_FORCE_WASI'true''error''false''0'Scope
cli/src/api/templates/js-binding.ts).napi build.NAPI_RS_FORCE_WASIto'true'or'error'are affected.Related
'error'tri-state valueSummary by CodeRabbit
'true'or'error') instead of treating any non-empty string as truthy, resulting in more predictable behavior and clearer error handling.