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(resolve)!: better search path normalization #307

Open
wants to merge 14 commits into
base: main
Choose a base branch
from
Open

Conversation

pi0
Copy link
Member

@pi0 pi0 commented Feb 14, 2025

resolves #158

Due to legacy reasons, resolve utils were wrongly trying to search outside of the current dir.

This PR improves behavior and fixes situations where no URL was passed or URL is absolute path to directory without file://. (check diff for details)

Note: I just learned that Node.js actually has different behavior for file: URLs with trailing slash, resolving from file:///Users/pooya/Code/mlly/ is the same as resolving from file:///Users/pooya/Code/mlly/_index.js.

Behavior comparison

1. default (no url option) [fixed]

  • Before:
    • file:///Users/pooya/Code/mlly (wrong)
    • file:///Users/pooya/Code/ (wrong)
    • file:///Users/pooya/Code/mlly/_index.js (correct)
    • file:///Users/pooya/Code/node_modules (wrong)
  • After:
    • file:///Users/pooya/Code/mlly/ (correct)

2. Passing file:// URL (import.meta.url) [less fallbacks]

  • Before:
    • file:///Users/pooya/Code/mlly/test.mjs (correct)
    • file:///Users/pooya/Code/mlly/ (correct)
    • file:///Users/pooya/Code/mlly/test.mjs/_index.js (correct)
    • file:///Users/pooya/Code/mlly/node_modules (correct)
  • After:
    • file:///Users/pooya/Code/mlly/test.mjs (correct)

3. Passing file path (__filename) [less fallbacks]

  • Before:
    • file:///Users/pooya/Code/mlly/test.mjs (correct)
    • file:///Users/pooya/Code/mlly/ (correct)
    • file:///Users/pooya/Code/mlly/test.mjs/_index.js (correct)
    • file:///Users/pooya/Code/mlly/node_modules (correct)
  • After:
    • file:///Users/pooya/Code/mlly/test.mjs (correct) [costs statSync()]

4. Passing dir path (__dirname or cwd()) [fixed]

  • Before:
    • file:///Users/pooya/Code/mlly (wrong)
    • file:///Users/pooya/Code/ (wrong)
    • file:///Users/pooya/Code/mlly/_index.js (correct)
    • file:///Users/pooya/Code/node_modules (wrong)
  • After:
    • file:///Users/pooya/Code/mlly/ (correct) [costs statSync()]

@pi0 pi0 requested a review from danielroe February 14, 2025 01:08
@pi0 pi0 changed the title fix(resolve): fix resolve path handling fix(resolve): better search path normalization Feb 14, 2025
@pi0 pi0 marked this pull request as ready for review February 14, 2025 01:45
@pi0 pi0 requested a review from antfu February 14, 2025 01:45
@pi0 pi0 marked this pull request as draft February 14, 2025 01:52
@pi0 pi0 marked this pull request as ready for review February 14, 2025 03:02
@antfu
Copy link
Member

antfu commented Feb 14, 2025

I'd love to see the bug get fixed and consistency improve. While I did not encounter issues regarding this, it's a bit hard for me to verify if dependents of this would be affected or not from looking at the code. Do you consider introducing http://pkg.pr.new/ where we could test this with variety of projects to check the affecting surface before landing?

src/resolve.ts Outdated Show resolved Hide resolved
Copy link
Member

@danielroe danielroe left a comment

Choose a reason for hiding this comment

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

Reading through, the logic looks good to me. 👌 More than happy to do some testing in real projects but won't have time until tonight, but don't let me stop the merge.

@pi0
Copy link
Member Author

pi0 commented Feb 14, 2025

Thanks for reviewing!

I will probably introduce change as v2 beta tag and backport later to v1 if we haven’t seen any issues in wild (however thinking more could be still tricky if some usages was depending on incorrect parent resolution)

Co-authored-by: Daniel Roe <daniel@roe.dev>
src/resolve.ts Outdated Show resolved Hide resolved
@danielroe
Copy link
Member

danielroe commented Feb 14, 2025

oh, I wish v2 would be released with this fix 😆 (it's so good!)

if we need more testing (understandable) would you be up for a perf improvement PR in v1 branch? For example, there's currently no deduplication if a URL is passed that matches one of the URLs that would be created, and we have URL -> normalisation -> URL behaviour which seems to be a negative from perf point of view....

@pi0
Copy link
Member Author

pi0 commented Feb 14, 2025

Good point for deduplicate let me see if can fit it in (it is so common and worth to test for v2, we add nested node_modules as search path, only deepest with common base is needed)

Only tricky point is, we should preserve order..

@pi0 pi0 changed the title fix(resolve): better search path normalization fix(resolve)!: better search path normalization Feb 14, 2025
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.

ensure we don't resolve modules in parent folders
4 participants