Skip to content

fix(miniflare): handle missing require.cache in getFreshSourceMapSupport (fix dev server crash under Yarn PnP)#13968

Merged
NuroDev merged 2 commits into
cloudflare:mainfrom
danieltroger:fix/miniflare-pnp-sourcemap-require-cache
May 28, 2026
Merged

fix(miniflare): handle missing require.cache in getFreshSourceMapSupport (fix dev server crash under Yarn PnP)#13968
NuroDev merged 2 commits into
cloudflare:mainfrom
danieltroger:fix/miniflare-pnp-sourcemap-require-cache

Conversation

@danieltroger

@danieltroger danieltroger commented May 19, 2026

Copy link
Copy Markdown
Contributor

Hits Yarn PnP setups using wrangler dev on Node 22 through 24. As soon as the worker emits a structured log or the inspector forwards a stack trace, wrangler crashes:

TypeError: Cannot read properties of undefined (reading '/Users/.../.yarn/berry/cache/@cspotcode-source-map-support-npm-0.8.1-...zip/node_modules/@cspotcode/source-map-support/source-map-support.js')
    at Object.getFreshSourceMapSupport (.../miniflare/dist/src/index.js:76958:40)
    at getSourceMappingPrepareStackTrace (.../wrangler-dist/cli.js:62619:29)
    at handleStructuredLogs (.../wrangler-dist/cli.js:65422:21)
    ...

require.cache is undefined at the failing line.

Why: wrangler does import "miniflare", Node's ESM loader asks Yarn PnP's loader hook for the source of miniflare/dist/src/index.js, gets it back, and therefore routes through loadCJSModule in node:internal/modules/esm/translators instead of loadCJSModuleWithModuleLoad. The Node translator path used when a hook returns a source hands the wrapped CJS code a re-invented require that only carries .resolve and .main, no .cache. Confirmed by logging Object.keys(require) -> [ 'resolve', 'main' ] and String(require).slice(0, 200) -> the body of loadCJSModule's requireFn.

Fix: when we detect the stub (no .cache), reach for a real CJS require via createRequire(__filename) for the cache-swap dance. No behavior change on the normal CJS-loaded path.

Related: yarnpkg/berry#7065 is the adjacent Node 25.7+ EBADF regression in the same area, but this symptom is independent and reproduces on Node 22 through 24.

Repro

Node 24.15.0, Yarn 4.14.1 with .yarnrc.yml containing nodeLinker: pnp, wrangler@4.88.0 / miniflare@4.20260504.0, plus anything that makes the worker emit a structured log:

yarn wrangler dev
curl http://localhost:8789/<any route that logs>

  • Tests
    • Tests included/updated
    • Automated tests not possible - manual testing has been completed as follows:
    • Additional testing not necessary because:
  • Public documentation
    • Cloudflare docs PR(s):
    • Documentation not necessary because: bugfix

Open in Devin Review

When `miniflare` is imported from ESM under Yarn PnP, Node's ESM->CJS
bridge (`loadCJSModule` in `node:internal/modules/esm/translators`) hands
the wrapped CJS module a re-invented `require` that only carries
`.resolve` and `.main`, with no `.cache`. The cache-swap below needs a
real CJS cache, so fall back to one built via `createRequire(__filename)`
when we detect the stub. No behavior change on the normal CJS-loaded
path.

Fixes a `TypeError: Cannot read properties of undefined (reading '<path>')`
crash in `wrangler dev` whenever the worker emits a structured log or the
inspector forwards a stack trace, on Yarn PnP setups using Node 22-24.
@workers-devprod workers-devprod requested review from a team and NuroDev and removed request for a team May 19, 2026 08:44
@workers-devprod

Copy link
Copy Markdown
Contributor

Codeowners approval required for this PR:

  • @cloudflare/wrangler
Show detailed file reviewers
  • .changeset/fix-miniflare-pnp-sourcemap.md: [@cloudflare/wrangler]
  • packages/miniflare/src/plugins/core/errors/sourcemap.ts: [@cloudflare/wrangler]

@devin-ai-integration devin-ai-integration Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

✅ Devin Review: No Issues Found

Devin Review analyzed this PR and found no potential bugs to report.

View in Devin Review to see 1 additional finding.

Open in Devin Review

@danieltroger danieltroger changed the title fix(miniflare): handle missing require.cache in getFreshSourceMapSupport (Yarn PnP) fix(miniflare): handle missing require.cache in getFreshSourceMapSupport (fix dev server crash under Yarn PnP) May 19, 2026
@github-project-automation github-project-automation Bot moved this to Untriaged in workers-sdk May 27, 2026
@workers-devprod

workers-devprod commented May 27, 2026

Copy link
Copy Markdown
Contributor

Codeowners approval required for this PR:

  • ✅ @cloudflare/wrangler
Show detailed file reviewers

@pkg-pr-new

pkg-pr-new Bot commented May 27, 2026

Copy link
Copy Markdown
create-cloudflare

npm i https://pkg.pr.new/create-cloudflare@13968

@cloudflare/deploy-helpers

npm i https://pkg.pr.new/@cloudflare/deploy-helpers@13968

@cloudflare/kv-asset-handler

npm i https://pkg.pr.new/@cloudflare/kv-asset-handler@13968

miniflare

npm i https://pkg.pr.new/miniflare@13968

@cloudflare/pages-shared

npm i https://pkg.pr.new/@cloudflare/pages-shared@13968

@cloudflare/unenv-preset

npm i https://pkg.pr.new/@cloudflare/unenv-preset@13968

@cloudflare/vite-plugin

npm i https://pkg.pr.new/@cloudflare/vite-plugin@13968

@cloudflare/vitest-pool-workers

npm i https://pkg.pr.new/@cloudflare/vitest-pool-workers@13968

@cloudflare/workers-editor-shared

npm i https://pkg.pr.new/@cloudflare/workers-editor-shared@13968

@cloudflare/workers-utils

npm i https://pkg.pr.new/@cloudflare/workers-utils@13968

wrangler

npm i https://pkg.pr.new/wrangler@13968

commit: 5f81952

@changeset-bot

changeset-bot Bot commented May 28, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 5f81952

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

This PR includes changesets to release 5 packages
Name Type
miniflare Patch
@cloudflare/pages-shared Patch
@cloudflare/vite-plugin Patch
@cloudflare/vitest-pool-workers Patch
wrangler 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

@NuroDev NuroDev left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks for this change @danieltroger 🧡

@workers-devprod workers-devprod left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Codeowners reviews satisfied

@github-project-automation github-project-automation Bot moved this from Untriaged to Approved in workers-sdk May 28, 2026
@NuroDev NuroDev merged commit 0ce88ea into cloudflare:main May 28, 2026
53 checks passed
@github-project-automation github-project-automation Bot moved this from Approved to Done in workers-sdk May 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

3 participants