Skip to content

fix(pnp): esm - prevent the readFileUtf8 patch from recursing - #7286

Open
yuri91 wants to merge 1 commit into
yarnpkg:masterfrom
yuri91:fix/esm-loader-readfile-recursion
Open

yuri91 wants to merge 1 commit into
yarnpkg:masterfrom
yuri91:fix/esm-loader-readfile-recursion

Conversation

@yuri91

@yuri91 yuri91 commented Sep 28, 2026

Copy link
Copy Markdown

What's the problem this PR addresses?

When the internal translators are loaded before our fs patch (see #5961),
the ESM loader replaces readFileUtf8 (or readFileSync on older Node
versions) on the internal fs binding with a function that reads the
file through fs.readFileSync. For files that aren't in a zip archive,
fs.readFileSync reads them through that same binding method, which is
now the patched one. Each such read recurses until the stack overflows;
the RangeError is swallowed by the catch, and the innermost call
falls back to the original binding, whose result is then returned
through every level of the recursion.

The result is right, but every read of a file outside of the cache costs
thousands of frames for nothing.

How did you fix it?

The patched function keeps a flag while it reads through
fs.readFileSync. Calls nested within it go straight to the original
binding, which is where the recursion used to end. Files inside zip
archives are still served by the patched fs, and errors still come
from the original binding.

Checklist

  • I have read the Contributing Guide.
  • I have set the packages that need to be released for my changes to be effective.
  • I will check that all automated PR checks pass before the PR gets reviewed.

The ESM loader replaces readFileUtf8 (readFileSync on older Node) on the
internal fs binding with a function that reads through fs.readFileSync.
For files outside of zip archives fs.readFileSync calls that same binding
method, so each read recursed until the stack overflowed; the RangeError
was swallowed and the innermost call fell back to the original binding.

Calls nested within the patched function now go straight to the original
binding.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>

This branch has not been deployed

No deployments
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.

1 participant