oven-sh / bun

Incredibly fast JavaScript runtime, bundler, test runner, and package manager – all in one
https://bun.sh
Other
74.35k stars 2.78k forks source link

Proper Call Stack in macros #15269

Open brainkim opened 2 days ago

brainkim commented 2 days ago

What is the problem this feature would solve?

Currently, the call stack in Bun macros do not provide helpful information. When calling the following code:

callstack.js

export function callstack() {
  return new Error().stack;
}

index.js

import {callstack} from "./callstack.ts" with {type: "macro"};
console.log(callstack());

Bun prints the following:

Error
    at callstack (/REDACTED/callstack.ts:2:13)
    at requestFetch (native:1:11)
    at <anonymous> (native:11:43)
    at <anonymous> (native:11:37)
    at requestInstantiate (native:1:11)
    at requestSatisfyUtil (native:1:11)
    at <anonymous> (native:11:83)

This call stack points to unhelpful internals and does not refernece the caller of the macro.

What is the feature you are proposing to solve the problem?

I think it would be nice if it printed something like:

Error
    at callstack (/REDACTED/callstack.ts:2:13)
    at module code (/REDACTED/index.ts:2:12)
    at callMacro (native:?:?)

My specific use-case is that I wanted to resolve relative paths in my macro asset("./favicon.ico"), and I needed the caller’s path. I went to use the callsites library but got this more or less useless stack.

I took a brief look at the code and it seems like it might be possible but requires some knowledge of JSC internals.

Bun version: 1.1.34

What alternatives have you considered?

No response