Skip to content

fix: requiring . or ./ should respect mainFiles option#163

Merged
JounQin merged 1 commit intomainfrom
fix/rework_dot
Jun 24, 2025
Merged

fix: requiring . or ./ should respect mainFiles option#163
JounQin merged 1 commit intomainfrom
fix/rework_dot

Conversation

@JounQin
Copy link
Member

@JounQin JounQin commented Jun 24, 2025

close #160

related #121 and #123, the first argument is directory, should never be file.


Important

Fixes directory resolution for . and ./ specifiers to respect mainFiles option, with updates to tests and documentation.

  • Behavior:
    • Adjusts require_relative() in src/lib.rs to handle . and ./ specifiers by ensuring directories are resolved only when the specifier is ..
    • Updates load_as_file_or_directory() to respect mainFiles option when resolving directories.
  • Tests:
    • Modifies resolve_dot() in src/tests/resolve.rs to test new behavior for . and ./ specifiers with and without mainFiles.
    • Updates incorrect_description_file_1() in src/tests/incorrect_description_file.rs to check for empty missing_dependencies.
  • Documentation:
    • Fixes typo in README.md changing import.meta.url to import.meta.dirname.

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

Summary by CodeRabbit

  • Documentation

    • Updated the README to clarify the reference for ECMAScript module directories, changing from import.meta.url to import.meta.dirname.
  • Bug Fixes

    • Adjusted test expectations for file and missing dependencies after a failed resolution due to an incorrect description file.
  • Refactor

    • Simplified the logic for resolving relative paths, streamlining how directory and file resolution is handled.
  • Tests

    • Updated and expanded tests to better reflect resolver behavior, including handling of missing main files and error scenarios.

@JounQin JounQin requested a review from Copilot June 24, 2025 08:56
@JounQin JounQin self-assigned this Jun 24, 2025
@JounQin JounQin added the bug Something isn't working label Jun 24, 2025
@coderabbitai
Copy link

coderabbitai bot commented Jun 24, 2025

Walkthrough

The updates streamline the resolution logic for "." and "./" specifiers in the resolver, removing special-case handling and delegating consistently to a unified resolution function. Related tests were updated to match the new behavior, and the documentation was corrected to reference import.meta.dirname for ECMAScript modules.

Changes

File(s) Change Summary
README.md Updated documentation to reference import.meta.dirname instead of import.meta.url for directories.
src/lib.rs Simplified require_relative logic by removing special-case handling for "." and "./" specifiers.
src/tests/incorrect_description_file.rs Adjusted test assertions to match new dependency tracking after JSON parse errors.
src/tests/resolve.rs Modified and extended tests for "." and "./" resolution; added tests for empty main_files config.

Sequence Diagram(s)

sequenceDiagram
    participant Caller
    participant Resolver
    participant Loader

    Caller->>Resolver: require_relative(specifier, ...)
    alt specifier is "."
        Resolver->>Loader: load_as_file_or_directory(cached_path, "./")
    else
        Resolver->>Loader: load_as_file_or_directory(cached_path, specifier)
    end
    Loader-->>Resolver: Resolution result or NotFound
    Resolver-->>Caller: Return result or error
Loading

Possibly related PRs

  • unrs/unrs-resolver#121: Adds special-case handling for "." and "./" in require_relative, which this PR now removes.
  • unrs/unrs-resolver#123: Modifies require_relative to handle "." and "./" specifiers with conditional logic, which this PR refactors for consistency.

Poem

In the warren of code, a path once split,
Now flows as one—no more special bit.
Tests hop along, their checks made neat,
While docs grow wise with a dirname treat.
With every merge, the garden’s clear—
A simpler world for all who burrow here! 🐇


📜 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 bbe873d and 9d82709.

