From 0d32de703737735cad97631fff4126bb3bd9f115 Mon Sep 17 00:00:00 2001 From: Michael Arnaldi Date: Tue, 17 Dec 2024 17:58:02 +0100 Subject: [PATCH] Carry both call-site and definition site in Effect.fn, auto-trace to anon --- .changeset/weak-pears-remember.md | 5 +++++ packages/effect/src/Effect.ts | 29 ++++++++++++++++++++++++--- packages/effect/src/internal/cause.ts | 10 +++++---- 3 files changed, 37 insertions(+), 7 deletions(-) create mode 100644 .changeset/weak-pears-remember.md diff --git a/.changeset/weak-pears-remember.md b/.changeset/weak-pears-remember.md new file mode 100644 index 00000000000..e28a4dd11f2 --- /dev/null +++ b/.changeset/weak-pears-remember.md @@ -0,0 +1,5 @@ +--- +"effect": patch +--- + +Carry both call-site and definition site in Effect.fn, auto-trace to anon diff --git a/packages/effect/src/Effect.ts b/packages/effect/src/Effect.ts index d4a2aeb5a28..1d7b3bd0079 100644 --- a/packages/effect/src/Effect.ts +++ b/packages/effect/src/Effect.ts @@ -10130,9 +10130,31 @@ export const fn: name: string, options?: Tracer.SpanOptions ) => fn.Gen & fn.NonGen) = function(nameOrBody: Function | string, ...pipeables: Array) { + const limit = Error.stackTraceLimit + Error.stackTraceLimit = 2 + const error0 = new Error() + Error.stackTraceLimit = limit if (typeof nameOrBody !== "string") { - return function(this: any) { - return fnApply(this, nameOrBody, arguments as any, pipeables) + return function(this: any, ...args: Array) { + const limit = Error.stackTraceLimit + Error.stackTraceLimit = 2 + const error = new Error() + Error.stackTraceLimit = limit + let cache: false | string = false + const captureStackTrace = () => { + if (cache !== false) { + return cache + } + if (error.stack) { + const stack0 = error0.stack!.trim().split("\n") + const stack = error.stack.trim().split("\n") + cache = `${stack0.slice(2).join("\n").trim()}\n${stack.slice(2).join("\n").trim()}` + return cache + } + } + const effect = fnApply(this, nameOrBody, args, pipeables) + const opts: any = { captureStackTrace } + return withSpan(effect, "", opts) } as any } const name = nameOrBody @@ -10149,8 +10171,9 @@ export const fn: return cache } if (error.stack) { + const stack0 = error0.stack!.trim().split("\n") const stack = error.stack.trim().split("\n") - cache = stack.slice(2).join("\n").trim() + cache = `${stack0.slice(2).join("\n").trim()}\n${stack.slice(2).join("\n").trim()}` return cache } } diff --git a/packages/effect/src/internal/cause.ts b/packages/effect/src/internal/cause.ts index 38bcbb4ddd1..8ca4623142a 100644 --- a/packages/effect/src/internal/cause.ts +++ b/packages/effect/src/internal/cause.ts @@ -1073,7 +1073,7 @@ export const prettyErrorMessage = (u: unknown): string => { return stringifyCircular(u) } -const locationRegex = /\((.*)\)/ +const locationRegex = /\((.*)\)/g /** @internal */ export const spanToTrace = globalValue("effect/Tracer/spanToTrace", () => new WeakMap()) @@ -1105,9 +1105,11 @@ const prettyErrorStack = (message: string, stack: string, span?: Span | undefine if (typeof stackFn === "function") { const stack = stackFn() if (typeof stack === "string") { - const locationMatch = stack.match(locationRegex) - const location = locationMatch ? locationMatch[1] : stack.replace(/^at /, "") - out.push(` at ${current.name} (${location})`) + const locationMatchAll = stack.matchAll(locationRegex) + for (const locationMatch of locationMatchAll) { + const location = locationMatch ? locationMatch[1] : stack.replace(/^at /, "") + out.push(` at ${current.name} (${location})`) + } } else { out.push(` at ${current.name}`) }