Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Incorrect stack frame name #5180

Closed
DerGernTod opened this issue Jul 11, 2022 · 5 comments · Fixed by #5657
Closed

Incorrect stack frame name #5180

DerGernTod opened this issue Jul 11, 2022 · 5 comments · Fixed by #5657
Assignees
Milestone

Comments

@DerGernTod
Copy link

DerGernTod commented Jul 11, 2022

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.

@kdy1
Copy link
Member

kdy1 commented Jul 11, 2022

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

@DerGernTod
Copy link
Author

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

@kdy1 kdy1 transferred this issue from swc-project/jest Jul 11, 2022
@kdy1 kdy1 added this to the Planned milestone Jul 11, 2022
@kwonoj
Copy link
Member

kwonoj commented Jul 26, 2022

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
Copy link
Member

kwonoj commented Jul 27, 2022

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)

@kdy1 kdy1 self-assigned this Aug 30, 2022
@kdy1 kdy1 modified the milestones: Planned, v1.3.4 Sep 30, 2022
@swc-bot
Copy link
Collaborator

swc-bot commented Oct 30, 2022

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-project swc-project locked as resolved and limited conversation to collaborators Oct 30, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Development

Successfully merging a pull request may close this issue.

4 participants