Skip to content

fix(cli): preserve stricter node engine ranges in WASI package generation - #3201

Merged
Brooooooklyn merged 4 commits into
napi-rs:mainfrom
trivikr:fix-engines-node
Apr 10, 2026
Merged

Brooooooklyn merged 4 commits into
napi-rs:mainfrom
trivikr:fix-engines-node

Conversation

@trivikr

@trivikr trivikr commented Apr 7, 2026 •

Copy link
Copy Markdown
Contributor

Fixes: #3200

Summary

This fixes a bug in napi create-npm-dirs for WASI targets where an existing package.json#engines.node range could be silently weakened to >=14.0.0.

The previous implementation used semver.parse() to inspect the engine value. That works for exact versions, but returns null for common valid ranges like >=18 and ^20.0.0. In those cases, the generated npm/wasm32-wasi/package.json fell back to >=14.0.0, even when the source package had a stricter requirement.

Changes

  • Replaced the WASI engine check with semver range-based handling instead of semver.parse().
  • Preserve stricter existing ranges such as >=18.
  • Apply the WASI minimum only when the source range is broader or lower than Node 14.
  • Intersect mixed ranges with the WASI minimum instead of overwriting them.

Summary by CodeRabbit

  • Tests

    • Added test coverage for Node.js engine version handling when producing WASM-targeted packages, covering stricter ranges, mixed OR/AND ranges, missing Node constraints, exact-below-minimum versions, and OR-expression normalization.
  • Refactor

    • More robust engine-range handling for WASM builds: always ensures a scoped Node engine constraint, preserves valid ranges, enforces a minimum (>=14.0.0) when required, and synthesizes safe restricted ranges with a fallback.

@coderabbitai

coderabbitai Bot commented Apr 7, 2026 •

Copy link
Copy Markdown

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: bc65b5ed-a16a-4b94-a9f8-8dd655421dc8

📥 Commits

Reviewing files that changed from the base of the PR and between 31f3512 and fdf153e.

📒 Files selected for processing (2)
  • cli/src/api/__tests__/create-npm-dirs.spec.ts
  • cli/src/api/create-npm-dirs.ts
🚧 Files skipped from review as they are similar to previous changes (1)
  • cli/src/api/tests/create-npm-dirs.spec.ts

📝 Walkthrough

Walkthrough

create-npm-dirs now enforces a WASI minimum Node engine range using precise range-restriction logic instead of coarse major-version checks; helpers compute or inject a restricted engines.node for WASI targets and five AVA tests validate multiple range scenarios for wasm32-wasi-preview1-threads. (34 words)

Changes

Cohort / File(s) Summary
Tests
cli/src/api/__tests__/create-npm-dirs.spec.ts
Added five AVA tests validating createNpmDirs handling of engines.node for the wasm32-wasi-preview1-threads target: preserve stricter ranges, intersect mixed ranges, inject node when missing, replace exact below-minimum versions, and remove below-minimum OR branches.
Implementation
cli/src/api/create-npm-dirs.ts
Replaced coarse semver-major logic with precise range restriction: introduced minimumWasiNodeVersion, always populate scoped engines for WASI targets, added restrictWasiNodeEngine() and normalizeComparatorSet() using semver utilities (subset, Range, Comparator, minVersion) to compute restricted engines.node, and expanded semver imports.

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~45 minutes

Possibly related PRs

Poem

