Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: use importResolve to get framework path #31

Merged
merged 1 commit into from
Dec 27, 2024
Merged

Conversation

fengmk2
Copy link
Member

@fengmk2 fengmk2 commented Dec 27, 2024

Summary by CodeRabbit

  • New Features

    • Introduced new module exports for both CommonJS and ESM formats, enhancing export capabilities.
    • Added debugging capabilities for improved error handling in module resolution.
  • Bug Fixes

    • Enhanced error handling in the importResolve function to provide more context during module resolution failures.
  • Documentation

    • Updated test suite to improve coverage for module resolution tests.
  • Chores

    • Updated dependency versions in package.json files for various modules.

Copy link

vercel bot commented Dec 27, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
egg-utils ✅ Ready (Inspect) Visit Preview 💬 Add feedback Dec 27, 2024 3:34pm

Copy link

coderabbitai bot commented Dec 27, 2024

Walkthrough

This pull request introduces modifications to the @eggjs/utils package, focusing on dependency management, module resolution, and testing. The changes include updating package dependencies, enhancing the importResolve function with improved error handling and debugging capabilities, and expanding test coverage for module resolution across CommonJS and ESM formats. The modifications aim to improve the package's flexibility and robustness in handling module imports and resolving package paths.

Changes

File Change Summary
package.json - Added @eggjs/bin dependency (v^7.0.0)
- Removed egg-bin dependency
src/framework.ts - Added importResolve import
- Replaced require.resolve with importResolve
- Introduced debug logging for error handling
src/import.ts - Enhanced error handling in importResolve
- Added debug logging for module resolution
test/fixtures/cjs-index/index.cjs - Added multiple module exports
test/fixtures/cjs-index/package.json - Added "type": "commonjs"
test/fixtures/egg-app/package.json - Updated @eggjs/core dependency to "beta"
test/fixtures/esm-index/index.mjs - Added default and named exports
test/fixtures/esm-index/package.json - Added "type": "module"
test/import.test.ts - Added new test cases for importResolve
- Expanded module resolution testing

Sequence Diagram

sequenceDiagram
    participant Caller
    participant importResolve
    participant FileSystem
    participant DebugLogger

    Caller->>importResolve: Resolve module path
    importResolve->>DebugLogger: Log resolution attempt
    importResolve->>FileSystem: Check module file status
    alt Module file exists
        FileSystem-->>importResolve: File found
        importResolve-->>Caller: Return resolved path
    else Module file not found
        FileSystem-->>importResolve: No file
        importResolve->>DebugLogger: Log error details
        importResolve->>Caller: Throw TypeError
    end
Loading

Possibly Related PRs

Poem

🐰 Hop, skip, and a module's leap,
Resolving paths with debug so deep,
From CommonJS to ESM's embrace,
Our imports dance with newfound grace,
CodeRabbit's magic, clean and neat! 🔍


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ 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. (Beta)
  • @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

pkg-pr-new bot commented Dec 27, 2024

Open in Stackblitz

npm i https://pkg.pr.new/eggjs/utils/@eggjs/utils@31

commit: 474f6d4

Copy link

New and removed dependencies detected. Learn more about Socket for GitHub ↗︎

Package New capabilities Transitives Size Publisher
npm/@eggjs/[email protected] environment Transitive: eval, filesystem, shell, unsafe +136 16.9 MB fengmk2
npm/@eggjs/[email protected] environment Transitive: eval, filesystem, network, unsafe +92 4.2 MB fengmk2

View full report↗︎

Copy link

codecov bot commented Dec 27, 2024

Codecov Report

Attention: Patch coverage is 90.00000% with 1 line in your changes missing coverage. Please review.

Project coverage is 89.44%. Comparing base (4605e3a) to head (474f6d4).
Report is 2 commits behind head on master.

Files with missing lines Patch % Lines
src/framework.ts 85.71% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master      #31      +/-   ##
==========================================
+ Coverage   88.34%   89.44%   +1.10%     
==========================================
  Files           6        6              
  Lines         609      616       +7     
  Branches      100      104       +4     
==========================================
+ Hits          538      551      +13     
+ Misses         71       65       -6     

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

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 (1)
test/import.test.ts (1)

14-18: Enhance test coverage for negative scenarios
This approach thoroughly tests a CommonJS module under coffee.fork. Consider adding a test case that verifies behavior when the module fails or is missing to improve robustness.

📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between a625246 and 474f6d4.

