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

Stack traces broken for static methods with --enable-source-maps #55236

Open
mika-fischer opened this issue Oct 2, 2024 · 7 comments
Open

Stack traces broken for static methods with --enable-source-maps #55236

mika-fischer opened this issue Oct 2, 2024 · 7 comments
Labels
source maps Issues and PRs related to source map support.

Comments

@mika-fischer
Copy link
Contributor

Version

22.9.0

Platform

Microsoft Windows NT 10.0.22631.0 x64

Subsystem

No response

What steps will reproduce the bug?

With

class JSClass {
  static async foo() {
    throw new Error("JSClass");
  }
}

the stack trace without --enable-source-maps is

Error: JSClass
    at JSClass.foo (file:///C:/Users/mfischer/src/videmo/test/node-bug-enable-source-maps/index.mjs:3:15)

but with --enable-source-maps it becomes:

Error: JSClass
    at Function.foo (C:\Users\mfischer\src\videmo\test\node-bug-enable-source-maps\index.mts:3:11)

The class name is lost somewhere.

I also noticed that the class name is missing even without using --enable-source-maps when using prototype based inheritance instead of classes and also for napi classes created with napi_define_class (not included in repro below).

See detailed repro at https://github.com/mika-fischer/node-bug-enable-source-maps

So maybe the issue is not really with enable-source-map-support but that the standard CallSite serialization accesses some magic property that only works for real JS classes and is missing when using prototype based inheritance or napi_define_class. And enable-source-maps just uses the public accessors of CallSite, which don't seem to expose/use this property...

In any case I'm interested in both:

  • --enable-source-map should retain the class names
  • Static methods of classes defined with napi_define_class (and preferable also "classes" defined using protoype inheritance) should have the class name (with or without --enable-source-maps)

How often does it reproduce? Is there a required condition?

Always

What is the expected behavior? Why is that the expected behavior?

the stack trace should be

Error: JSClass
    at JSClass.foo (C:\Users\mfischer\src\videmo\test\node-bug-enable-source-maps\index.mts:3:11)

What do you see instead?

the stack trace is

Error: JSClass
    at Function.foo (C:\Users\mfischer\src\videmo\test\node-bug-enable-source-maps\index.mts:3:11)

Additional information

No response

@mika-fischer mika-fischer changed the title Stack traces borken for static methods with --enable-source-maps Stack traces broken for static methods with --enable-source-maps Oct 2, 2024
@RedYetiDev RedYetiDev added the source maps Issues and PRs related to source map support. label Oct 2, 2024
@RedYetiDev
Copy link
Member

RedYetiDev commented Oct 2, 2024

I can't reproduce:

class JSClass {
    static async foo() {
        throw new Error("JSClass");
    }
}

JSClass.foo();
$ node --enable-source-maps test.js
...
Error: JSClass
    at JSClass.foo (/test.js:3:15)
...
$ node test.js
...
Error: JSClass
    at JSClass.foo (/test.js:3:15)
...

$ npx esbuild test.js --outfile=build.js --sourcemap --minify
$ node --enable-source-maps build.js
...
Error: JSClass
    at JSClass.foo (/test.js:3:15)
...

@RedYetiDev RedYetiDev closed this as not planned Won't fix, can't repro, duplicate, stale Oct 2, 2024
@RedYetiDev
Copy link
Member

I've closed this, as it's not reproducible, but I'm happy to take another look if thats not the case.

Verify that your sourcemap is not faulty.

@mika-fischer
Copy link
Contributor Author

Did you try my repo @RedYetiDev ?

I think there are several shortcuts in the sourcemap implementation where it uses the normal toString() implementation if the sourcemap is not present or trivial...

@mika-fischer
Copy link
Contributor Author

mika-fischer commented Oct 2, 2024

@RedYetiDev I figured it out.

There are two issues:

  1. V8 has special handling in SerializeJSStackFrame for receivers that are JSClassConstructors. That's why it doesn't show the correct name for prototype inheritance and napi classes. If the special case is widened to all JSFunctions everything works as expected (with the standard Error.prepareStackTrace implementation, that uses SerializeJSStackFrame). See mika-fischer@9802b28

  2. The issue with --enable-source-maps is that the custom formatter obviously does not use the standard stack frame serialization but uses .getTypeName() etc. to format the stack frame itself. Unfortunately, the special handling mentioned above is not in GetTypeName but only in SerializeJSStackFrame, so that one cannot get the correct name via this interface... Moving the special handling from SerializeJSStackFrame to GetTypeName fixes the remaining issue with --enable-source-maps. See mika-fischer@3c30538 (updated: bed2637)

With these two patches I get:

❯ C:\Users\mfischer\src\libs\node\out\Release\node.exe .\index.mjs                     
Version: v23.0.0-pre
execArgv: []
Error: JSClass
    at JSClass.foo (file:///C:/Users/mfischer/src/videmo/test/node-bug-enable-source-maps/index.mjs:3:15)
    ...
Error: JSProto
    at JSProto.foo (file:///C:/Users/mfischer/src/videmo/test/node-bug-enable-source-maps/index.mjs:12:19)
    ...

❯ C:\Users\mfischer\src\libs\node\out\Release\node.exe --enable-source-maps .\index.mjs
Version: v23.0.0-pre
execArgv: ["--enable-source-maps"]
Error: JSClass
    at JSClass.foo (C:\Users\mfischer\src\videmo\test\node-bug-enable-source-maps\index.mts:3:11)
    ...
Error: JSProto
    at JSProto.foo (C:\Users\mfischer\src\videmo\test\node-bug-enable-source-maps\index.mts:13:13)
    ...

instead of

❯ node .\index.mjs                     
Version: v22.9.0
execArgv: []
Error: JSClass
    at JSClass.foo (file:///C:/Users/mfischer/src/videmo/test/node-bug-enable-source-maps/index.mjs:3:15)
    ...
Error: JSProto
    at Function.foo (file:///C:/Users/mfischer/src/videmo/test/node-bug-enable-source-maps/index.mjs:12:19)
    ...

❯ node --enable-source-maps .\index.mjs
Version: v22.9.0
execArgv: ["--enable-source-maps"]
Error: JSClass
    at Function.foo (C:\Users\mfischer\src\videmo\test\node-bug-enable-source-maps\index.mts:3:11)
    ...
Error: JSProto
    at Function.foo (C:\Users\mfischer\src\videmo\test\node-bug-enable-source-maps\index.mts:13:13)
    ...

What do you think?

Is this something that node is willing to patch to fix this? Or does this need to go through V8 (I never contributed there before...)?

@RedYetiDev RedYetiDev reopened this Oct 2, 2024
@RedYetiDev
Copy link
Member

CC @nodejs/v8

@joyeecheung
Copy link
Member

joyeecheung commented Oct 3, 2024

Or does this need to go through V8 (I never contributed there before...)?

Yes, in principle patches to deps/v8 should land in upstream first before being backported to Node.js. See https://github.com/nodejs/node/blob/main/doc/contributing/maintaining/maintaining-V8.md#unfixed-upstream-bugs

@mika-fischer
Copy link
Contributor Author

Upstream issue: https://issues.chromium.org/issues/371019881

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
source maps Issues and PRs related to source map support.
Projects
None yet
Development

No branches or pull requests

3 participants