Fix/pnp require cache extensions 7234 - #7256
Open
Ayush442842q wants to merge 4 commits into
Open
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What's the problem this PR addresses?
Under Yarn PnP on Node.js versions affected by
HAS_BROKEN_FSTAT_FOR_ZIP_FDS(such as Node.js v22.22.3+, v24.15.0, v25.7.0+),require.cacheandrequire.extensionsareundefinedin CommonJS dependencies (such asrechoir,webpack-cli,tailwindcss,eslint).Root Cause
fstatSyncon virtual file descriptors for files inside.ziparchives to throwEBADF: bad file descriptor, fstat.HAS_BROKEN_FSTAT_FOR_ZIP_FDSwas added to return{ format: 'commonjs', source: <file_content> }from the ESMloadhook for CJS files in zip archives.sourceforformat: 'commonjs'forces Node's ESM loader to construct a synthetic CommonJS module wrapper instead of using standard CJS module loading (Module._load).requirefunction passed to CJS modules loaded via the ESM loader does not attachrequire.cacheorrequire.extensions(undefined). Any dependency inside a zip archive accessingrequire.cacheorrequire.extensionsfails with aTypeError.Closes #7234.
How did you fix it?
Patched
Module.prototype._compile: Inpackages/yarnpkg-pnp/sources/loader/applyPatch.ts, monkey-patchedModule.prototype._compileto inject a single-line safeguard statement at the top of CommonJS module contents:Shebang Compatibility: If the source file starts with a shebang (#!), the safeguard statement is inserted immediately after the shebang line to preserve shebang parsing.
Line Numbering Preservation: Because the safeguard is a single statement on a single line, line numbers in stack traces and sourcemaps remain unaffected.
Rebuilt Hook Artifacts: Recompiled PnP hook bundles (sources/hook.js, sources/hook.raw.js, sources/esm-loader/built-loader.js).
Added Acceptance Test: Added an acceptance test in packages/acceptance-tests/pkg-tests-specs/sources/require.test.js verifying that require.cache and require.extensions are available inside required dependencies under PnP.
Checklist