📒 Files selected for processing (12)
  • package.json (1 hunks)
  • src/framework.ts (2 hunks)
  • src/import.ts (1 hunks)
  • test/fixtures/cjs-index/index.cjs (1 hunks)
  • test/fixtures/cjs-index/package.json (1 hunks)
  • test/fixtures/cjs/run.js (1 hunks)
  • test/fixtures/egg-app/config/plugin.test.js (0 hunks)
  • test/fixtures/egg-app/get_loadunit.js (1 hunks)
  • test/fixtures/egg-app/package.json (1 hunks)
  • test/fixtures/esm-index/index.mjs (1 hunks)
  • test/fixtures/esm-index/package.json (1 hunks)
  • test/import.test.ts (2 hunks)
💤 Files with no reviewable changes (1)
  • test/fixtures/egg-app/config/plugin.test.js
✅ Files skipped from review due to trivial changes (4)
  • test/fixtures/cjs-index/package.json
  • test/fixtures/esm-index/package.json
  • test/fixtures/esm-index/index.mjs
  • test/fixtures/egg-app/get_loadunit.js
🔇 Additional comments (13)
test/fixtures/egg-app/package.json (1)

5-5: LGTM! Verify beta tag stability.

The change to use the "beta" tag for @eggjs/core aligns with the egg dependency version and is appropriate for test fixtures. However, ensure that the beta versions are stable enough for testing purposes.

Let's verify the beta tags:

✅ Verification successful

Beta tags are stable and aligned with major version transitions

The beta versions are stable development releases:

  • @eggjs/core beta (6.3.0-beta.0) is a minor version bump from the latest stable (6.2.4)
  • egg beta (4.0.0-beta.7) represents the upcoming v4 major release with multiple beta iterations

Using these beta versions in test fixtures is appropriate as they:

  1. Follow semantic versioning conventions
  2. Are official pre-releases from the maintainers
  3. Have consistent version alignment between dependencies
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Check the available versions and beta tags for @eggjs/core and egg

# Check available versions for @eggjs/core
echo "Available versions for @eggjs/core:"
curl -s https://registry.npmjs.org/@eggjs/core | jq -r '.["dist-tags"]'

# Check available versions for egg
echo -e "\nAvailable versions for egg:"
curl -s https://registry.npmjs.org/egg | jq -r '.["dist-tags"]'

Length of output: 487

test/fixtures/cjs-index/index.cjs (2)

1-3: Looks good for CommonJS exports.
Exporting an object with a foo: 'bar' property is straightforward and aligns with CommonJS usage.


5-5: Adding extra property on module.exports.
Appending one = 1 directly to module.exports is a clean approach.

test/fixtures/cjs/run.js (2)

1-1: Verify the relative import path.
Using require('../../../') might break if the directory structure changes. Confirm that this relative path is correct and robust in the broader project context.


3-5: Debug log usage.
Logging the result of importResolve using %o is suitable for object debugging. No issues found.

src/framework.ts (4)

1-1: Use of debuglog for targeted debugging.
Importing debuglog from node:util is a well-structured approach to conditionally enable verbose logs.


6-7: Integration of importResolve.
Replacing require.resolve with the more flexible importResolve is a solid improvement for advanced module resolution scenarios.


73-78: Handling scoped and unscoped packages differently.
Using importResolve(${frameworkName}/package.json) and adjusting how far to walk up the directory tree constitutes a targeted approach to handle scoping. Double-check that no off-by-one directory jumps occur for complex package paths.


85-85: Robust error logging.
Using debug to log errors when importResolve fails helps with diagnosing resolution issues. This is a best practice for maintainability.

src/import.ts (1)

191-193: Enhanced error checking for resolved files.
Using fs.statSync(..., { throwIfNoEntry: false }) combined with an explicit file check (.isFile()) provides robust validation. Throwing a TypeError prevents silent failures. Nicely done.

test/import.test.ts (3)

2-2: Confirm that 'coffee' is properly declared as a dependency
Make sure 'coffee' is added to the project's devDependencies or dependencies to avoid runtime or testing issues.


11-12: Good test coverage for .cjs resolution
Verifies that cjs-index resolves correctly to cjs-index/index.cjs.


23-23: Sufficient clarity for esm-index resolution
This assertion ensures that esm-index resolves to index.mjs, reflecting accurate coverage of ESM module resolution.

package.json Show resolved Hide resolved
@fengmk2 fengmk2 merged commit 7caad4d into master Dec 27, 2024
25 checks passed
@fengmk2 fengmk2 deleted the fix-get-framework branch December 27, 2024 15:39
fengmk2 pushed a commit that referenced this pull request Dec 27, 2024
[skip ci]

## [4.1.5](v4.1.4...v4.1.5) (2024-12-27)

### Bug Fixes

* use importResolve to get framework path ([#31](#31)) ([7caad4d](7caad4d))
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.

1 participant