Skip to content

Conversation

JounQin
Copy link
Member

@JounQin JounQin commented Apr 26, 2025

Summary by CodeRabbit

  • Bug Fixes

    • Improved compatibility for WebAssembly (WASI) environments by ensuring consistent target selection and handling during installation and setup.
    • Adjusted installation process and file management for WASI targets, including proper handling of package directories and binary file extensions.
  • Chores

    • Increased the allowed size limit for the main library file to accommodate recent changes.

@JounQin JounQin added the bug Something isn't working label Apr 26, 2025
@JounQin JounQin requested a review from Copilot April 26, 2025 14:07
@JounQin JounQin self-assigned this Apr 26, 2025
Copy link

changeset-bot bot commented Apr 26, 2025

⚠️ No Changeset found

Latest commit: 4634d06

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

Copy link

coderabbitai bot commented Apr 26, 2025

Walkthrough

This update introduces specialized handling for the wasm32-wasi target across the codebase. The helper function for determining native targets now returns only 'wasm32-wasi' when running inside a webcontainer environment. The installation logic in installUsingNPM is updated to use specific npm flags and directory management for this target, including recursive movement of installed packages and cleanup of temporary files. Additionally, the expected binary extension changes to .wasm for this target. Configuration files are updated to reflect these changes, including a new changeset and an increased size limit.

Changes

File(s) Summary
.changeset/fair-pillows-divide.md Adds a changeset documenting a patch update to ensure webcontainer always uses the wasm32-wasi target.
.size-limit.json Increases the size limit for ./lib/index.js from 2.5kB to 2.6kB.
src/helpers.ts Updates getNapiNativeTargets to return only 'wasm32-wasi' in webcontainer environments.
src/index.ts Modifies installUsingNPM and checkAndPreparePackage for wasm32-wasi: new npm flags, directory handling, and .wasm binary extension.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant MainModule as src/index.ts
    participant Helpers as src/helpers.ts
    participant NPM
    participant FS as File System

    User->>MainModule: Initiate installUsingNPM(target)
    MainModule->>Helpers: getNapiNativeTargets()
    Helpers-->>MainModule: Return ['wasm32-wasi'] if webcontainer, else targets
    alt Target is wasm32-wasi
        MainModule->>NPM: npm install with --cpu=wasm32 --force
        MainModule->>FS: Move all temp node_modules to parent node_modules
        MainModule->>FS: Remove temp package.json
        MainModule->>FS: Use .wasm extension for binary
    else Other targets
        MainModule->>NPM: npm install (standard flags)
        MainModule->>FS: Move specific package dir to parent node_modules
        MainModule->>FS: Use .node extension for binary
    end
Loading

Possibly related PRs

  • un-ts/napi-postinstall#18: Adds initial support for wasm32-wasi installation with the --cpu=wasm32 flag, which is extended and refined in this PR.

Poem

In the land where WASI hops,
A rabbit tweaks and never stops.
Webcontainers now know their fate—
Only WASI, no debate!
With .wasm files and npm’s might,
Installations now work just right.
🐇✨

Warning

There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure.

🔧 ESLint

If the error stems from missing dependencies, add them to the package.json file. For unrecoverable errors (e.g., due to private dependencies), disable the tool in the CodeRabbit configuration.

src/helpers.ts

