Skip to content

fix: ensure emnapi version is synced - #3151

Merged
Brooooooklyn merged 4 commits into
napi-rs:mainfrom
sapphi-red:fix/sync-emnapi-version
Mar 27, 2026
Merged

Brooooooklyn merged 4 commits into
napi-rs:mainfrom
sapphi-red:fix/sync-emnapi-version

Conversation

@sapphi-red

@sapphi-red sapphi-red commented Mar 17, 2026 •

Copy link
Copy Markdown
Contributor

A fix suggestion for toyobayashi/emnapi#202

Summary by CodeRabbit

  • New Features

    • Detect emnapi version mismatches during WebAssembly builds to prevent incompatible setups
    • Inject matching emnapi runtime/core dependencies into generated WebAssembly packages
  • Dependencies

    • Bumped emnapi to 1.9.1
    • Moved emnapi runtime/core to peer dependencies for the wasm runtime package
  • Tests

    • Added tests covering version validation and dependency injection for WASM builds

@coderabbitai

coderabbitai Bot commented Mar 17, 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: 01cdcaf1-ff99-4152-8ddb-ec567ef26e2b

📥 Commits

Reviewing files that changed from the base of the PR and between 8222e50 and 2a13f75.

⛔ Files ignored due to path filters (1)
  • yarn.lock is excluded by !**/yarn.lock, !**/*.lock
📒 Files selected for processing (2)
  • cli/package.json
  • cli/src/api/build.ts
✅ Files skipped from review due to trivial changes (1)
  • cli/package.json

📝 Walkthrough

Walkthrough

Adds runtime checks that ensure @emnapi/core and @emnapi/runtime versions match the installed emnapi package during WASM build/package generation, bumps emnapi-related dependency versions to 1.9.1 in several manifests, adds tests for the mismatch behavior, and moves two emnapi deps to peerDependencies in wasm-runtime.

Changes

Cohort / File(s) Summary
CLI package and examples
cli/package.json, examples/napi/package.json
Bumped emnapi and pinned @emnapi/core/@emnapi/runtime versions to 1.9.1.
WASM build & env validation
cli/src/api/build.ts
Added logic in Builder.setWasiEnv() to read the installed emnapi version and the project's @emnapi/core/@emnapi/runtime versions (via a project require), compare them, and throw an error on mismatch.
WASM package generation
cli/src/api/create-npm-dirs.ts
Enabled ESM createRequire and injects @emnapi/core and @emnapi/runtime pinned to the installed emnapi version into generated WASM package.json files.
Tests
cli/src/api/__tests__/build.spec.ts, cli/src/api/__tests__/create-npm-dirs.spec.ts
Added tests: one asserting the build throws on emnapi version mismatch; another asserting generated WASM package.json dependencies match the installed emnapi version.
Runtime package peer deps
wasm-runtime/package.json
Moved @emnapi/core and @emnapi/runtime from dependencies to peerDependencies.

Sequence Diagram(s)

sequenceDiagram
  participant Builder as "Builder\n(setWasiEnv)"
  participant FS as "Project\npackage.json (cwd)"
  participant Emnapi as "Installed\nemnapi/package.json"
  participant CreateDirs as "create-npm-dirs\n(generated package.json)"

  rect rgba(100,150,240,0.5)
    Builder->>Emnapi: read installed emnapi version
    Builder->>FS: read project's `@emnapi/core` & `@emnapi/runtime` versions
  end

  alt versions match
    rect rgba(100,200,120,0.5)
      Builder->>Builder: compare versions (equal)
      Builder->>CreateDirs: proceed, inject pinned deps into generated package.json
      Builder->>Builder: set WASI env vars and link dir
    end
  else mismatch
    rect rgba(240,100,100,0.5)
      Builder->>Builder: detect mismatch
      Builder-->>FS: throw Error("emnapi version mismatch")
    end
  end
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Poem

🐇 I checked the versions, one by one with care,
Pinning cores and runtimes so none would err or tear,
If mismatch pops up, I shout with a squeak,
"Fix the numbers!" — then hop off for a carrot snack. 🥕

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title 'fix: ensure emnapi version is synced' directly aligns with the main objective of the pull request, which is to synchronize emnapi versions across dependencies (@emnapi/core and @emnapi/runtime).
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

✏️ 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.

@sapphi-red
sapphi-red marked this pull request as ready for review March 25, 2026 04:19

@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

🧹 Nitpick comments (1)
cli/src/api/__tests__/build.spec.ts (1)

255-262: Keep the fake emnapi modules' exports empty.

Right now this test still passes if production code reads require('@emnapi/core').version from the module entrypoint, because the stubbed index.js exports version. Leaving the version only in package.json turns this into a real regression test.

Suggested fix
     await writeFile(
       join(pkgDir, 'index.js'),
-      `module.exports = { version: "${fakeVersion}" }`,
+      'module.exports = {}',
     )
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@cli/src/api/__tests__/build.spec.ts` around lines 255 - 262, The test
currently writes an index.js that exports version, which masks regressions;
update the second writeFile call that writes join(pkgDir, 'index.js') so the
module exports are empty (e.g., no version export or an empty module.exports)
while keeping the package.json containing the fakeVersion; modify the write to
create an empty export for the fake emnapi modules referenced by pkg and pkgDir
so production code must read version from package.json rather than the module
entrypoint.
🤖 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/package.json`:
- Around line 69-77: Update package.json peer dependency declarations so WASM
builds resolve correctly: in package.json's "peerDependencies" bump
"@emnapi/runtime" from ^1.7.1 to ^1.9.1 and add "@emnapi/core" (same major range
you expect) to "peerDependencies", and mark "@emnapi/core" as optional via
"peerDependenciesMeta" (e.g. "peerDependenciesMeta": {"@emnapi/core":
{"optional": true}}) so build.ts can resolve both `@emnapi/runtime` and
`@emnapi/core` from the consumer without package managers allowing incompatible
installs.

In `@cli/src/api/build.ts`:
- Around line 573-575: The code currently uses
projectRequire('@emnapi/core').version and
projectRequire('@emnapi/runtime').version and creates the require with
join(this.options.cwd, 'package.json'); change this to
createRequire(resolve(this.options.cwd, 'package.json')) (use resolve from
'path') and read the real package metadata by requiring the packages'
package.json files: projectRequire('@emnapi/core/package.json').version and
projectRequire('@emnapi/runtime/package.json').version so you get the installed
versions instead of undefined.

---

Nitpick comments:
In `@cli/src/api/__tests__/build.spec.ts`:
- Around line 255-262: The test currently writes an index.js that exports
version, which masks regressions; update the second writeFile call that writes
join(pkgDir, 'index.js') so the module exports are empty (e.g., no version
export or an empty module.exports) while keeping the package.json containing the
fakeVersion; modify the write to create an empty export for the fake emnapi
modules referenced by pkg and pkgDir so production code must read version from
package.json rather than the module entrypoint.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: acf5498b-d5be-4959-b327-e65e8614c2f0

📥 Commits

Reviewing files that changed from the base of the PR and between afb6f67 and 8222e50.

⛔ Files ignored due to path filters (1)
  • yarn.lock is excluded by !**/yarn.lock, !**/*.lock
📒 Files selected for processing (7)
  • cli/package.json
  • cli/src/api/__tests__/build.spec.ts
  • cli/src/api/__tests__/create-npm-dirs.spec.ts
  • cli/src/api/build.ts
  • cli/src/api/create-npm-dirs.ts
  • examples/napi/package.json
  • wasm-runtime/package.json

Comment thread cli/package.json Resolved
Comment thread cli/src/api/build.ts Resolved
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.

2 participants