Skip to content

fix: resolve dir index with dot specifier correctly #123

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

Merged
merged 1 commit into from
May 29, 2025

Conversation

JounQin
Copy link
Member

@JounQin JounQin commented May 29, 2025

Important

Fixes directory index resolution with dot specifier in ResolverGeneric::require_relative() and updates tests and fixtures accordingly.

  • Behavior:
    • Fixes resolution of directory indices with dot specifier in ResolverGeneric::require_relative() in src/lib.rs.
    • If the path is a directory, resolves to ./index.js; otherwise, resolves to ../index.js.
  • Tests:
    • Adds test cases in resolve_dot() in src/tests/resolve.rs to verify correct resolution of dot specifiers.
  • Fixtures:
    • Renames dot.js to foo.js and index.js to foo/foo.js in fixtures/dot to align with test cases.

This description was created by Ellipsis for bcf1978. You can customize this summary. It will automatically update as commits are pushed.

Summary by CodeRabbit

  • Bug Fixes

    • Improved handling of relative imports using "." and "./", ensuring correct resolution based on whether the path is a file or directory.
  • Tests

    • Expanded test coverage for relative import resolution, including additional scenarios to verify correct behavior with various directory and file structures.

@JounQin JounQin requested a review from Copilot May 29, 2025 04:14
@JounQin JounQin self-assigned this May 29, 2025
@JounQin JounQin added the bug Something isn't working label May 29, 2025
Copy link

coderabbitai bot commented May 29, 2025

Walkthrough

The changes update the resolution logic for relative specifiers "." and "./" in the require_relative method, making the behavior conditional on the fully_specified option and whether the cached path is a directory. Corresponding tests are updated to cover new scenarios involving directory and file paths.

Changes

File(s) Change Summary
src/lib.rs Refined logic for handling "." and "./" in require_relative, adding checks for fully_specified and directory status before resolving to index.js.
src/tests/resolve.rs Updated and expanded resolve_dot test to use a directory with both foo.js and index.js, testing resolution from both file and directory paths.

Sequence Diagram(s)

sequenceDiagram
    participant Caller
    participant Resolver
    participant Filesystem

    Caller->>Resolver: require_relative(specifier, cached_path, options)
    alt specifier is "." or "./" and !fully_specified
        Resolver->>Filesystem: check if cached_path is directory
        alt cached_path is directory
            Resolver->>Filesystem: normalize with "./index.js"
        else cached_path is file
            Resolver->>Filesystem: normalize with "../index.js"
        end
        Filesystem-->>Resolver: return file existence
        alt index.js exists
            Resolver-->>Caller: return index.js path
        else
            Resolver->>Filesystem: continue with original logic
        end
    else
        Resolver->>Filesystem: continue with original logic
    end
    Filesystem-->>Resolver: result
    Resolver-->>Caller: resolved path
Loading

Possibly related PRs

Poem

In the warren of files, with dots in our way,
We checked if a folder or file led astray.
Now index appears, when the spec isn’t full,
Our paths are more clever, our logic less dull.
🐇✨ Resolving with care, as all bunnies should!


📜 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 52f8e2b and bcf1978.

📒 Files selected for processing (2)
  • src/lib.rs (1 hunks)
  • src/tests/resolve.rs (1 hunks)
🧰 Additional context used
🧠 Learnings (1)
📓 Common learnings
Learnt from: JounQin
PR: unrs/unrs-resolver#72
File: fixtures/tsconfig/cases/dot-alias/tsconfigs/tsconfig.base.json:0-0
Timestamp: 2025-04-21T06:11:46.964Z
Learning: JounQin's fix for the dot alias issue in PR #72 addresses the problem by correctly normalizing paths in the `extend_tsconfig` method, ensuring that base URLs are properly resolved as absolute paths when extending tsconfigs, rather than adding special case handling for dot aliases in the resolver.
⏰ Context from checks skipped due to timeout of 90000ms (3)
  • GitHub Check: Codacy Static Code Analysis
  • GitHub Check: Test (windows-latest)
  • GitHub Check: Benchmark
🔇 Additional comments (2)
src/lib.rs (1)

438-448: LGTM! Correct implementation of directory index resolution for dot specifiers.