Oops! Something went wrong! :(

ESLint: 9.25.1

Error [ERR_MODULE_NOT_FOUND]: Cannot find package '@1stg/eslint-config' imported from /eslint.config.mjs
at packageResolve (node:internal/modules/esm/resolve:839:9)
at moduleResolve (node:internal/modules/esm/resolve:908:18)
at defaultResolve (node:internal/modules/esm/resolve:1038:11)
at ModuleLoader.defaultResolve (node:internal/modules/esm/loader:557:12)
at ModuleLoader.resolve (node:internal/modules/esm/loader:525:25)
at ModuleLoader.getModuleJob (node:internal/modules/esm/loader:246:38)
at ModuleJob._link (node:internal/modules/esm/module_job:126:49)

src/index.ts

Oops! Something went wrong! :(

ESLint: 9.25.1

Error [ERR_MODULE_NOT_FOUND]: Cannot find package '@1stg/eslint-config' imported from /eslint.config.mjs
at packageResolve (node:internal/modules/esm/resolve:839:9)
at moduleResolve (node:internal/modules/esm/resolve:908:18)
at defaultResolve (node:internal/modules/esm/resolve:1038:11)
at ModuleLoader.defaultResolve (node:internal/modules/esm/loader:557:12)
at ModuleLoader.resolve (node:internal/modules/esm/loader:525:25)
at ModuleLoader.getModuleJob (node:internal/modules/esm/loader:246:38)
at ModuleJob._link (node:internal/modules/esm/module_job:126:49)

✨ Finishing Touches
  • 📝 Generate Docstrings

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
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR fixes the target selection for the webcontainer by ensuring that the wasm32-wasi target is always used, and updates related npm installation commands and subpath resolution.

  • Update npm install flags and file handling to support wasm32-wasi
  • Adjust subpath construction to return the proper file extension based on the target
  • Add a webcontainer check in getNapiNativeTargets to force the WASI target

Reviewed Changes

Copilot reviewed 2 out of 3 changed files in this pull request and generated no comments.

File Description
src/index.ts Updates to npm install command, package.json handling, and subpath resolution for wasm32-wasi support
src/helpers.ts Introduces a webcontainer check to enforce WASI target usage
Files not reviewed (1)
  • .size-limit.json: Language not supported
Comments suppressed due to low confidence (2)

src/index.ts:298

  • Consider adding tests to verify the correct subpath resolution for both wasm and node targets, ensuring that the filename extension adjusts appropriately based on the target.
const subpath = `${binaryPrefix}${target}.${target === WASM32_WASI ? 'wasm' : 'node'}`

src/helpers.ts:258

  • Ensure adequate test coverage is in place for scenarios where process.versions.webcontainer is defined so that the function correctly returns the [WASI_TARGET] array.
if (process.versions.webcontainer) {

Copy link

pkg-pr-new bot commented Apr 26, 2025

Open in StackBlitz

npm i https://pkg.pr.new/napi-postinstall@23

commit: c893d10

Copy link
Contributor

Deploy preview for napi-postinstall ready!

✅ Preview
https://napi-postinstall-l8tcxrbhr-1stg.vercel.app

Built with commit c893d10.
This pull request is being automatically deployed with vercel-action

Copy link
Contributor

size-limit report 📦

Path Size
lib/index.js 2.49 KB (+11.75% 🔺)

Copy link
Contributor

📊 Package size report   7%↑

File Before After
lib/helpers.js 7.0 kB 1%↑7.1 kB
lib/helpers.js.map 6.1 kB 1%↑6.2 kB
lib/index.js 7.2 kB 22%↑8.7 kB
lib/index.js.map 6.5 kB 19%↑7.7 kB
Total (Includes all files) 45.2 kB 7%↑48.1 kB
Tarball size 13.0 kB 4%↑13.5 kB
Unchanged files
File Size
lib/cli.d.ts 31 B
lib/cli.js 306 B
lib/cli.js.map 317 B
lib/constants.d.ts 382 B
lib/constants.js 749 B
lib/constants.js.map 479 B
lib/helpers.d.ts 685 B
lib/index.d.ts 361 B
lib/target.d.ts 101 B
lib/target.js 1.6 kB
lib/target.js.map 1.5 kB
lib/types.d.ts 825 B
lib/types.js 110 B
lib/types.js.map 102 B
LICENSE 1.1 kB
package.json 3.4 kB
README.md 6.4 kB

🤖 This report was automatically generated by pkg-size-action

Copy link

codecov bot commented Apr 26, 2025

Codecov Report

Attention: Patch coverage is 3.84615% with 50 lines in your changes missing coverage. Please review.

Project coverage is 36.34%. Comparing base (ca5a744) to head (c893d10).
Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
src/index.ts 2.04% 48 Missing ⚠️
src/helpers.ts 33.33% 2 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main      #23      +/-   ##
==========================================
- Coverage   39.02%   36.34%   -2.68%     
==========================================
  Files           6        6              
  Lines         492      531      +39     
  Branches       53       55       +2     
==========================================
+ Hits          192      193       +1     
- Misses        300      338      +38     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🧹 Nitpick comments (3)
src/helpers.ts (1)

258-260: Add unit tests for the new webcontainer fast-path

getNapiNativeTargets() now returns early when process.versions.webcontainer is truthy, but the added branch is not covered by tests (see Codecov warning). A trivial unit test can stub process.versions.webcontainer and assert that only ['wasm32-wasi'] is returned. Verifying this guards against future refactors that might inadvertently break the WebContainer experience.

🧰 Tools
🪛 GitHub Check: codecov/patch

[warning] 259-260: src/helpers.ts#L259-L260
Added lines #L259 - L260 were not covered by tests

src/index.ts (2)

84-88: Nit: local boolean name gives stronger intent

const isWasm32Wasi = target === WASM32_WASI is clear, but because you branch on this flag ~40 lines later it’s easy to forget the semantic. A slightly more specific name (targetIsWasi) improves readability for future readers skimming the file.

-const isWasm32Wasi = target === WASM32_WASI
+const targetIsWasi32 = target === WASM32_WASI
🧰 Tools
🪛 GitHub Check: codecov/patch

[warning] 86-86: src/index.ts#L86
Added line #L86 was not covered by tests


122-157: Directory shuffle can be replaced with one atomic move

The loop iterates over every package and attempts fs.renameSync, swallowing EEXIST. Two drawbacks:

• Performance: rename is invoked O(number of packages).
• Partial state: if the process dies midway the install may be left half-moved.

Consider a simpler and atomic alternative:

-if (isWasm32Wasi) {
-  const newNodeModulesDir = path.resolve(installDir, '../node_modules')
-  fs.rmSync(newNodeModulesDir, { recursive: true, force: true })
-  fs.renameSync(nodeModulesDir, newNodeModulesDir)
-} else {
+if (!isWasm32Wasi) {
   // existing single-package move

fs.renameSync on the whole node_modules tree is fast (it just patches directory entries) and either succeeds entirely or leaves the original tree untouched.

🧰 Tools
🪛 GitHub Check: codecov/patch

[warning] 122-126: src/index.ts#L122-L126
Added lines #L122 - L126 were not covered by tests


[warning] 129-140: src/index.ts#L129-L140
Added lines #L129 - L140 were not covered by tests


[warning] 143-151: src/index.ts#L143-L151
Added lines #L143 - L151 were not covered by tests


[warning] 154-157: src/index.ts#L154-L157
Added lines #L154 - L157 were not covered by tests

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between ca5a744 and c893d10.

📒 Files selected for processing (4)
  • .changeset/fair-pillows-divide.md (1 hunks)
  • .size-limit.json (1 hunks)
  • src/helpers.ts (1 hunks)
  • src/index.ts (5 hunks)
🧰 Additional context used
🧬 Code Graph Analysis (1)
src/index.ts (1)
src/constants.ts (3)
  • WASM32_WASI (15-15)
  • PACKAGE_JSON (19-19)
  • WASM32 (13-13)
🪛 GitHub Check: codecov/patch
src/helpers.ts

[warning] 259-260: src/helpers.ts#L259-L260
Added lines #L259 - L260 were not covered by tests

src/index.ts

[warning] 86-86: src/index.ts#L86
Added line #L86 was not covered by tests


[warning] 99-99: src/index.ts#L99
Added line #L99 was not covered by tests


[warning] 101-101: src/index.ts#L101
Added line #L101 was not covered by tests


[warning] 111-111: src/index.ts#L111
Added line #L111 was not covered by tests


[warning] 117-119: src/index.ts#L117-L119
Added lines #L117 - L119 were not covered by tests


[warning] 122-126: src/index.ts#L122-L126
Added lines #L122 - L126 were not covered by tests


[warning] 129-140: src/index.ts#L129-L140
Added lines #L129 - L140 were not covered by tests


[warning] 143-151: src/index.ts#L143-L151
Added lines #L143 - L151 were not covered by tests


[warning] 154-157: src/index.ts#L154-L157
Added lines #L154 - L157 were not covered by tests


[warning] 167-177: src/index.ts#L167-L177
Added lines #L167 - L177 were not covered by tests

⏰ Context from checks skipped due to timeout of 90000ms (4)
  • GitHub Check: Lint and Test with Node.js 20 on windows-latest
  • GitHub Check: Lint and Test with Node.js 22 on windows-latest
  • GitHub Check: Lint and Test with Node.js 22 on windows-latest
  • GitHub Check: Lint and Test with Node.js 20 on windows-latest
🔇 Additional comments (3)
src/index.ts (1)

297-299: Confirm .wasm resolution works with require.resolve

require.resolve() is used on *.wasm. Node ≥ 16 resolves .wasm automatically only when run with --experimental-wasm-modules; otherwise it must be imported via fs.readFileSync/ WebAssembly.instantiate. Ensure:

  1. The runtime actually loads the path via fs/URL rather than require(), or
  2. The project sets "type": "module" and the proper loader flag, or
  3. A custom loader/hook is in place.

If none of the above is true, this line will resolve the path but later require() will throw “Unexpected file extension”.

.size-limit.json (1)

4-4: Size-limit bump looks fine

The increase from 2.5 kB → 2.6 kB matches the extra WASI code paths; nothing else to flag.

.changeset/fair-pillows-divide.md (1)

1-6: Changelog entry LGTM

Clear, concise description of the behavioural change.

@JounQin JounQin merged commit f14cd3f into main Apr 26, 2025
28 of 30 checks passed
@JounQin JounQin deleted the fix/webcontainer branch April 26, 2025 14:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant