Skip to content

fix: pnp issue when used in a monorepo #384

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

scytacki
Copy link

@scytacki scytacki commented Jun 12, 2025

Always use the file being linted as the "location" of the require when loading a resolver for the file. This prevents an issue when the sourceFile is in a dependency of the package being linted.
Add a fallback when loading a parser, the fallback uses the file being linted as the location of the require.

Using the file being linted isn't perfect because a user might configure the linter to lint files in other packages. However the approach is better than what was before.

All of the existing tests are passing. I don't time to figure out how to add a test specific to this change. I think the test code would have to be run in a PnP repo that has the top level fallback disabled.

Copy link

changeset-bot bot commented Jun 12, 2025

⚠️ No Changeset found

Latest commit: b4c4d81

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

Copy link

coderabbitai bot commented Jun 12, 2025

Warning

Rate limit exceeded

@JounQin has exceeded the limit for the number of commits or files that can be reviewed per hour. Please wait 16 minutes and 25 seconds before requesting another review.

⌛ How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

📥 Commits

Reviewing files that changed from the base of the PR and between c932f40 and b4c4d81.

📒 Files selected for processing (3)
  • src/utils/module-require.ts (3 hunks)
  • src/utils/parse.ts (1 hunks)
  • src/utils/resolve.ts (1 hunks)

Walkthrough

The changes update the moduleRequire utility to accept an ESLint context parameter, allowing module resolution relative to the file being linted. Correspondingly, calls to moduleRequire and a configuration resolver now pass the relevant context or filename property. No other logic or error handling is modified.

Changes

File(s) Change Summary
src/utils/module-require.ts Updated moduleRequire to accept a context parameter and attempt module resolution relative to the context's physicalFilename. Added relevant imports and updated function signature.
src/utils/parse.ts Modified call to moduleRequire to pass the context as a second argument.
src/utils/resolve.ts Changed argument to normalizeConfigResolvers from sourceFile to context.physicalFilename.

Poem

In the warren of code, a new path we find,
Modules now resolve with context in mind.
From parser to config, the changes align—
Each file’s location helps modules combine.
With context in paw, this rabbit’s elated,
For errors and modules are now better located!
🐇✨

✨ 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 Jun 12, 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

@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 c932f40 in 1 minute and 35 seconds. Click for details.
  • Reviewed 66 lines of code in 3 files
  • Skipped 0 files when reviewing.
  • Skipped posting 5 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/utils/module-require.ts:15
  • Draft comment:
    New 'context' parameter added to moduleRequire. Ensure that both ChildContext and RuleContext reliably provide a valid 'physicalFilename'.
  • Reason this comment was not posted:
    Comment did not seem useful. Confidence is useful = 0% <= threshold 50% The comment is asking the author to ensure that certain contexts provide a valid physicalFilename. This falls under the category of asking the author to ensure something, which is against the rules. The comment does not provide a specific suggestion or point out a specific issue, making it unapproved.
2. src/utils/module-require.ts:41
  • Draft comment:
    Using createRequire(context.physicalFilename) to load modules relative to the linted file is appropriate. Verify that 'physicalFilename' always points to the correct file in all contexts.
  • Reason this comment was not posted:
    Comment did not seem useful. Confidence is useful = 0% <= threshold 50% This comment is asking the PR author to verify something, which is against the rules. It does not provide a specific suggestion or point out a specific issue.
3. src/utils/module-require.ts:29
  • Draft comment:
    Empty catch blocks are used in moduleRequire to fallback silently. Consider logging errors for debugging if resolution unexpectedly fails.
  • 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.
4. src/utils/parse.ts:120
  • Draft comment:
    Passing 'context' to moduleRequire ensures the parser is resolved relative to the linted file, which aligns with the monorepo fix.
  • Reason this comment was not posted:
    Comment did not seem useful. Confidence is useful = 0% <= threshold 50% This comment is purely informative, explaining why a certain approach is taken. It doesn't provide a suggestion, ask for confirmation, or point out a potential issue.
5. src/utils/resolve.ts:303
  • Draft comment:
    Replacing 'sourceFile' with 'context.physicalFilename' in normalizeConfigResolvers ensures resolvers load relative to the actual file being linted, which improves monorepo resolution.
  • Reason this comment was not posted:
    Comment did not seem useful. Confidence is useful = 0% <= threshold 50% This comment is purely informative, explaining the reason behind a code change. It does not provide a suggestion, ask for confirmation, or point out a potential issue.

Workflow ID: wflow_dn8QP1VGh1hqzXHz

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

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

📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 10cf017 and c932f40.

📒 Files selected for processing (3)
  • src/utils/module-require.ts (3 hunks)
  • src/utils/parse.ts (1 hunks)
  • src/utils/resolve.ts (1 hunks)
🧰 Additional context used
🧬 Code Graph Analysis (1)
src/utils/parse.ts (1)
src/utils/module-require.ts (1)
  • moduleRequire (15-48)
⏰ Context from checks skipped due to timeout of 90000ms (1)
  • GitHub Check: Codacy Static Code Analysis
🔇 Additional comments (3)
src/utils/module-require.ts (1)

15-18:

✅ Verification successful

Signature change is breaking – make sure every call-site is patched

moduleRequire now requires a second context argument. Any forgotten call will turn the compile error into a runtime undefined is not a function the moment TS is bypassed (JS build, tests, etc.).
I’ve spotted the update in parse.ts, but please run a quick grep to confirm nothing else slipped through.


🏁 Script executed:

