From 29c3176fa4eab92cc1eb0642916826b51218ca71 Mon Sep 17 00:00:00 2001 From: JPeer264 Date: Sun, 22 Nov 2026 15:42:33 +0100 Subject: [PATCH 1/2] fix(effect): Stop bundling all of Effect with the error reporter The error reporter read `ErrorReporter` off the `effect` main entry with `Reflect.get`, which made bundlers keep every export of Effect (about 250 KB gzipped). It now uses the string keys of Effect's ErrorReporter API and a `Context.Reference` with the key of `CurrentErrorReporters`, so the same build still works with Effect v3 and v4 and tree-shakes. `contextExports` moves into `compat.ts` together with `isEffectV4`, because Rolldown folds a single-use local alias back into `Context.Reference`, which webpack rejects on Effect v3 before 3.11. One reporter instance now serves every Sentry layer, so a failure that is not an object is captured once when the layer is provided twice. Co-Authored-By: Claude Opus 5.5 --- packages/effect/src/compat.ts | 31 ++++ packages/effect/src/errorReporter.ts | 137 ++++++++++-------- packages/effect/src/tracer.ts | 28 +--- packages/effect/test/errorReporter-v3.test.ts | 16 +- packages/effect/test/errorReporter.test.ts | 9 ++ 5 files changed, 122 insertions(+), 99 deletions(-) create mode 100644 packages/effect/src/compat.ts diff --git a/packages/effect/src/compat.ts b/packages/effect/src/compat.ts new file mode 100644 index 000000000000..5e21bbefa360 --- /dev/null +++ b/packages/effect/src/compat.ts @@ -0,0 +1,31 @@ +import { isObjectLike } from '@sentry/core'; +import * as Context from 'effect/Context'; +import * as Exit from 'effect/Exit'; + +// Check if we're running Effect v4 by checking the Exit/Cause structure +// In v4, causes have a 'reasons' array +// In v3, causes have '_tag' directly on the cause object +export const isEffectV4 = (() => { + try { + const testExit = Exit.fail('test') as unknown as { cause?: unknown }; + const cause = testExit.cause; + // v4 causes have 'reasons' array, v3 causes have '_tag' directly + if (isObjectLike(cause) && 'reasons' in cause) { + return true; + } + return false; + } catch { + return false; + } +})(); + +interface ContextExports { + Reference: typeof Context.Reference; + /** Only Effect v3 exports it. */ + GenericTag(key: string): Context.Key; +} + +// Effect v4 has no `Context.GenericTag` and Effect v3 before 3.11 has no `Context.Reference`. Both are read +// through a separate binding because bundlers fail the build on a namespace member the installed version lacks. +// It is exported so our own build cannot fold a single use back into `Context.`. +export const contextExports = Context as unknown as ContextExports; diff --git a/packages/effect/src/errorReporter.ts b/packages/effect/src/errorReporter.ts index 197066e41fcf..a2961e681b8a 100644 --- a/packages/effect/src/errorReporter.ts +++ b/packages/effect/src/errorReporter.ts @@ -1,78 +1,80 @@ import type { SeverityLevel } from '@sentry/core'; import { captureException, isObjectLike } from '@sentry/core'; -import * as Effect from 'effect'; import type * as Cause from 'effect/Cause'; +import * as Effect from 'effect/Effect'; import type * as EffectErrorReporter from 'effect/ErrorReporter'; -import type * as EffectLayer from 'effect/Layer'; -import { empty as emptyLayer } from 'effect/Layer'; -import type * as LogLevel from 'effect/LogLevel'; +import * as Layer from 'effect/Layer'; +import { contextExports, isEffectV4 } from './compat'; -// `effect/ErrorReporter` only exists in Effect v4, so it is read off the main entry instead of being imported -// as a subpath. On Effect v3 the lookup yields `undefined` and no reporter is registered. The property is read -// with `Reflect.get` because bundlers fail the build on `Effect.ErrorReporter` when the export is absent, and -// an aliased property read gets folded back into exactly that member access by our own build. -const ErrorReporter = Reflect.get(Effect, 'ErrorReporter') as typeof EffectErrorReporter | undefined; +// `effect/ErrorReporter` only exists in Effect v4. Importing it fails Effect v3 builds, and reading it off the +// `effect` main entry bundles all of Effect, so the reporter uses the string keys of that API. The types make a +// changed key fail type checking. +const TYPE_ID: EffectErrorReporter.TypeId = '~effect/ErrorReporter'; +const IGNORE: EffectErrorReporter.ignore = '~effect/ErrorReporter/ignore'; +const SEVERITY: EffectErrorReporter.severity = '~effect/ErrorReporter/severity'; +const ATTRIBUTES: EffectErrorReporter.attributes = '~effect/ErrorReporter/attributes'; -const SEVERITY_TO_LEVEL: Record = { - Fatal: 'fatal', - Error: 'error', - Warn: 'warning', - Info: 'info', - Debug: 'debug', - Trace: 'debug', -}; - -function getLevel(errorReporter: typeof EffectErrorReporter, error: unknown): SeverityLevel { - // Effect's `getSeverity` falls back to `Info` for unannotated errors, which would file plain failures as - // informational in Sentry. Only an explicit annotation changes the level. - if (isObjectLike(error) && errorReporter.severity in error) { - return SEVERITY_TO_LEVEL[errorReporter.getSeverity(error)]; - } - return 'error'; -} +/** + * Reference to the reporters that Effect calls for a reported failure. Effect looks up references by key, so it + * resolves to the same entry as `ErrorReporter.CurrentErrorReporters`. `undefined` on Effect v3. + */ +const CurrentErrorReporters = isEffectV4 + ? contextExports.Reference>( + 'effect/ErrorReporter/CurrentErrorReporters', + { defaultValue: () => new Set() }, + ) + : undefined; -function makeSentryErrorReporter(errorReporter: typeof EffectErrorReporter): EffectErrorReporter.ErrorReporter { - const reported = new WeakSet(); +// Only an explicit severity annotation changes the level. Effect's own `getSeverity` falls back to `Info`, which +// would file plain failures as informational in Sentry. +const SEVERITY_TO_LEVEL = new Map([ + ['Fatal', 'fatal'], + ['Error', 'error'], + ['Warn', 'warning'], + ['Info', 'info'], + ['Debug', 'debug'], + ['Trace', 'debug'], +]); - return { - [errorReporter.TypeId]: errorReporter.TypeId, - report({ cause }: { readonly cause: Cause.Cause }): void { - if (reported.has(cause)) { - return; - } - reported.add(cause); +const reported = new WeakSet(); - for (const reason of cause.reasons) { - if (reason._tag === 'Interrupt') { - continue; - } +/** + * One instance for every Sentry layer, so the reporters set holds it once even when the layer is provided twice. + */ +const sentryErrorReporter: EffectErrorReporter.ErrorReporter = { + [TYPE_ID]: TYPE_ID, + report({ cause }: { readonly cause: Cause.Cause }): void { + if (reported.has(cause)) { + return; + } + reported.add(cause); - // The raw error is captured rather than Effect's pretty-printed copy so Sentry sees the original stack, - // `cause` chain and error class. - const error = reason._tag === 'Fail' ? reason.error : reason.defect; + for (const reason of cause.reasons) { + if (reason._tag === 'Interrupt') { + continue; + } - if (isObjectLike(error)) { - if (reported.has(error)) { - continue; - } - reported.add(error); - } + // The raw error is captured rather than Effect's pretty-printed copy so Sentry sees the original stack, + // `cause` chain and error class. + const error = reason._tag === 'Fail' ? reason.error : reason.defect; - if (errorReporter.isIgnored(error)) { + if (isObjectLike(error)) { + if (reported.has(error) || error[IGNORE] === true) { continue; } - - captureException(error, { - mechanism: { type: 'auto.function.effect.error_reporter', handled: false }, - captureContext: { - level: getLevel(errorReporter, error), - extra: isObjectLike(error) ? { ...errorReporter.getAttributes(error) } : undefined, - }, - }); + reported.add(error); } - }, - }; -} + + captureException(error, { + mechanism: { type: 'auto.function.effect.error_reporter', handled: false }, + captureContext: { + level: (isObjectLike(error) && SEVERITY_TO_LEVEL.get(error[SEVERITY])) || 'error', + extra: isObjectLike(error) ? { ...(error[ATTRIBUTES] as Record | undefined) } : undefined, + }, + }); + } + }, +}; /** * Registers a Sentry `ErrorReporter` for `Effect.withErrorReporting`, `ErrorReporter.report` and the @@ -80,10 +82,17 @@ function makeSentryErrorReporter(errorReporter: typeof EffectErrorReporter): Eff * * Effect v3 has no `ErrorReporter` API, so the returned layer is empty there. */ -export function makeSentryErrorReporterLayer(): EffectLayer.Layer { - if (!ErrorReporter) { - return emptyLayer; +export function makeSentryErrorReporterLayer(): Layer.Layer { + if (!CurrentErrorReporters) { + return Layer.empty; } - return ErrorReporter.layer([makeSentryErrorReporter(ErrorReporter)], { mergeWithExisting: true }); + return Layer.effect( + CurrentErrorReporters, + Effect.gen(function* () { + const reporters = new Set(yield* CurrentErrorReporters); + reporters.add(sentryErrorReporter); + return reporters; + }), + ); } diff --git a/packages/effect/src/tracer.ts b/packages/effect/src/tracer.ts index 96a5bdd58b4d..e53961a85207 100644 --- a/packages/effect/src/tracer.ts +++ b/packages/effect/src/tracer.ts @@ -20,6 +20,7 @@ import type * as EffectLayer from 'effect/Layer'; import { succeed as succeedLayer } from 'effect/Layer'; import * as Option from 'effect/Option'; import * as EffectTracer from 'effect/Tracer'; +import { contextExports, isEffectV4 } from './compat'; const EFFECT_SPAN_SYMBOL = Symbol.for('@sentry/effect.EffectSpan'); @@ -213,35 +214,8 @@ class SentrySpanWrapper implements SentrySpanLike { */ export type StartInactiveSpan = (options: StartSpanOptions) => Span; -// Check if we're running Effect v4 by checking the Exit/Cause structure -// In v4, causes have a 'reasons' array -// In v3, causes have '_tag' directly on the cause object -const isEffectV4 = (() => { - try { - const testExit = Exit.fail('test') as unknown as { cause?: unknown }; - const cause = testExit.cause; - // v4 causes have 'reasons' array, v3 causes have '_tag' directly - if (isObjectLike(cause) && 'reasons' in cause) { - return true; - } - return false; - } catch { - return false; - } -})(); - const EXTERNAL_SPAN_KEY = '@sentry/effect/ExternalSpan'; -interface ContextExports { - Reference: typeof Context.Reference; - /** Only Effect v3 exports it. */ - GenericTag(key: string): Context.Key; -} - -// Effect v4 has no `Context.GenericTag` and Effect v3 before 3.11 has no `Context.Reference`. Both are read -// through a separate binding because bundlers fail the build on a namespace member the installed version lacks. -const contextExports = Context as unknown as ContextExports; - /** * Whether the tracer continues the trace of a `Tracer.externalSpan` parent. Effect v4 has no `FiberRef` * and v3 before 3.11 has no `Context.Reference`, so the flag is a plain service in both. The cast is safe: diff --git a/packages/effect/test/errorReporter-v3.test.ts b/packages/effect/test/errorReporter-v3.test.ts index 03e18ce7e445..3d90e00be0ca 100644 --- a/packages/effect/test/errorReporter-v3.test.ts +++ b/packages/effect/test/errorReporter-v3.test.ts @@ -1,15 +1,15 @@ import { getMainCarrier } from '@sentry/core'; -import type * as EffectModule from 'effect'; import { Effect, Layer } from 'effect'; import { beforeEach, describe, expect, it, vi } from 'vitest'; +import type * as Compat from '../src/compat'; import { makeSentryErrorReporterLayer } from '../src/errorReporter'; -import * as sentryServer from '../src/index.server'; +import { effectLayer } from '../src/server/index'; -// Effect v3 ships no `ErrorReporter` module, so its main entry has no such namespace. -vi.mock('effect', async importOriginal => { - const actual = await importOriginal(); - return { ...actual, ErrorReporter: undefined }; -}); +// Effect v3 ships no `ErrorReporter` API. +vi.mock('../src/compat', async importOriginal => ({ + ...(await importOriginal()), + isEffectV4: false, +})); describe('Sentry ErrorReporter without the Effect v4 ErrorReporter API', () => { beforeEach(() => { @@ -24,7 +24,7 @@ describe('Sentry ErrorReporter without the Effect v4 ErrorReporter API', () => { const result = await Effect.runPromise( Effect.succeed('ok').pipe( Effect.provide( - sentryServer.effectLayer({ + effectLayer({ dsn: 'https://username@domain/123', transport: () => ({ send: vi.fn().mockResolvedValue({}), diff --git a/packages/effect/test/errorReporter.test.ts b/packages/effect/test/errorReporter.test.ts index 62e6cb346708..b634ee0132b2 100644 --- a/packages/effect/test/errorReporter.test.ts +++ b/packages/effect/test/errorReporter.test.ts @@ -175,6 +175,15 @@ describe.each([ }).pipe(Effect.provide(makeLayer())), ); + it.effect('reports a failure once when the Sentry layer is provided twice', () => + Effect.gen(function* () { + yield* Effect.fail('Provided twice').pipe(Effect.withErrorReporting, Effect.exit); + yield* flush; + + expect(events).toHaveLength(1); + }).pipe(Effect.provide(makeLayer()), Effect.provide(makeLayer())), + ); + it.effect('keeps reporters registered before the Sentry layer', () => Effect.gen(function* () { const userReporter = vi.fn(); From f66aad6ac93e8bbfbbf338669d5ee34a31b54d1f Mon Sep 17 00:00:00 2001 From: JPeer264 Date: Sun, 22 Nov 2026 17:22:14 +0100 Subject: [PATCH 2/2] fixup! fix(effect): Stop bundling all of Effect with the error reporter --- packages/effect/src/errorReporter.ts | 67 +++++++++++----------- packages/effect/test/errorReporter.test.ts | 9 --- 2 files changed, 34 insertions(+), 42 deletions(-) diff --git a/packages/effect/src/errorReporter.ts b/packages/effect/src/errorReporter.ts index a2961e681b8a..1ef26e108b02 100644 --- a/packages/effect/src/errorReporter.ts +++ b/packages/effect/src/errorReporter.ts @@ -36,45 +36,44 @@ const SEVERITY_TO_LEVEL = new Map([ ['Trace', 'debug'], ]); -const reported = new WeakSet(); +function makeSentryErrorReporter(): EffectErrorReporter.ErrorReporter { + const reported = new WeakSet(); -/** - * One instance for every Sentry layer, so the reporters set holds it once even when the layer is provided twice. - */ -const sentryErrorReporter: EffectErrorReporter.ErrorReporter = { - [TYPE_ID]: TYPE_ID, - report({ cause }: { readonly cause: Cause.Cause }): void { - if (reported.has(cause)) { - return; - } - reported.add(cause); - - for (const reason of cause.reasons) { - if (reason._tag === 'Interrupt') { - continue; + return { + [TYPE_ID]: TYPE_ID, + report({ cause }: { readonly cause: Cause.Cause }): void { + if (reported.has(cause)) { + return; } + reported.add(cause); - // The raw error is captured rather than Effect's pretty-printed copy so Sentry sees the original stack, - // `cause` chain and error class. - const error = reason._tag === 'Fail' ? reason.error : reason.defect; - - if (isObjectLike(error)) { - if (reported.has(error) || error[IGNORE] === true) { + for (const reason of cause.reasons) { + if (reason._tag === 'Interrupt') { continue; } - reported.add(error); - } - captureException(error, { - mechanism: { type: 'auto.function.effect.error_reporter', handled: false }, - captureContext: { - level: (isObjectLike(error) && SEVERITY_TO_LEVEL.get(error[SEVERITY])) || 'error', - extra: isObjectLike(error) ? { ...(error[ATTRIBUTES] as Record | undefined) } : undefined, - }, - }); - } - }, -}; + // The raw error is captured rather than Effect's pretty-printed copy so Sentry sees the original stack, + // `cause` chain and error class. + const error = reason._tag === 'Fail' ? reason.error : reason.defect; + + if (isObjectLike(error)) { + if (reported.has(error) || error[IGNORE] === true) { + continue; + } + reported.add(error); + } + + captureException(error, { + mechanism: { type: 'auto.function.effect.error_reporter', handled: false }, + captureContext: { + level: (isObjectLike(error) && SEVERITY_TO_LEVEL.get(error[SEVERITY])) || 'error', + extra: isObjectLike(error) ? { ...(error[ATTRIBUTES] as Record | undefined) } : undefined, + }, + }); + } + }, + }; +} /** * Registers a Sentry `ErrorReporter` for `Effect.withErrorReporting`, `ErrorReporter.report` and the @@ -87,6 +86,8 @@ export function makeSentryErrorReporterLayer(): Layer.Layer { return Layer.empty; } + const sentryErrorReporter = makeSentryErrorReporter(); + return Layer.effect( CurrentErrorReporters, Effect.gen(function* () { diff --git a/packages/effect/test/errorReporter.test.ts b/packages/effect/test/errorReporter.test.ts index b634ee0132b2..62e6cb346708 100644 --- a/packages/effect/test/errorReporter.test.ts +++ b/packages/effect/test/errorReporter.test.ts @@ -175,15 +175,6 @@ describe.each([ }).pipe(Effect.provide(makeLayer())), ); - it.effect('reports a failure once when the Sentry layer is provided twice', () => - Effect.gen(function* () { - yield* Effect.fail('Provided twice').pipe(Effect.withErrorReporting, Effect.exit); - yield* flush; - - expect(events).toHaveLength(1); - }).pipe(Effect.provide(makeLayer()), Effect.provide(makeLayer())), - ); - it.effect('keeps reporters registered before the Sentry layer', () => Effect.gen(function* () { const userReporter = vi.fn();