refactor(pnp): support resolve from global cached pkg#167
refactor(pnp): support resolve from global cached pkg#167stormslowly wants to merge 8 commits intomainfrom
Conversation
Summary of ChangesHello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request refactors the Yarn Plug'n'Play (PnP) resolution logic to better support resolving modules from global cached packages. It streamlines the internal caching of PnP manifests and enhances the discovery mechanism, removing the need for a manual Highlights
🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console. Changelog
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Code Review
This pull request refactors the pnpm manifest discovery logic to better align with pnpm's behavior, particularly for resolving packages from a global cache. The changes include renaming cache-related fields for clarity, removing the pnp_manifest option in favor of automatic discovery, and rewriting find_pnp_manifest with a more robust two-phase lookup. However, it introduces a high-severity security regression in find_pnp_manifest. The new 'Phase 1' search allows any manifest to claim ownership of any path, which can be exploited for cross-project module resolution hijacking, potentially leading to Remote Code Execution (RCE) in shared resolver environments. Additionally, there's a suggestion to improve error handling for manifest loading to make the resolver more robust against configuration errors.
1d87b3d to
60c474f
Compare
60c474f to
900e08c
Compare
d9f0294 to
6d013ac
Compare
6d013ac to
85fec66
Compare
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 85fec66b37
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
Problem
web-infra-dev/rspress#1246
We already have a solution by specifying the PnP Manifest. It has two drawbacks
Compared to yarn PnP, there is no config to specify a
.pnp.cjs. So should rspack-resolver.What changed
Before this PR, Rspack Resolver does only phase 2's job. If a resolving request's module path goes to the global cache path, no PnPAPI will be found. So resolving failed.
In this PR, the Rspack resolver implements phase 1 in
find_pnp_manifestto find PnPAPI for the global cache path.How yarn Plug'n'Play works from the resolver's view
PnP resolve Steps:
resolveToUnqualified...) to finish the request.Let's focus on "find the right PnPAPI".
Find the PnPAPI
Where does PnPapi come from? The
.pnp.cjsfile exports the API.> require('./.pnp.cjs') { // .... resolveToUnqualified: [Function (anonymous)], resolveUnqualified: [Function (anonymous)], resolveRequest: [Function (anonymous)], //... }How to find the PnPAPI? When you need to find something in a company, the easiest way is to ask the manager. So as the PnpAPI.
There is a manager in every
.pnp.cjs. If there are multiple.pnp.cjsfiles involved in the project, which manager rules?The one comes as
module.parent.id === "internal/preload"All the modules required via
--require module_file.jswill have "internal/preload" parent.In a Yarn PnP project, scripts will be run this way:
node --require './current/pkg/.pnp.cjs' script.jsIn the
setupAPI, one important thing is to intercept the Node Module API_resolveFilename, which does the job of resolvingIn PnP intercepted
_resolveFilename, the manager finds the right PnPAPI by resolve request's parameters (specifier and module path).PnPAPI manager does the PnPAPI (
.pnp.cjs) finding in two phases.phase 1
Try all the registered PnPAPIs to check if the module path matches the PnPAPI's locations. This phase is important when resolving from a global cache ZIP FS.
for example:
If the module path matches one PnPAPI, this PnPAPI is picked. And it will be called in this resolve request.
If no PnPAPIs match, we will go to phase 2.
phase 2
Try to find
.pnp.cjsfrom the module path; if there is none, we walk up the directory.It works as
pnp::find_closest_pnp_manifest_path.After finding the PnPAPIPath, PnP will fetch the stored PnPapi instance or load from the found path (
instance: loadApiInstance(pnpApiPath)) and register it.In the Rspack resolver's view, we have the
pnp-rscrate, which implements PnPAPI for resolving. But we have to implement the manager's logic in the Rspack resolver by ourselves. This PR does some parts of the manager's job.