#!/bin/bash
# List invocations that still pass a single argument
rg --no-heading -n "moduleRequire\\s*<[^>]*>?\\s*\\([^,)]*\\)" | grep -v "module-require.ts"

Length of output: 88


All moduleRequire call-sites updated

A search for single-argument invocations returned no results outside module-require.ts. The new two-parameter signature has been applied everywhere.

src/utils/parse.ts (1)

120-121: Call-site updated correctly – LGTM

The extra context argument matches the new moduleRequire signature. No other logic affected.

src/utils/resolve.ts (1)

303-304: Switching to context.physicalFilename fixes PnP, but guard against undefined

normalizeConfigResolvers now receives context.physicalFilename; great for Yarn-PnP look-ups.
However, when ESLint feeds virtual filenames this will become undefined, changing the previous behaviour (where a concrete sourceFile was guaranteed). Please double-check the implementation of normalizeConfigResolvers to ensure it copes with undefined, or add a fallback:

-  normalizeConfigResolvers(
-    configResolvers,
-    context.physicalFilename,
+  normalizeConfigResolvers(
+    configResolvers,
+    context.physicalFilename ?? sourceFile,
   )

@JounQin
Copy link
Member

JounQin commented Jun 12, 2025

It seems depending on undefined behavior of yarn pnp.

@scytacki
Copy link
Author

@JounQin which behavior is it depending on that is undefined?

It is just calling createRequire like the code has already been doing.

  • For the resolvers: It is calling createRequire just like the resolver code was doing before it is just passing it a different filename argument. The new filename argument is more likely to be in the correct package than the sourceFile used before.
  • For the parsers: it is adding an additional place to look for the parser. This new place is very likely to be the user's package that is running the linter. It is this user's package which is the one expected to declare the dependency on the parser.

@JounQin
Copy link
Member

JounQin commented Jun 15, 2025

@arcanis Do you think is this a reasonable and intended solution for Yarn P'n'P?

Always use the file being linted as the "location" of the require when
loading a resolver for the file. This prevents an issue when the sourceFile
 is in a dependency of the package being linted.
Add a fallback when loading a parser, the fallback uses the file being
linted as the location of the require.

Using the file being linted isn't perfect because a user might configure
the linter to lint files in other packages. However the approach is better
than what was before.
Copy link

pkg-pr-new bot commented Jun 18, 2025

Open in StackBlitz

npm i https://pkg.pr.new/eslint-plugin-import-x@384

commit: b4c4d81

@JounQin
Copy link
Member

JounQin commented Jun 18, 2025

@scytacki After thinking days, I'm fine with current changes now. Would you like to add a new e2e test case based on https://github.com/un-ts/eslint-plugin-import-x/tree/master/test/fixtures/yarn-pnp and https://github.com/un-ts/eslint-plugin-import-x/blob/master/test/fixtures.spec.ts? Or I can do it myself.

cc @SukkaW

Copy link
Collaborator

@SukkaW SukkaW left a comment

Choose a reason for hiding this comment

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

LGTM, only a small nitpick.

@@ -117,7 +117,7 @@ export function parse(
// require the parser relative to the main module (i.e., ESLint)
const parser =
typeof parserOrPath === 'string'
? moduleRequire<TSESLint.Parser.ParserModule>(parserOrPath)
? moduleRequire<TSESLint.Parser.ParserModule>(parserOrPath, context)
Copy link
Collaborator

@SukkaW SukkaW Jun 19, 2025

Choose a reason for hiding this comment

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

Suggested change
? moduleRequire<TSESLint.Parser.ParserModule>(parserOrPath, context)
? moduleRequire<TSESLint.Parser.ParserModule>(parserOrPath, context.physicalFilname)

What about only passing context.physicalFilname instead of passing the entire context here?

I don't see any reason why moduleRequire needs to accept the entire context. Let's keep the utility minimum and maintain the semantics.

Copy link
Author

Choose a reason for hiding this comment

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

I was hoping that eslint would eventually add the path of the active config file to the context. So by passing the context we'd only have to update the moduleRequire implementation instead of the places that call it.

I could also see eslint considering adding a function like moduleRequire to the context itself. Basically meaning: "given the current context require this module".

So with those cases it seems to make sense to use the context as the "context" of the require, rather than the path of the file being linted.

Copy link
Member

Choose a reason for hiding this comment

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

I could also see eslint considering adding a function like moduleRequire to the context itself. Basically meaning: "given the current context require this module".

Is there any related reference? I don't think this still need to be required in favor of flat config?

Copy link
Author

@scytacki scytacki Jun 19, 2025

Choose a reason for hiding this comment

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

No reference, that is just my wishful thinking. I assume there will continue to be plugins even with the flat config which use strings to dynamically load optional dependencies (like what import-x is doing). So it would be a nice addition to eslint to make this easier for plugins to do in a way that is better for strict module loaders like Yarn PnP.

But I haven't done research on this. Perhaps eslint really wants to encourage people to stop using strings to load optional dependencies, in that case they will be unlikely to add this kind of method to the context.

Copy link
Member

Choose a reason for hiding this comment

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

Then let's not assume the changes on context, I think sourceFile: string is more reasonable here.

Don't forget to add the related e2e test case, thanks!

@scytacki
Copy link
Author

@JounQin if you can add that e2e test that'd be great. If I do it, it will take me a few days to find time again.

@JounQin
Copy link
Member

JounQin commented Jun 20, 2025

@JounQin if you can add that e2e test that'd be great. If I do it, it will take me a few days to find time again.

image

@scytacki I don't have the permission to update your repo.

Let's continue at #390

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.

3 participants