Skip to content

fix: allow optional optionalDependencies for development#6

Merged
JounQin merged 1 commit intomainfrom
fix/optionalDependencies
Apr 19, 2025
Merged

fix: allow optional optionalDependencies for development#6
JounQin merged 1 commit intomainfrom
fix/optionalDependencies

Conversation

@JounQin
Copy link
Member

@JounQin JounQin commented Apr 19, 2025

the optionalDependencies field is auto generated on build

Summary by CodeRabbit

  • Bug Fixes

    • Improved handling of optional dependencies, allowing them to be truly optional during development.
    • Enhanced version consistency checks to ensure correct N-API version usage when installing native binaries.
    • Updated error messages for clearer guidance when required fields are missing.
  • Tests

    • Adjusted test assertions to reflect updated error messages and logic for missing fields.
  • Chores

    • Added documentation for the latest patch update.

@JounQin JounQin requested a review from Copilot April 19, 2025 20:51
@changeset-bot
Copy link

changeset-bot bot commented Apr 19, 2025

🦋 Changeset detected

Latest commit: 71d9533

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
napi-postinstall Patch

Not sure what this means? Click here to learn what changesets are.

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

@coderabbitai
Copy link

coderabbitai bot commented Apr 19, 2025

Walkthrough

This update modifies the handling of optionalDependencies in the napi-postinstall package. The internal logic was adjusted so that optionalDependencies are no longer required for development, and the code now handles their absence gracefully. Error messages and checks were updated to reflect this new behavior. The version used for native binary installation was also aligned to use the specific N-API version. Corresponding tests and documentation were updated to match these changes, and a new changeset was added to document the patch.

Changes

File(s) Change Summary
.changeset/ten-ways-crash.md Added a changeset documenting a patch for handling optionalDependencies as optional during development.
lib/helpers.js, src/helpers.ts, lib/helpers.d.ts Modified getNapiInfoFromPackageJson to remove the requirement for optionalDependencies, update error messages, set default fields earlier, return early if optionalDependencies is absent, and updated function signatures to reflect version property optionality based on checkVersion.
lib/index.js, src/index.ts Updated version consistency checks to only compare if checkVersion is true and napiVersion differs, added safe handling for missing optionalDependencies using optional chaining, and changed the version argument for native binary installation to use napiVersion.
lib/types.d.ts, src/types.ts Changed version property in NapiInfo interface from required to optional.
test/basic.spec.ts Updated test assertions to match new error messages and logic regarding napi.targets and napi.triples.additional.

Sequence Diagram(s)

sequenceDiagram
    participant Dev as Developer
    participant NPI as napi-postinstall
    participant NPM as npm Registry

    Dev->>NPI: Run checkAndPreparePackage()
    NPI->>NPI: Read package.json
    alt napi.targets or napi.triples.additional missing
        NPI-->>Dev: Throw error (missing fields)
    else optionalDependencies missing
        NPI-->>Dev: Return napi info (no error)
    else All fields present
        NPI->>NPI: Check optionalDependencies versions
        alt Version mismatch or missing
            NPI-->>Dev: Throw error
        else
            NPI->>NPM: Install native binary using napiVersion
            NPM-->>NPI: Provide binary
            NPI-->>Dev: Success
        end
    end
Loading

Poem

🐇
A patch arrives, so neat and small,
No longer must dependencies stall.
If optional ones are not in sight,
The code hops on, all is right!
With version checks now clear and bright,
The tests all pass—what sheer delight!
Hooray for fixes, swift and light!

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.

lib/types.d.ts

