Skip to content

fix(node): Patch the mutable fs object in fsIntegration under native ESM - #26086

Open
chargome wants to merge 3 commits into
developfrom
fix-fsintegration-esm-cra
Open

chargome wants to merge 3 commits into
developfrom
fix-fsintegration-esm-cra

Conversation

@chargome

@chargome chargome commented Nov 4, 2026 •

Copy link
Copy Markdown
Member

Why: In native ESM, fsIntegration crashed with Cannot assign to read only property, because it patched the read-only import * as fs namespace. It now patches the mutable fs object behind the default export. A plain import fs from 'fs' isn't an option: our rollup interop: 'esModule' setting would turn it into require('fs').default in the CJS build, which is undefined. After patching, it calls syncBuiltinESMExports() so named imports (import { readFile } from 'fs') pick up the patched functions. That call is wrapped in try/catch because Cloudflare workerd exports it but throws when it's called.

How to review: The fix is in instrumentation.ts. The fs suite now runs in both ESM and CJS. On Bun and Deno, the ESM node:fs exports aren't tied to the object we patch, so only fs.promises calls get spans there. For that reason those ESM tests run on Node only. The named-import test is ESM-only, because CJS destructuring copies the functions before the patch is applied.

Unsure about: syncBuiltinESMExports() updates the ESM exports of every builtin module, not only fs. It only makes patches applied to the CJS objects visible to ESM importers, so it shouldn't affect anything else.

Closes #25950

In native ESM, `import * as fs` is a read-only module namespace, so patching it threw
`Cannot assign to read only property`. Patch the mutable CJS `fs` object behind the
default export instead, and sync builtin ESM exports so named imports see the wrappers.

The fs integration tests now also run in ESM.

Fixes #25950
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@chargome

chargome commented Nov 4, 2026

Copy link
Copy Markdown
Member Author

bugbot run

@github-actions

github-actions Bot commented Nov 4, 2026 •

Copy link
Copy Markdown
Contributor

size-limit report 📦

Compared: fc15b78 (PR test merge using develop at b9d0f82)

Against: b9d0f82 (develop) · base run #64917

✅ Baseline matches: this diff contains only this PR's changes.

Path Size % Change Change
@sentry/browser 30.4 kB - -
@sentry/browser - with treeshaking flags 28.54 kB - -
@sentry/browser - with treeshaking flags tracing without tracing 28.43 kB - -
@sentry/browser (incl. Tracing) 52.72 kB - -
@sentry/browser (incl. Tracing + Span Streaming) 52.72 kB - -
@sentry/browser (incl. Tracing, Profiling) 55.69 kB - -
@sentry/browser (incl. Tracing, Replay) 93.02 kB - -
@sentry/browser (incl. Tracing, Replay) - with treeshaking flags 81.61 kB - -
@sentry/browser (incl. Tracing, Replay with Canvas) 97.76 kB - -
@sentry/browser (incl. Tracing, Replay, Feedback) 110.75 kB - -
@sentry/browser (incl. Feedback) 47.98 kB - -
@sentry/browser (incl. sendFeedback) 35.5 kB - -
@sentry/browser (incl. FeedbackAsync) 40.61 kB - -
@sentry/browser (incl. Metrics) 31.44 kB - -
@sentry/browser (incl. Logs) 31.73 kB - -
@sentry/browser (incl. Metrics & Logs) 32.45 kB - -
@sentry/react 32.29 kB - -
@sentry/react (incl. Tracing) 55.09 kB - -
@sentry/vue 38.67 kB - -
@sentry/vue (incl. Tracing) 55.73 kB - -
@sentry/svelte 30.43 kB - -
@sentry/remix (Remix 3 client bundle) 57.8 kB - -
CDN Bundle 32.19 kB - -
CDN Bundle (incl. Tracing) 53.24 kB - -
CDN Bundle (incl. Logs, Metrics) 34.47 kB - -
CDN Bundle (incl. Tracing, Logs, Metrics) 55.19 kB - -
CDN Bundle (incl. Replay, Logs, Metrics) 75.88 kB - -
CDN Bundle (incl. Tracing, Replay) 91.46 kB - -
CDN Bundle (incl. Tracing, Replay, Logs, Metrics) 93.39 kB - -
CDN Bundle (incl. Tracing, Replay, Feedback) 97.56 kB - -
CDN Bundle (incl. Tracing, Replay, Feedback, Logs, Metrics) 99.56 kB - -
CDN Bundle - uncompressed 94.67 kB - -
CDN Bundle (incl. Tracing) - uncompressed 157.63 kB - -
CDN Bundle (incl. Logs, Metrics) - uncompressed 101.14 kB - -
CDN Bundle (incl. Tracing, Logs, Metrics) - uncompressed 163.37 kB - -
CDN Bundle (incl. Replay, Logs, Metrics) - uncompressed 232.71 kB - -
CDN Bundle (incl. Tracing, Replay) - uncompressed 279.32 kB - -
CDN Bundle (incl. Tracing, Replay, Logs, Metrics) - uncompressed 285.05 kB - -
CDN Bundle (incl. Tracing, Replay, Feedback) - uncompressed 293.03 kB - -
CDN Bundle (incl. Tracing, Replay, Feedback, Logs, Metrics) - uncompressed 298.74 kB - -
@sentry/nextjs (client) 58.04 kB - -
@sentry/sveltekit (client) 53.23 kB - -
@sentry/core/server 44.25 kB - -
@sentry/core/browser 13.93 kB - -
@sentry/node 155.8 kB +0.02% +21 B 🔺
@sentry/node/import (ESM hook with diagnostics-channel injection) 82.26 kB - -
@sentry/node - without tracing 96.9 kB +0.02% +18 B 🔺
@sentry/node - without channel injection 136.13 kB +0.02% +23 B 🔺
@sentry/aws-serverless 105.11 kB +0.02% +20 B 🔺
@sentry/cloudflare (withSentry) - minified 200.67 kB - -
@sentry/cloudflare (withSentry) 499.05 kB - -
@sentry/nextjs/cloudflare (withSentry) - minified 221.21 kB - -