📒 Files selected for processing (4)
  • README.md (1 hunks)
  • src/lib.rs (1 hunks)
  • src/tests/incorrect_description_file.rs (1 hunks)
  • src/tests/resolve.rs (1 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (2)
  • GitHub Check: Benchmark
  • GitHub Check: Test (windows-latest)
🔇 Additional comments (3)
README.md (1)

91-91: Documentation correction for ECMAScript modules looks good.

The change from import.meta.url to import.meta.dirname correctly reflects the directory path for ECMAScript modules, aligning with the definition of "directory" in this context.

src/tests/resolve.rs (1)

161-161: Test updates properly validate the mainFiles option fix.

The test modifications correctly validate the new behavior:

  • Removing file-based resolution tests aligns with the simplified logic
  • Adding tests with empty main_files vector confirms that the resolver now properly respects this option
  • Both "." and "./" specifiers are tested consistently for the NotFound error case

These tests effectively demonstrate that the PR objective has been achieved.

Also applies to: 168-177

src/tests/incorrect_description_file.rs (1)

20-21: LGTM! Test expectations correctly updated to reflect streamlined resolution logic.

The updated assertions accurately reflect the new resolution behavior:

  • file_dependencies now contains only the specific file that was accessed (package.json), rather than tracking both the directory and file
  • missing_dependencies is empty because the file exists (it's just malformed), so it's not considered missing

This change aligns with the PR's objective to streamline the resolution logic for "." specifiers and provides more precise dependency tracking.

✨ 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 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 refactors how . and ./ are resolved so that they respect the mainFiles option and adds tests to verify behavior when mainFiles is empty. It also updates an existing test to simplify its dependency assertions.

  • Replaces manual special-casing for ././ in ResolverGeneric with a unified load_as_file_or_directory call.
  • Adds tests in resolve.rs to assert NotFound errors when mainFiles is set to an empty list.
  • Simplifies incorrect_description_file_1 test to only assert the expected package file dependency.

Reviewed Changes

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

File Description
src/tests/resolve.rs Updated resolve_dot tests and added cases for empty mainFiles
src/tests/incorrect_description_file.rs Simplified expected file dependencies and missing-dependencies check
src/lib.rs Replaced manual dot-resolution logic with load_as_file_or_directory
Comments suppressed due to low confidence (1)

src/lib.rs:503

  • [nitpick] Update this comment to reflect that both . and ./ are special-cased for directory resolution.
            // ensure resolve directory only when specifier is `.`

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 1df7c1a in 54 seconds. Click for details.
  • Reviewed 83 lines of code in 3 files
  • Skipped 0 files when reviewing.
  • Skipped posting 2 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/tests/incorrect_description_file.rs:20
  • Draft comment:
    Expected file dependencies now only include the package.json. The test now asserts that only 'pack1/package.json' is registered and missing_dependencies is empty. Confirm this is the intended behavior.
  • Reason this comment was not posted:
    Comment did not seem useful. Confidence is useful = 0% <= threshold 50% The comment is asking the PR author to confirm the intended behavior of a test change. It is not making a specific code suggestion or pointing out a specific issue. This violates the rule against asking the author to confirm their intention.
2. src/tests/resolve.rs:168
  • Draft comment:
    New test block in 'resolve_dot' verifies that when mainFiles are empty, resolving '.' or './' fails. This aligns with the fix—ensure that a missing mainFiles configuration results in a NotFound error.
  • Reason this comment was not posted:
    Comment did not seem useful. Confidence is useful = 0% <= threshold 50% This comment is purely informative, explaining what a new test block does and how it aligns with a fix. It doesn't provide a suggestion, ask for confirmation, or point out a potential issue.

Workflow ID: wflow_bKtkp2xBp25LCc5z

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

@sonarqubecloud
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 9d82709 in 1 minute and 1 seconds. Click for details.
  • Reviewed 96 lines of code in 4 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/tests/resolve.rs:152
  • Draft comment:
    The new test block in resolve_dot nicely verifies that when the default mainFiles (e.g. ["index"]) is set, resolving '.' or './' finds the index file, and when mainFiles is empty the dot resolution fails with NotFound. This clearly exercises the intended fix.
  • Reason this comment was not posted:
    Confidence changes required: 0% <= threshold 50% None
2. src/tests/incorrect_description_file.rs:16
  • Draft comment:
    The tests correctly assert that JSON parse errors update the file dependencies as expected. The explicit equality check on file_dependencies ensures that only the package JSON file is reported. This test appears comprehensive.
  • Reason this comment was not posted:
    Confidence changes required: 0% <= threshold 50% None
3. src/tests/resolve.rs:168
  • Draft comment:
    Overall, the additional tests (e.g. for normalized Windows paths, preferRelative, and symlink resolution) cover many edge cases. Consider adding a comment or an extra case verifying behavior when custom mainFiles (other than 'index') is provided to further document the intended configuration behavior.
  • Reason this comment was not posted:
    Confidence changes required: 0% <= threshold 50% None

Workflow ID: wflow_HcpGcfAKNn1naqHo

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

@codecov
Copy link

codecov bot commented Jun 24, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 92.22%. Comparing base (bbe873d) to head (9d82709).
Report is 1 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #163      +/-   ##
==========================================
+ Coverage   92.20%   92.22%   +0.01%     
==========================================
  Files          13       13              
  Lines        3068     3060       -8     
==========================================
- Hits         2829     2822       -7     
+ Misses        239      238       -1     

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

@codspeed-hq
Copy link

codspeed-hq bot commented Jun 24, 2025

CodSpeed Performance Report

Merging #163 will not alter performance

Comparing fix/rework_dot (9d82709) with main (bbe873d)

Summary

✅ 3 untouched benchmarks

@JounQin JounQin merged commit caf2e20 into main Jun 24, 2025
21 checks passed
@JounQin JounQin deleted the fix/rework_dot branch June 24, 2025 09:12
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.

requiring . or ./ should respect mainFiles option

2 participants