TypeStrong / ts-node

TypeScript execution and REPL for node.js
https://typestrong.org/ts-node
MIT License
12.86k stars 536 forks source link

ts-node loses the class name from the stack trace when the error is thrown in a static method. #2046

Closed KostyaTretyak closed 1 year ago

KostyaTretyak commented 1 year ago

Search Terms

Expected Behavior

The class name must be present.

Actual Behavior

The word "Function" is inserted instead of the class name.

Steps to reproduce the problem

Call this code with ts-node:

class MyClass {
  static staticMethod() {
    throw new Error('Error in static method');
  }
}

MyClass.staticMethod();

And see the stack trace:

Error: Error in static method
    at Function.staticMethod

If you run the same code from node, the stack trace will be correct:

Error: Error in static method
    at MyClass.staticMethod

Specifications

ts-node v10.9.1 node v18.17.1 compiler v5.1.6 Ubuntu 22.04

cspotcode commented 1 year ago

ts-node is compiling the code via typescript compiler before executing it. What happens when you compile via tsc and then run in node? Same stack trace, or different?

KostyaTretyak commented 1 year ago

@cspotcode, just checked. If the compiled code reads node.js, then the class name is correct:

Error: Error in static method
    at MyClass.staticMethod
cspotcode commented 1 year ago

Ok thanks, can you share that reproduction somewhere? Can be a new git repo, then commit the package.json, tsconfig, .ts source, and compiled .js output. It'll help reproducing locally.

Also, one more thing to try: make sure tsc emits a sourcemap, then run node with --enable-source-maps

ts-node enables sourcemaps and its own mapping of stack traces, so if we can compare ts-node's mapping against what vanilla node is doing, we can see how they both handle sourcemapping.

KostyaTretyak commented 1 year ago

@cspotcode, I've provided the code above that needs to be tested to see the problem.

cspotcode commented 1 year ago

Hmm here's what I see:

Error
    at Function.Foo.bar 
KostyaTretyak commented 1 year ago

I think I found the culprit. When node is run with the --enable-source-maps option, the class name is also lost.