#How to get a stack trace in a runtime-agnostic way?

1 messages · Page 1 of 1 (latest)

molten bloom
#

Hello! My question is: how am I supposed to get a stack trace in a runtime-agnostic way?

More info:
I am using the stack-trace npm module, located here:
https://www.npmjs.com/package/stack-trace
This module uses the v8 API to get the stack trace.
Bun emulates this v8 API, so the npm package seems to work on Bun too.

However, it gives different results:

$ bun run ./scripts/build.ts --verbose
Attempting to find the package root with an "upStackBy" of: 3
i: 0, fileName: C:\Users\jnesta\Repositories\infrastructure\node_modules\complete-node\dist\index.mjs, functionName: getCallingFunction, methodName: getCallingFunction, typeName: undefined, line: 591:22
i: 1, fileName: C:\Users\jnesta\Repositories\infrastructure\node_modules\complete-node\dist\index.mjs, functionName: getPackageRoot, methodName: getPackageRoot, typeName: undefined, line: 625:23
i: 2, fileName: C:\Users\jnesta\Repositories\infrastructure\node_modules\complete-node\dist\index.mjs, functionName: getPackageRoot, methodName: getPackageRoot, typeName: undefined, line: 624:30
i: 3, fileName: C:\Users\jnesta\Repositories\infrastructure\node_modules\complete-node\dist\index.mjs, functionName: script, methodName: script, typeName: undefined, line: 1305:24
i: 4, fileName: C:\Users\jnesta\Repositories\infrastructure\node_modules\complete-node\dist\index.mjs, functionName: script, methodName: script, typeName: undefined, line: 1286:22
i: 5, fileName: C:\Users\jnesta\Repositories\infrastructure\node_modules\complete-node\dist\index.mjs, functionName: buildScript, methodName: buildScript, typeName: undefined, line: 1278:8
i: 6, fileName: C:\Users\jnesta\Repositories\infrastructure\node_modules\complete-node\dist\index.mjs, functionName: buildScript, methodName: buildScript, typeName: undefined, line: 1273:27
i: 7, fileName: C:\Users\jnesta\Repositories\infrastructure\3_Applications\containers\logix-ci-cd-tasks\scripts\build.ts, functionName: , methodName: , typeName: undefined, line: 4:6
i: 8, fileName: , functionName: asyncModuleEvaluation, methodName: asyncModuleEvaluation, typeName: undefined, line: 2:0
i: 9, fileName: , functionName: processTicksAndRejections, methodName: processTicksAndRejections, typeName: undefined, line: 7:38
$ npx tsx ./scripts/build.ts --verbose
Attempting to find the package root with an "upStackBy" of: 3
i: 0, fileName: file:///C:/Users/jnesta/Repositories/infrastructure/node_modules/complete-node/dist/index.mjs, functionName: getCallingFunction, methodName: null, typeName: null, line: 591:23
i: 1, fileName: file:///C:/Users/jnesta/Repositories/infrastructure/node_modules/complete-node/dist/index.mjs, functionName: getPackageRoot, methodName: null, typeName: null, line: 625:24
i: 2, fileName: file:///C:/Users/jnesta/Repositories/infrastructure/node_modules/complete-node/dist/index.mjs, functionName: script, methodName: null, typeName: null, line: 1305:25
i: 3, fileName: file:///C:/Users/jnesta/Repositories/infrastructure/node_modules/complete-node/dist/index.mjs, functionName: buildScript, methodName: null, typeName: null, line: 1278:9
i: 4, fileName: file:///C:/Users/jnesta/Repositories/infrastructure/3_Applications/containers/logix-ci-cd-tasks/scripts/build.ts, functionName: null, methodName: null, typeName: null, line: 1:82
i: 5, fileName: node:internal/modules/esm/module_job, functionName: run, methodName: run, typeName: ModuleJob, line: 271:25
i: 6, fileName: node:internal/modules/esm/loader, functionName: onImport.tracePromise.__proto__, methodName: null, typeName: null, line: 578:26
i: 7, fileName: node:internal/modules/run_main, functionName: asyncRunEntryPointWithESMLoader, methodName: null, typeName: null, line: 116:5

(Continued below.)

#

As you can see, Bun does not properly emulate the API, as some functions get duplicated in the stack for no apparent reason. This messes up the code in my downstream helper function.

Specifically, the reason that I am using the stack-trace library is because I have a helper function that gets the file path of the calling function. (This is really convenient because it avoids having to explicitly pass import.meta.dirname.)

I am not married to the stack-trace library. So is there some other way that I can get the stack trace and/or the file path of the calling function in a JavaScript-runtime-agnostic way?