View base workflow run

@cursor cursor Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Stale Bugbot comment from a previous run.

@github-actions

github-actions Bot commented Nov 4, 2026 •

Copy link
Copy Markdown
Contributor

Optional E2E tests

Some optional checks were not successful. They do not block the merge. 3 not successful, 41 successful. See the run.

Check Status
❌ CI: Optional E2E Tests / E2E nuxt-5 Test (optional) Failing after 2m Details
❌ CI: Optional E2E Tests / E2E node-flue (latest) Test (optional) Failing after 12s Details
❌ CI: Optional E2E Tests / E2E nuxt-5 (tunnel-dev) Test (optional) Failing after 2m Details
Successful checks
Check Status
✅ CI: Optional E2E Tests / E2E astro-7 Test (optional) Successful in 55s Details
✅ CI: Optional E2E Tests / E2E nextjs-16 (deno) Test (optional) Successful in 1m Details
✅ CI: Optional E2E Tests / E2E nextjs-15 (turbo) Test (optional) Successful in 2m Details
✅ CI: Optional E2E Tests / E2E gen-ai-libraries (cloudflare) Test (optional) Successful in 26s Details
✅ CI: Optional E2E Tests / E2E nextjs-14 (latest) Test (optional) Successful in 1m Details
✅ CI: Optional E2E Tests / E2E gen-ai-libraries Test (optional) Successful in 38s Details
✅ CI: Optional E2E Tests / E2E gen-ai-libraries (latest) Test (optional) Successful in 29s Details
✅ CI: Optional E2E Tests / E2E nextjs-15 (latest) Test (optional) Successful in 2m Details
✅ CI: Optional E2E Tests / E2E nextjs-16 (vinext, bun) Test (optional) Successful in 1m Details
✅ CI: Optional E2E Tests / E2E debug-id-sourcemaps Test (optional) Successful in 22s Details
✅ CI: Optional E2E Tests / E2E astro-7-static Test (optional) Successful in 46s Details
✅ CI: Optional E2E Tests / E2E nestjs-12 (latest) Test (optional) Successful in 37s Details
✅ CI: Optional E2E Tests / E2E nextjs-16 (cloudflare, latest) Test (optional) Successful in 1m Details
✅ CI: Optional E2E Tests / E2E node-eve (latest) Test (optional) Successful in 1m Details
✅ CI: Optional E2E Tests / E2E node-mastra (latest) Test (optional) Successful in 46s Details
✅ CI: Optional E2E Tests / E2E node-eve (orchestrion) Test (optional) Successful in 33s Details
✅ CI: Optional E2E Tests / E2E node-express-send-to-sentry Test (optional) Successful in 1m Details
✅ CI: Optional E2E Tests / E2E node-openai-decisions Test (optional) Successful in 17s Details
✅ CI: Optional E2E Tests / E2E node-flue Test (optional) Successful in 1m Details
✅ CI: Optional E2E Tests / E2E nuxt-5-sourcemaps (static / nuxt generate) Test (optional) Successful in 18s Details
✅ CI: Optional E2E Tests / E2E tanstackstart-react (latest) Test (optional) Successful in 1m Details
✅ CI: Optional E2E Tests / E2E react-router-7-framework-instrumentation Test (optional) Successful in 2m Details
✅ CI: Optional E2E Tests / E2E nextjs-16 (vinext) Test (optional) Successful in 1m Details
✅ CI: Optional E2E Tests / E2E supabase-nextjs Test (optional) Successful in 1m Details
✅ CI: Optional E2E Tests / E2E node-fastify-6 (tracer provider) Test (optional) Successful in 19s Details
✅ CI: Optional E2E Tests / E2E node-openai-decisions (latest) Test (optional) Successful in 16s Details
✅ CI: Optional E2E Tests / E2E nextjs-orpc Test (optional) Successful in 59s Details
✅ CI: Optional E2E Tests / E2E node-fastify-6 Test (optional) Successful in 19s Details
✅ CI: Optional E2E Tests / E2E node-express-send-to-sentry-static Test (optional) Successful in 46s Details
✅ CI: Optional E2E Tests / E2E node-mastra Test (optional) Successful in 40s Details
✅ CI: Optional E2E Tests / E2E node-eve Test (optional) Successful in 34s Details
✅ CI: Optional E2E Tests / E2E node-pi-durable (latest) Test (optional) Successful in 22s Details
✅ CI: Optional E2E Tests / E2E nextjs-16-tunnel (cloudflare) Test (optional) Successful in 44s Details
✅ CI: Optional E2E Tests / E2E nuxt-5-sourcemaps Test (optional) Successful in 19s Details
✅ CI: Optional E2E Tests / E2E solidstart-2 Test (optional) Successful in 37s Details
✅ CI: Optional E2E Tests / E2E tanstackstart-react-sourcemaps Test (optional) Successful in 18s Details
✅ CI: Optional E2E Tests / E2E nuxt-5-sourcemaps (client source maps kept) Test (optional) Successful in 19s Details
✅ CI: Optional E2E Tests / E2E tanstackstart-react-cloudflare Test (optional) Successful in 45s Details
✅ CI: Optional E2E Tests / E2E node-express-mcp-v1 Test (optional) Successful in 16s Details
✅ CI: Optional E2E Tests / E2E node-express-mcp-v2 Test (optional) Successful in 17s Details
✅ CI: Optional E2E Tests / E2E node-pi-durable Test (optional) Successful in 21s Details