Oops! Something went wrong! :(

ESLint: 9.24.0

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/helpers.ts

Oops! Something went wrong! :(

ESLint: 9.24.0

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)

lib/helpers.d.ts

Oops! Something went wrong! :(

ESLint: 9.24.0

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)

  • 5 others

📜 Recent review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 71d9533 and 83ee2ef.

⛔ Files ignored due to path filters (2)
  • lib/helpers.js.map is excluded by !**/*.map
  • lib/index.js.map is excluded by !**/*.map
📒 Files selected for processing (9)
  • .changeset/ten-ways-crash.md (1 hunks)
  • lib/helpers.d.ts (1 hunks)
  • lib/helpers.js (1 hunks)
  • lib/index.js (2 hunks)
  • lib/types.d.ts (1 hunks)
  • src/helpers.ts (2 hunks)
  • src/index.ts (4 hunks)
  • src/types.ts (1 hunks)
  • test/basic.spec.ts (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (6)
  • .changeset/ten-ways-crash.md
  • test/basic.spec.ts
  • lib/index.js
  • src/index.ts
  • src/helpers.ts
  • lib/helpers.js
⏰ Context from checks skipped due to timeout of 90000ms (9)
  • GitHub Check: Lint and Test with Node.js 22 on windows-latest
  • GitHub Check: Lint and Test with Node.js 20 on windows-latest
  • GitHub Check: Lint and Test with Node.js 22 on macos-latest
  • GitHub Check: Lint and Test with Node.js 20 on macos-latest
  • GitHub Check: Lint and Test with Node.js 18 on macos-latest
  • GitHub Check: Lint and Test with Node.js 18 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
  • GitHub Check: Lint and Test with Node.js 18 on windows-latest
🔇 Additional comments (3)
src/types.ts (1)

25-25: LGTM: Making version optional in NapiInfo interface

This change correctly makes the version property optional in the NapiInfo interface, aligning with the PR objective to make certain properties optional during development. This modification matches the changes in related declaration files and supports the updated behavior in helper functions.

lib/types.d.ts (1)

21-21: LGTM: Consistent type definition update

Making the version property optional in this declaration file is consistent with the change in src/types.ts. This ensures type safety for JavaScript consumers of the library.

lib/helpers.d.ts (1)

5-8: LGTM: Improved type safety with function overloading

The updated function overloads provide better type safety by:

  1. Guaranteeing a required version property when checkVersion is explicitly true
  2. Using the default NapiInfo interface with optional version when checkVersion is omitted or false

This change correctly aligns with the modifications to NapiInfo in both type files and provides a more precise contract for consumers of this API.

✨ 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 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 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 modifies version and dependency handling to support auto‐generated optionalDependencies during development while improving error messages for missing NAPI support fields.

  • Updates error messages to reference "napi.triples.additional" instead of "optionalDependencies"
  • Revises version comparison logic and uses optional chaining for optionalDependencies
  • Adjusts helper functions to set sensible defaults for napi.binaryName and napi.packageName

Reviewed Changes

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

Show a summary per file
File Description
test/basic.spec.ts Error message updated in snapshot to require "napi.triples.additional" instead of the old field.
src/index.ts Conditional version check and safe access to optionalDependencies have been improved.
src/helpers.ts Refactored the early dependency check and initialized defaults for binaryName/packageName.
lib/index.js Mirrors the changes in src/index.ts regarding version check and dependency access.
lib/helpers.js Updated error message and default value assignments to align with the new field naming.
Comments suppressed due to low confidence (3)

test/basic.spec.ts:23

  • Ensure the updated error message clearly reflects the new required field 'napi.triples.additional' and update any related documentation or tests accordingly.
[Error: No `napi.targets` nor `napi.triples.additional` field found in `napi-postinstall`'s `package.json`. Please ensure the package is built with NAPI support.]

lib/helpers.js:55

  • Verify that the error message now consistently references 'napi.triples.additional' and that any related test expectations are updated accordingly.
throw new Error(`No `napi.targets` nor `napi.triples.additional` field found in `${name}`'s `package.json`. Please ensure the package is built with NAPI support.`);

src/index.ts:197

  • Confirm that skipping the version consistency check when napiVersion is undefined is intended; if not, consider handling this scenario explicitly.
if (checkVersion && napiVersion && version !== napiVersion) {

@github-actions
Copy link
Contributor

github-actions bot commented Apr 19, 2025

📊 Package size report   0.5%↑

File Before After
lib/helpers.d.ts 686 B -0.15%↓685 B
lib/helpers.js 7.8 kB 0.4%↑7.8 kB
lib/helpers.js.map 6.3 kB 0.9%↑6.4 kB
lib/index.js 7.1 kB 1%↑7.2 kB
lib/index.js.map 5.9 kB 0.8%↑5.9 kB
lib/types.d.ts 540 B 0.2%↑541 B
Total (Includes all files) 44.5 kB 0.5%↑44.8 kB
Tarball size 12.8 kB 0.6%↑12.8 kB
Unchanged files
File Size
lib/cli.d.ts 31 B
lib/cli.js 289 B
lib/cli.js.map 282 B
lib/constants.d.ts 168 B
lib/constants.js 567 B
lib/constants.js.map 307 B
lib/index.d.ts 249 B
lib/target.d.ts 348 B
lib/target.js 1.5 kB
lib/target.js.map 1.5 kB
lib/types.js 110 B
lib/types.js.map 102 B
LICENSE 1.1 kB
package.json 3.4 kB
README.md 6.3 kB

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

@github-actions
Copy link
Contributor

github-actions bot commented Apr 19, 2025

size-limit report 📦

Path Size
lib/index.js 2.18 KB (+0.82% 🔺)

@pkg-pr-new
Copy link

pkg-pr-new bot commented Apr 19, 2025

Open in StackBlitz

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

commit: 83ee2ef

@github-actions
Copy link
Contributor

github-actions bot commented Apr 19, 2025

Deploy preview for napi-postinstall ready!

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

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

the `optionalDependencies` field is auto generated on build
@JounQin JounQin force-pushed the fix/optionalDependencies branch from 71d9533 to 83ee2ef Compare April 19, 2025 20:59
@JounQin JounQin merged commit c56e93a into main Apr 19, 2025
28 checks passed
@JounQin JounQin deleted the fix/optionalDependencies branch April 19, 2025 21:10
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