The logic correctly handles the resolution of "." and "./" specifiers by:

  • Checking if fully_specified is disabled (allowing implicit index resolution)
  • Determining the appropriate path based on whether the current cached path is a directory
  • Using "./index.js" for directory contexts and "../index.js" for file contexts
  • Providing proper fallback through load_as_file

This aligns with Node.js module resolution behavior for directory specifiers.

src/tests/resolve.rs (1)

138-147: Excellent test coverage for the new dot specifier resolution behavior.

The updated test properly validates the new functionality by:

  • Testing both "." and "./" specifiers
  • Covering resolution from both file paths (foo.js) and directory paths (foo/)
  • Using a realistic fixture structure with a directory containing both a file and an index
  • Verifying that all scenarios correctly resolve to the index.js file

The test data structure comprehensively covers the expected use cases for the dot specifier resolution feature.

✨ 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.
    • Explain this complex logic.
    • 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 explain this code block.
    • @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 explain its main purpose.
    • @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.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

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 resolution of directory indexes when using the dot specifier, ensuring that the correct index file is selected based on whether the target is a directory or a file.

  • Test cases in src/tests/resolve.rs were updated to verify resolution behavior using a "foo" directory.
  • The resolution logic in src/lib.rs now considers the fully_specified option and conditionally applies sub-path normalization.

Reviewed Changes

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

File Description
src/tests/resolve.rs Updated tests to validate proper index resolution in a "foo" folder
src/lib.rs Modified resolution logic to conditionally use "./index.js" or "../index.js" based on file type and the fully_specified flag
Comments suppressed due to low confidence (1)

src/tests/resolve.rs:139

  • [nitpick] The variable name 'foo_dir_foo' may be unclear. Consider renaming it to 'foo_file' or a similarly concise name to better indicate that it contains a file path.
let foo_dir_foo = foo_dir.join("foo.js");

Copy link

Copy link

@ellipsis-dev ellipsis-dev bot left a comment

Choose a reason for hiding this comment

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

Important

Looks good to me! 👍

Reviewed everything up to bcf1978 in 57 seconds. Click for details.
  • Reviewed 52 lines of code in 5 files
  • Skipped 0 files when reviewing.
  • Skipped posting 3 draft comments. View those below.
  • Modify your settings and rules to customize what types of comments Ellipsis leaves. And don't forget to react with 👍 or 👎 to teach Ellipsis.
1. src/lib.rs:435
  • Draft comment:
    Good fix in require_relative: using "./index.js" if the base path is a directory and "../index.js" if it's a file. Consider adding a brief comment explaining why the file case uses '../index.js'.
  • Reason this comment was not posted:
    Confidence changes required: 0% <= threshold 50% None
2. src/tests/resolve.rs:39
  • Draft comment:
    Duplicate test case for 'file in module with query and fragment' found (lines 39–40). Consider removing one to avoid redundancy.
  • Reason this comment was not posted:
    Comment was not on a location in the diff, so it can't be submitted as a review comment.
3. src/tests/resolve.rs:136
  • Draft comment:
    The 'resolve_dot' test clearly verifies dot specifier resolution in both file and directory contexts. Adding a brief inline comment to explain the expected behavior could improve clarity for future maintainers.
  • Reason this comment was not posted:
    Confidence changes required: 0% <= threshold 50% None

Workflow ID: wflow_QG6OZQh5fWiheiKf

You can customize Ellipsis by changing your verbosity settings, reacting with 👍 or 👎, replying to comments, or adding code review rules.

Copy link

codecov bot commented May 29, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 93.43%. Comparing base (52f8e2b) to head (bcf1978).
Report is 1 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main     #123   +/-   ##
=======================================
  Coverage   93.42%   93.43%           
=======================================
  Files          13       13           
  Lines        2891     2892    +1     
=======================================
+ Hits         2701     2702    +1     
  Misses        190      190           

☔ 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

codspeed-hq bot commented May 29, 2025

CodSpeed Performance Report

Merging #123 will not alter performance

Comparing fix/resolve_dir_index (bcf1978) with main (52f8e2b)

Summary

✅ 3 untouched benchmarks

@JounQin JounQin merged commit b5c1fd5 into main May 29, 2025
22 checks passed
@JounQin JounQin deleted the fix/resolve_dir_index branch May 29, 2025 04:18
@JounQin JounQin mentioned this pull request May 29, 2025
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