The Bun build project runs a bundled scenario outside the test's tmp dir, so a path derived
from the scenario location missed the fixtures. Also skip the ESM tests on Bun, whose ESM
`node:fs` exports behave like Deno's.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@chargome

chargome commented Nov 4, 2026

Copy link
Copy Markdown
Member Author

bugbot run

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@chargome

chargome commented Nov 4, 2026

Copy link
Copy Markdown
Member Author

bugbot run

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

✅ Bugbot reviewed your changes and found no new issues!

Comment @cursor review or bugbot run to trigger another review on this PR

Reviewed by Cursor Bugbot for commit ded32e7. Configure here.

@chargome chargome self-assigned this Nov 4, 2026
@chargome
chargome marked this pull request as ready for review November 4, 2026 09:23
@chargome
chargome requested a review from a team as a code owner November 4, 2026 09:23
@chargome
chargome requested review from andreiborza and mydea and removed request for a team November 4, 2026 09:23
const createRunner = (): ReturnType<typeof createTestRunner> =>
createTestRunner().withEnv({ FIXTURES_DIR: join(cwd, 'fixtures') });
// On Bun and Deno the ESM `node:fs` exports are not tied to the `fs` object we patch, so only `fs.promises` is traced there.
const testUnlessNonNodeEsm = test.runIf(mode === 'cjs' || supports({ runtimes: ['node'] }));

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.

no strong feelings, but would it be good enough to just only run these tests in cjs...? 🤔

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Hm this would defeat the purpose of the pr then, since it is about native esm?

@chargome
chargome requested a review from nicohrubec November 5, 2026 13:03

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.

fsIntegration crashes in native ESM when patching the read-only fs namespace

3 participants