@@ -338,6 +338,11 @@ function installTransformIfNeeded() {
338338 return ;
339339 transformInstalled = true ;
340340
341+ // Transpilation in `bun` is not necessary, and trying to register a hook would cause issues.
342+ // https://github.com/oven-sh/bun/issues/8222#issuecomment-3665364677
343+ if ( 'Bun' in globalThis )
344+ return ;
345+
341346 registerESMLoader ( ) ;
342347 cc . installSourceMapSupport ( ) ;
343348
@@ -389,28 +394,44 @@ const collectCJSDependencies = (module: Module, dependencies: Set<string>) => {
389394export function wrapFunctionWithLocation < A extends any [ ] , R > ( func : ( location : Location , ...args : A ) => R ) : ( ...args : A ) => R {
390395 return ( ...args ) => {
391396 const oldPrepareStackTrace = Error . prepareStackTrace ;
392- Error . prepareStackTrace = ( error , stackFrames ) => {
393- const frame = sourceMapSupport . wrapCallSite ( stackFrames [ 1 ] as any ) as NodeJS . CallSite ;
394- const fileName = frame . getFileName ( ) ;
395- // Node error stacks for modules use file:// urls instead of paths.
396- const file = ( fileName && fileName . startsWith ( 'file://' ) ) ? url . fileURLToPath ( fileName ) : fileName ;
397- return {
398- file,
399- line : frame . getLineNumber ( ) ,
400- column : frame . getColumnNumber ( ) ,
401- } ;
402- } ;
403397 const oldStackTraceLimit = Error . stackTraceLimit ;
398+ Error . prepareStackTrace = 'Bun' in globalThis ? bunCallerLocation : nodeCallerLocation ;
404399 Error . stackTraceLimit = 2 ;
405400 const obj : { stack : Location } = { } as any ;
406- Error . captureStackTrace ( obj ) ;
407- const location = obj . stack ;
408- Error . stackTraceLimit = oldStackTraceLimit ;
409- Error . prepareStackTrace = oldPrepareStackTrace ;
401+ let location : Location ;
402+ try {
403+ Error . captureStackTrace ( obj ) ;
404+ location = obj . stack ;
405+ } finally {
406+ Error . stackTraceLimit = oldStackTraceLimit ;
407+ Error . prepareStackTrace = oldPrepareStackTrace ;
408+ }
410409 return func ( location , ...args ) ;
411410 } ;
412411}
413412
413+ function nodeCallerLocation ( error : Error , stackFrames : NodeJS . CallSite [ ] ) {
414+ const frame = sourceMapSupport . wrapCallSite ( stackFrames [ 1 ] as any ) as NodeJS . CallSite ;
415+ const fileName = frame . getFileName ( ) ;
416+ // Node error stacks for modules use file:// urls instead of paths.
417+ const file = ( fileName && fileName . startsWith ( 'file://' ) ) ? url . fileURLToPath ( fileName ) : fileName ;
418+ return {
419+ file,
420+ line : frame . getLineNumber ( ) ,
421+ column : frame . getColumnNumber ( ) ,
422+ } ;
423+ }
424+
425+ function bunCallerLocation ( error : Error , stackFrames : NodeJS . CallSite [ ] ) {
426+ const frame = stackFrames [ 1 ] ;
427+ return {
428+ file : frame . getFileName ( ) ,
429+ line : frame . getLineNumber ( ) ,
430+ // Bun's CallSite.getColumnNumber() is 0-based.
431+ column : frame . getColumnNumber ( ) ! + 1 ,
432+ } ;
433+ }
434+
414435function isRelativeSpecifier ( specifier : string ) {
415436 return specifier === '.' || specifier === '..' || specifier . startsWith ( './' ) || specifier . startsWith ( '../' ) ;
416437}
@@ -426,11 +447,6 @@ function registerESMLoader() {
426447 if ( process . env . PW_DISABLE_TS_ESM )
427448 return ;
428449
429- // Transpilation in `bun` is not necessary, and trying to register a hook would cause issues.
430- // https://github.com/oven-sh/bun/issues/8222#issuecomment-3665364677
431- if ( 'Bun' in globalThis )
432- return ;
433-
434450 const nodeModule = require ( 'node:module' ) ;
435451
436452 if ( nodeModule . registerHooks && ! process . env . PLAYWRIGHT_FORCE_ASYNC_LOADER ) {
0 commit comments