Skip to content

fix: newPath dir could be unavailable, make sure it's already there#27

Merged
JounQin merged 2 commits intomainfrom
fix/dir
Apr 28, 2025
Merged

fix: newPath dir could be unavailable, make sure it's already there#27
JounQin merged 2 commits intomainfrom
fix/dir

Conversation

@JounQin
Copy link
Member

@JounQin JounQin commented Apr 28, 2025

close unrs/unrs-resolver#108

Without reproduction, I can only guess the root cause.

Summary by CodeRabbit

  • Bug Fixes

    • Fixed an issue where the target directory for certain files might not exist by ensuring it is created before use, preventing related errors.
  • Refactor

    • Simplified path handling by introducing a reusable variable for the node modules directory.
    • Streamlined code for path construction and improved directory management logic.

@JounQin JounQin added the bug Something isn't working label Apr 28, 2025
@JounQin JounQin requested a review from Copilot April 28, 2025 17:43
@JounQin JounQin self-assigned this Apr 28, 2025
@changeset-bot
Copy link

changeset-bot bot commented Apr 28, 2025

🦋 Changeset detected

Latest commit: 649b15a

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 28, 2025

Walkthrough

The changes introduce a patch that ensures the target directory for a downloaded or moved binary exists before writing to it, preventing errors related to missing directories. The installUsingNPM function now uses a dedicated variable for the node_modules path, and the checkAndPreparePackage function adds a nested resolution check for already patched binaries. Additionally, a minor code simplification is made in the downloadedNodePath helper. These updates are documented in a new changeset file.

Changes

File(s) Change Summary
.changeset/eight-balloons-relax.md Added a changeset documenting a patch fix for ensuring target directory existence before writing binaries.
src/index.ts Refactored to reuse nodeModulesDir path, added nested binary resolution check, ensured directory exists.
src/helpers.ts Simplified argument to path.join in downloadedNodePath by removing unnecessary template literal syntax.

Assessment against linked issues

Objective Addressed Explanation
Prevent install failures on Alpine/MUSL by ensuring target directory exists before writing (108)

Poem

A patch for the path, so binaries may thrive,
No more missing folders, your builds will survive!
With checks and with mkdir, the install is robust,
On Alpine or MUSL, proceed if you must.
🐇✨
Hop along, dear coder, your troubles are done—
For directories missing, we now make each one!

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)


📜 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 10b00af and 649b15a.

📒 Files selected for processing (3)
  • .changeset/eight-balloons-relax.md (1 hunks)
  • src/helpers.ts (1 hunks)
  • src/index.ts (4 hunks)
🧰 Additional context used
🪛 GitHub Check: codecov/patch
src/helpers.ts

[warning] 40-40: src/helpers.ts#L40
Added line #L40 was not covered by tests

src/index.ts

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


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


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


[warning] 331-333: src/index.ts#L331-L333
Added lines #L331 - L333 were not covered by tests


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

⏰ Context from checks skipped due to timeout of 90000ms (4)
  • 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
  • GitHub Check: Lint and Test with Node.js 22 on windows-latest
🔇 Additional comments (5)
src/helpers.ts (1)

40-40:

Details

✅ Verification successful

Function simplification looks good.

The change removes unnecessary template literals around path.basename(subpath), which is a clean simplification that maintains the same behavior while making the code more readable.


🏁 Script executed:

#!/bin/bash
# Description: Check if the change impacts function behavior by examining potential callers

# Find where this function is called
rg -A 3 "downloadedNodePath\(" --type=ts

Length of output: 550


Approve downloadedNodePath simplification

Verified that downloadedNodePath is only called in src/index.ts and the return value remains a string as before—no behavior change.

• Caller:

  • src/index.ts: nodePath = downloadedNodePath(name, subpath)
🧰 Tools
🪛 GitHub Check: codecov/patch

[warning] 40-40: src/helpers.ts#L40
Added line #L40 was not covered by tests

src/index.ts (3)

142-143: Good variable extraction for code reuse.

Extracting nodeModulesDir avoids duplicating the path construction logic and makes the code more maintainable. This is a solid refactoring that improves code readability.

🧰 Tools
🪛 GitHub Check: codecov/patch

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


204-204: Using the extracted variable correctly.

This change appropriately uses the newly created nodeModulesDir variable, maintaining consistency across the codebase.

🧰 Tools
🪛 GitHub Check: codecov/patch

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


329-333: Good enhancement for already patched binaries.

This addition helps detect if the binary has already been patched, potentially avoiding redundant operations. This is a thoughtful optimization that improves the overall robustness of the package resolution process.

🧰 Tools
🪛 GitHub Check: codecov/patch

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


[warning] 331-333: src/index.ts#L331-L333
Added lines #L331 - L333 were not covered by tests

.changeset/eight-balloons-relax.md (1)

1-5: Appropriate changeset for the fix.

The changeset correctly documents this as a patch-level fix and accurately describes the issue being addressed. This will help users understand the changes when a new version is released.

✨ 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 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 addresses an issue where the destination directory for renaming modules might not exist by ensuring the directory is created before the rename operation. Key changes include reordering try blocks in installUsingNPM, explicitly creating the target directory via fs.mkdirSync, and a minor cleanup in helpers.ts by removing an unnecessary template literal.

Reviewed Changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
src/index.ts Reorders try blocks, creates the destination directory before renaming binary executable
src/helpers.ts Removes redundant template literal usage when joining paths

@pkg-pr-new
Copy link

pkg-pr-new bot commented Apr 28, 2025

Open in StackBlitz

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

commit: 649b15a

@github-actions
Copy link
Contributor

Deploy preview for napi-postinstall ready!

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

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

@github-actions
Copy link
Contributor

size-limit report 📦

Path Size
lib/index.js 2.76 KB (+0.65% 🔺)

@github-actions
Copy link
Contributor

📊 Package size report   0.7%↑

File Before After
lib/helpers.js 7.4 kB -0.07%↓7.4 kB
lib/helpers.js.map 6.6 kB -0.15%↓6.5 kB
lib/index.js 10.2 kB 2%↑10.4 kB
lib/index.js.map 8.7 kB 2%↑8.9 kB
Total (Includes all files) 51.5 kB 0.7%↑51.9 kB
Tarball size 14.2 kB 0.3%↑14.3 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 412 B
lib/constants.js 703 B
lib/constants.js.map 519 B
lib/helpers.d.ts 841 B
lib/index.d.ts 415 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

@codecov
Copy link

codecov bot commented Apr 28, 2025

Codecov Report

Attention: Patch coverage is 0% with 8 lines in your changes missing coverage. Please review.

Project coverage is 34.41%. Comparing base (10b00af) to head (649b15a).
Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
src/index.ts 0.00% 7 Missing ⚠️
src/helpers.ts 0.00% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main      #27      +/-   ##
==========================================
- Coverage   34.52%   34.41%   -0.12%     
==========================================
  Files           6        6              
  Lines         585      587       +2     
  Branches       57       59       +2     
==========================================
  Hits          202      202              
- Misses        383      385       +2     

☔ 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.

@JounQin JounQin merged commit f52b3e9 into main Apr 28, 2025
28 of 30 checks passed
@JounQin JounQin deleted the fix/dir branch April 28, 2025 17:48
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.

Install failure on Alpine/MUSL in latest version.

2 participants