Skip to content

fix: always sort projects by affinity before iterating #459

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 3 commits into from
May 30, 2025

Conversation

carlocorradini
Copy link
Collaborator

@carlocorradini carlocorradini commented May 30, 2025

Fix #393

It is necessary to always sort projects by affinity before iterating.
This correctly resolves the current file to the nearest and correct tsconfig.json file, even if there are several mappings with the same prefix in different tsconfig.json files.

The _e2e_ test `nearestTsconfig` fails because the function `matcherCached` always returns `undefined`, indicating that the file could not be resolved. I believe the latter is due to _vitest_'s snapshot serializer, which replaces the absolute path of the root with the alias ``, and this is not handled correctly by `get-tsconfig`. I'll let you deal with this minor issue because I'm not sure whether it's necessary or how to fix it properly.

Important

Fixes issue #393 by sorting projects by affinity in resolve() to ensure correct tsconfig.json resolution.

  • Behavior:
  • Tests:
    • nearestTsconfig e2e test fails due to path aliasing issue in vitest's snapshot serializer, unrelated to this fix.

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

Summary by CodeRabbit

  • Bug Fixes

    • Improved project resolution consistency by sorting projects by affinity before processing.
    • Enhanced resolver caching to only store successful resolutions, improving reliability.
    • Simplified fallback behavior with clearer handling and logging.
  • Chores

    • Added a Codecov coverage badge to the documentation.

@carlocorradini carlocorradini requested a review from JounQin May 30, 2025 13:27
Copy link

changeset-bot bot commented May 30, 2025

🦋 Changeset detected

Latest commit: d3e5916

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
eslint-import-resolver-typescript 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

Copy link

coderabbitai bot commented May 30, 2025

Walkthrough

The pull request updates the resolver logic to sort TypeScript projects by affinity before iteration, attempts resolution before caching the resolver, and adjusts fallback resolution accordingly. It also adds a Codecov badge to the README and introduces a changeset documenting these patch updates.

Changes

Files Change Summary
src/index.ts Modified resolver creation to sort projects by affinity, resolve before caching, and update fallback iteration and logging. Added NapiResolveOptions type import and usage internally.
.changeset/ready-mirrors-sneeze.md Added a changeset documenting the patch update for eslint-import-resolver-typescript fixing project sorting by affinity.
README.md Added Codecov coverage badge to README badges section.

Assessment against linked issues