🐰 I nibbled at ranges with tidy feet,
Folded ORs and made minima neat,
When versions would wobble or quietly slide,
I hopped in and kept the WASI gate wide. 🥕

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly describes the main bug fix: preserving stricter node engine ranges in WASI package generation, which is the core objective.
Linked Issues check ✅ Passed The PR implementation directly addresses all coding requirements from issue #3200: preserving stricter ranges, handling semver ranges instead of just parsing, intersecting mixed ranges, and applying WASI minimum appropriately.
Out of Scope Changes check ✅ Passed All changes are scoped to the WASI engine constraint handling in create-npm-dirs.ts and its test cases, with no unrelated modifications.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@cli/src/api/create-npm-dirs.ts`:
- Around line 116-119: The current else branch on scopedPackageJson.engines
overwrites any existing engines object and drops sibling constraints; update the
logic that sets scopedPackageJson.engines (in create-npm-dirs) to preserve
existing engine keys and only set/override the node property to
minimumWasiNodeVersion, merging with any existing scopedPackageJson.engines
(e.g., if engines.npm exists keep it) instead of replacing the whole object.
- Around line 185-187: The exact-match branch returns exactMatch.value without
checking the injected floor; update the logic in the function handling
comparators (the code that finds exactMatch from comparators) to validate that
exactMatch.value satisfies the injected floor (>=14.0.0) and is compatible with
all other comparators before returning it — use the same semver utilities used
elsewhere (e.g., semver.satisfies or Range/intersects) to test exactMatch.value
against the floor and each comparator in comparators, and only return
exactMatch.value when all validations pass; otherwise continue processing or
reject the exact match.
🪄 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: b333771e-3cff-47f9-aa8a-6eb4eab207e0

📥 Commits

Reviewing files that changed from the base of the PR and between 99be12c and 5faaa2f.

📒 Files selected for processing (2)
  • cli/src/api/__tests__/create-npm-dirs.spec.ts
  • cli/src/api/create-npm-dirs.ts

Comment thread cli/src/api/create-npm-dirs.ts Resolved
Comment thread cli/src/api/create-npm-dirs.ts Resolved
@trivikr
trivikr force-pushed the fix-engines-node branch 2 times, most recently from 037199e to 31f3512 Compare April 9, 2026 06:27

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick comments (1)
cli/src/api/create-npm-dirs.ts (1)

199-227: Consider logging suppressed errors for debuggability.

The range restriction logic is solid and correctly handles the various scenarios (preserving stricter ranges, applying the floor for broader ranges, intersecting mixed ranges). However, the silent catch block at lines 222-224 could mask unexpected errors during debugging.

💡 Optional: Add debug logging for suppressed errors
   } catch {
-    // ignore
+    // Malformed range or unexpected error – fall back to minimum
   }

Or if the debug utility is appropriate here:

-  } catch {
-    // ignore
+  } catch (err) {
+    debug('Failed to restrict node engine range %s: %O', nodeRange, err)
   }
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@cli/src/api/create-npm-dirs.ts` around lines 199 - 227, The catch block in
restrictWasiNodeEngine currently swallows exceptions silently; update the catch
to log the caught error (including the error object and context string like
"restrictWasiNodeEngine failed for nodeRange") using the project's logging
utility (e.g., debug(...) or console.error(...)) so you still return
minimumWasiNodeVersion on failure but preserve error details for debugging;
modify the try/catch inside function restrictWasiNodeEngine to log the error
before falling through to the final return.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Nitpick comments:
In `@cli/src/api/create-npm-dirs.ts`:
- Around line 199-227: The catch block in restrictWasiNodeEngine currently
swallows exceptions silently; update the catch to log the caught error
(including the error object and context string like "restrictWasiNodeEngine
failed for nodeRange") using the project's logging utility (e.g., debug(...) or
console.error(...)) so you still return minimumWasiNodeVersion on failure but
preserve error details for debugging; modify the try/catch inside function
restrictWasiNodeEngine to log the error before falling through to the final
return.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 3cfb6157-712b-4a22-b4be-7ca7bb327e2d

📥 Commits

Reviewing files that changed from the base of the PR and between 5faaa2f and 31f3512.

📒 Files selected for processing (2)
  • cli/src/api/__tests__/create-npm-dirs.spec.ts
  • cli/src/api/create-npm-dirs.ts
🚧 Files skipped from review as they are similar to previous changes (1)
  • cli/src/api/tests/create-npm-dirs.spec.ts

@Brooooooklyn
Brooooooklyn merged commit f3f4dae into napi-rs:main Apr 10, 2026
72 checks passed
@trivikr
trivikr deleted the fix-engines-node branch April 10, 2026 15:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

create-npm-dirs can silently weaken a stricter engines.node requirement for WASI packages

2 participants