swc icon indicating copy to clipboard operation
swc copied to clipboard

Incorrect stack frame name

Open DerGernTod opened this issue 2 years ago • 4 comments

I have a test that makes sure the stack trace of an error is correct. When I run the test with @swc/jest, the name of the function is changed to apply from namedHandlerFn. It works fine when I run it with ts-jest.

Versions:

node: 16.15.1
npm: 8.1.2
    "@jest/environment": "^28.1.2",
    "@jest/types": "^28.1.1",
    "@swc/core": "^1.2.212",
    "@swc/jest": "^0.2.21",
    "@types/jest": "^28.1.4",
    "jest": "^28.1.2",
    "ts-jest": "^28.0.5",
    "typescript": "^4.7.4"

You can reproduce it yourself with this reproducer: https://github.com/DerGernTod/swc-jest-stack-frame-name

Simply run npm test to see the result in ts-jest, and npm run test-swc if you want to see the issue.


Stacks in comparison: Expected (something around this, most importantly including namedHandlerFn):

    Error: irrelevant
        at C:\workspaces\repos\reproducers\swc-jest-stack-frame-name\src\test.spec.ts:7:19
        at namedHandlerFn (C:\workspaces\repos\reproducers\swc-jest-stack-frame-name\src\test.ts:4:16)
        at callTimer (C:\workspaces\repos\reproducers\swc-jest-stack-frame-name\node_modules\@sinonjs\fake-timers\src\fake-timers-src.js:745:24)
        at doTickInner (C:\workspaces\repos\reproducers\swc-jest-stack-frame-name\node_modules\@sinonjs\fake-timers\src\fake-timers-src.js:1307:29)
        at doTick (C:\workspaces\repos\reproducers\swc-jest-stack-frame-name\node_modules\@sinonjs\fake-timers\src\fake-timers-src.js:1388:20)
        at Object.tick (C:\workspaces\repos\reproducers\swc-jest-stack-frame-name\node_modules\@sinonjs\fake-timers\src\fake-timers-src.js:1396:20)
        at FakeTimers.advanceTimersByTime (C:\workspaces\repos\reproducers\swc-jest-stack-frame-name\node_modules\@jest\fake-timers\build\modernFakeTimers.js:86:19)
        at Object.advanceTimersByTime (C:\workspaces\repos\reproducers\swc-jest-stack-frame-name\node_modules\jest-runtime\build\index.js:2378:26)
        at Object.<anonymous> (C:\workspaces\repos\reproducers\swc-jest-stack-frame-name\src\test.spec.ts:10:18)
        at Promise.then.completed (C:\workspaces\repos\reproducers\swc-jest-stack-frame-name\node_modules\jest-circus\build\utils.js:333:28)

Actual:

Error: irrelevant
        at C:\\workspaces\\repos\\reproducers\\swc-jest-stack-frame-name\\src\\test.spec.ts:7:19
        at apply (C:\\workspaces\\repos\\reproducers\\swc-jest-stack-frame-name\\src\\test.ts:4:16)
        at callTimer (C:\\workspaces\\repos\\reproducers\\swc-jest-stack-frame-name\\node_modules\\@sinonjs\\fake-timers\\src\\fake-timers-src.js:745:24)
        at doTickInner (C:\\workspaces\\repos\\reproducers\\swc-jest-stack-frame-name\\node_modules\\@sinonjs\\fake-timers\\src\\fake-timers-src.js:1307:29)
        at doTick (C:\\workspaces\\repos\\reproducers\\swc-jest-stack-frame-name\\node_modules\\@sinonjs\\fake-timers\\src\\fake-timers-src.js:1388:20)
        at Object.tick (C:\\workspaces\\repos\\reproducers\\swc-jest-stack-frame-name\\node_modules\\@sinonjs\\fake-timers\\src\\fake-timers-src.js:1396:20)
        at FakeTimers.advanceTimersByTime (C:\\workspaces\\repos\\reproducers\\swc-jest-stack-frame-name\\node_modules\\@jest\\fake-timers\\build\\modernFakeTimers.js:86:19)
        at Object.advanceTimersByTime (C:\\workspaces\\repos\\reproducers\\swc-jest-stack-frame-name\\node_modules\\jest-runtime\\build\\index.js:2378:26)
        at Object.advanceTimersByTime (C:\\workspaces\\repos\\reproducers\\swc-jest-stack-frame-name\\src\\test.spec.ts:10:18)
        at Promise.then.completed (C:\\workspaces\\repos\\reproducers\\swc-jest-stack-frame-name\\node_modules\\jest-circus\\build\\utils.js:333:28)

If this is behavior is expected, is there a way I can inspect the files swc generates? If I debug into the test, break right when the error is generated and step into the ts file, is there a way I can see the transpiled file instead? I guess swc gets rid of the name for some reason.

DerGernTod avatar Jul 11 '22 14:07 DerGernTod

I think it's caused by swc target defaulting to es5. Can you try changing the target?

kdy1 avatar Jul 11 '22 14:07 kdy1

neither works with es3, nor with es2021, nor without explicit target

DerGernTod avatar Jul 11 '22 14:07 DerGernTod

Sourcemapping is correctly working, while difference is how tsc picks up the mapping for the name vs. swc does.

For both, see the actual stack frame points swc-jest-stack-frame-name\src\test.ts:4:16


//ts-jest
at namedHandlerFn (C:\workspaces\repos\reproducers\swc-jest-stack-frame-name\src\test.ts:4:16)
//swc
at apply (C:\\workspaces\\repos\\reproducers\\swc-jest-stack-frame-name\\src\\test.ts:4:16)

and if we go to the actual source, https://github.com/DerGernTod/swc-jest-stack-frame-name/blob/2f672368aca1d86caac49d4c029779e58dff83ae/src/test.ts#L4=

it points to original code

fn.apply(this);
   ^ this is 16

which is actual execution stack. However, even though stack frame points to 4:16, tsc points those name into different mapping at https://github.com/DerGernTod/swc-jest-stack-frame-name/blob/2f672368aca1d86caac49d4c029779e58dff83ae/src/test.ts#L3= instead.

If we peek sourcemap generated by SWC, it includes apply as naming mapping symbol indeed

"names":["callFnWithError","fn","handler","namedHandlerFn","apply","setTimeout"]

I'm not able to say which one is correct behavior honestly, but if stack location points 4:16, and SWC's mapping correctly points naming symbol at 4:16 this seems this is difference to the other compiler, not a bug?

kwonoj avatar Jul 26 '22 22:07 kwonoj

Though, one thing to consider is in native stack at v8 it seems to pick up fn name correctly.

Error: irrelevant
    at Timeout.<anonymous> (/Users/ojkwon/github/swc/test.js:11:9)
    at Timeout.namedHandlerFn [as _onTimeout] (/Users/ojkwon/github/swc/test.js:4:10)

kwonoj avatar Jul 27 '22 00:07 kwonoj

This closed issue has been automatically locked because it had no new activity for a month. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you.

swc-bot avatar Oct 30 '22 12:10 swc-bot