Objective Addressed Explanation
Fix import/no-unresolved on NX monorepos on 4.x (#393)

Assessment against linked issues: Out-of-scope changes

No out-of-scope changes detected.

Possibly related PRs

Poem

🐇
Projects sorted, one by one,
Resolvers race till task is done.
Cache with care, no guess or miss,
Codecov shines, a badge of bliss!
A rabbit hops with joy anew,
Fixes made and badges too!
🐰✨

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.

yarn install v1.22.22
warning Resolution field "workspace:" has an invalid version entry and may be ignored
(node:7314) [DEP0169] DeprecationWarning: url.parse() behavior is not standardized and prone to errors that have security implications. Use the WHATWG URL API instead. CVEs are not issued for url.parse() vulnerabilities.
(Use node --trace-deprecation ... to show where the warning was created)
[1/4] Resolving packages...
error Couldn't find any versions for "eslint-import-resolver-typescript" that matches "workspace:
"
info Visit https://yarnpkg.com/en/docs/cli/install for documentation about this command.


📜 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 2bec63e and d3e5916.

📒 Files selected for processing (2)
  • README.md (1 hunks)
  • src/index.ts (2 hunks)
✅ Files skipped from review due to trivial changes (1)
  • README.md
🚧 Files skipped from review as they are similar to previous changes (1)
  • src/index.ts
⏰ Context from checks skipped due to timeout of 90000ms (4)
  • GitHub Check: Lint and Test with Node.js 24 on windows-latest
  • 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
✨ 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

codesandbox-ci bot commented May 30, 2025

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

Copy link

pkg-pr-new bot commented May 30, 2025

Open in StackBlitz

npm i https://pkg.pr.new/eslint-import-resolver-typescript@459

commit: d3e5916

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 d82e908 in 1 minute and 33 seconds. Click for details.
  • Reviewed 50 lines of code in 2 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. .changeset/ready-mirrors-sneeze.md:5
  • Draft comment:
    Changeset message looks good; it reflects the sorting fix.
  • Reason this comment was not posted:
    Confidence changes required: 0% <= threshold 50% None
2. src/index.ts:100
  • Draft comment:
    The removed block that parses tsconfig and creates a new ResolverFactory has been omitted. Without this, if no cached resolver is found, no resolver is created, which may break resolution for new tsconfig files.
  • Reason this comment was not posted:
    Comment was on unchanged code.
3. src/index.ts:142
  • Draft comment:
    The fallback recursion that attempted resolution with sorted projects has been removed. This may lead to missing valid resolution paths when no cached resolver exists.
  • Reason this comment was not posted:
    Comment did not seem useful. Confidence is useful = 30% <= threshold 50% The comment is informative and suggests a potential issue with the removal of a feature. However, it does not provide a specific suggestion or ask for confirmation of intent. It implies a potential problem but does not directly address the PR author with a specific action or question.

Workflow ID: wflow_yakE3ODVzcYW09uV

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

@JounQin JounQin changed the title Fix issue 393 fix: always sort projects by affinity before iterating May 30, 2025
@JounQin
Copy link
Collaborator

JounQin commented May 30, 2025

I believe the latter is due to vitest's snapshot serializer,

@carlocorradini Did you try to disable the snapshot serializer for testing locally?

@carlocorradini
Copy link
Collaborator Author

carlocorradini commented May 30, 2025

I believe the latter is due to vitest's snapshot serializer,

@carlocorradini Did you try to disable the snapshot serializer for testing locally?

@JounQin
Yes, it works. However, the tests fail since certain expect functions use <ROOT> instead of the entire absolute path, causing the comparison to fail.

@JounQin
Copy link
Collaborator

JounQin commented May 30, 2025

Yes, it works. However, the tests fail since certain expect functions use instead of the entire absolute path, causing the comparison to fail.

Thanks for the clarification, I'll try to fix the test cases then.

@JounQin
Copy link
Collaborator

JounQin commented May 30, 2025

image

@carlocorradini I'm still getting this issue even if I disabled the snapshot serializer.

You need to run yarn build first before e2e testing.

image

I don't see any difference between the original and sorted tsconfigs.

image

@JounQin JounQin enabled auto-merge (squash) May 30, 2025 23:00
@JounQin JounQin merged commit f16150b into import-js:master May 30, 2025
20 checks passed
@carlocorradini
Copy link
Collaborator Author

@JounQin Awesome! What/where was the issue(s)?

@JounQin
Copy link
Collaborator

JounQin commented May 31, 2025

@JounQin Awesome! What/where was the issue(s)?

See my commit message d3e5916 (#459),

renovate bot added a commit to andrei-picus-tink/auto-renovate that referenced this pull request May 31, 2025
| datasource | package                           | from  | to    |
| ---------- | --------------------------------- | ----- | ----- |
| npm        | eslint-import-resolver-typescript | 4.3.4 | 4.4.2 |


## [v4.4.2](https://github.com/import-js/eslint-import-resolver-typescript/blob/HEAD/CHANGELOG.md#442)

##### Patch Changes

-   [#459](import-js/eslint-import-resolver-typescript#459) [`f16150b`](import-js/eslint-import-resolver-typescript@f16150b) Thanks [@carlocorradini](https://github.com/carlocorradini)! - fix: always sort projects by affinity before iterating


## [v4.4.1](https://github.com/import-js/eslint-import-resolver-typescript/blob/HEAD/CHANGELOG.md#441)

##### Patch Changes

-   [#457](import-js/eslint-import-resolver-typescript#457) [`d9f3e68`](import-js/eslint-import-resolver-typescript@d9f3e68) Thanks [@JounQin](https://github.com/JounQin)! - fix(deps): bump `eslint-import-context` to v0.1.5


## [v4.4.0](https://github.com/import-js/eslint-import-resolver-typescript/blob/HEAD/CHANGELOG.md#440)

##### Minor Changes

-   [#453](import-js/eslint-import-resolver-typescript#453) [`7d87517`](import-js/eslint-import-resolver-typescript@7d87517) Thanks [@JounQin](https://github.com/JounQin)! - feat: integrate `eslint-import-context` to get correct `context.cwd`


## [v4.3.5](https://github.com/import-js/eslint-import-resolver-typescript/blob/HEAD/CHANGELOG.md#435)

##### Patch Changes

-   [#450](import-js/eslint-import-resolver-typescript#450) [`3f1aab1`](import-js/eslint-import-resolver-typescript@3f1aab1) Thanks [@JounQin](https://github.com/JounQin)! - fix: remove buggy `module-sync` exports field
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

import/no-unresolved on NX monorepos on 4.x
2 participants