TypeStrong / ts-node

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

When running .js files using allowJs, ts-node does not respect source maps in stack traces #1903

Open adamsmasher opened 1 year ago

adamsmasher commented 1 year ago

Search Terms

I searched for allowJs and didn't see anything clearly related in either open or closed issues.

Expected Behavior

When executing transpiled .js files with a source map using allowJs, ts-node should work like node --enable-source-maps: stack traces should show the original source file/line number, not the transpiled file/line number.

Actual Behavior

The opposite: ts-node shows the transpiled file/line number. This only happens when allowJs is on - turning it off causes ts-node to work correctly.

Steps to reproduce the problem

Enable allowJs and sourceMaps in tsconfig.json. Transpile files using tsc. Run the transpiled files using ts-node. When an exception is thrown, note that the stack trace refers to the transpiled files and not the original .ts files. Compare to running with node --enable-source-maps, which does the right thing.

Minimal reproduction

I've put up a yarn 1 repo here showing this in action. After running yarn tsc, yarn ts-node dist/index.js produces:

% yarn ts-node dist/index.js
yarn run v1.22.17
$ /Users/adam/source-maps-issue/node_modules/.bin/ts-node dist/index.js
/Users/adam/source-maps-issue/dist/bad.js:5
    throw new Error("An exception");
          ^
Error: An exception
    at throwAnException (/Users/adam/source-maps-issue/dist/bad.js:5:11)
    at Object.<anonymous> (/Users/adam/source-maps-issue/dist/index.js:4:28)
    at Module._compile (node:internal/modules/cjs/loader:1101:14)
    at Module.m._compile (/Users/adam/source-maps-issue/node_modules/ts-node/src/index.ts:1618:23)
    at Module._extensions..js (node:internal/modules/cjs/loader:1153:10)
    at Object.require.extensions.<computed> [as .js] (/Users/adam/source-maps-issue/node_modules/ts-node/src/index.ts:1621:12)
    at Module.load (node:internal/modules/cjs/loader:981:32)
    at Function.Module._load (node:internal/modules/cjs/loader:822:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)
    at phase4 (/Users/adam/source-maps-issue/node_modules/ts-node/src/bin.ts:649:14)

where as yarn node --enable-stack-traces dist/index.js produces:

% yarn node --enable-source-maps dist/index.js
yarn node v1.22.17
/Users/adam/source-maps-issue/src/bad.ts:2
  throw new Error("An exception");
        ^

Error: An exception
    at null.throwAnException (/Users/adam/source-maps-issue/src/bad.ts:2:9)
    at Object.<anonymous> (/Users/adam/source-maps-issue/src/index.ts:3:17)
    at Module._compile (node:internal/modules/cjs/loader:1101:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10)
    at Module.load (node:internal/modules/cjs/loader:981:32)
    at Function.Module._load (node:internal/modules/cjs/loader:822:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)
    at node:internal/main/run_main_module:17:47

Specifications

ts-node v10.9.1 node v16.13.0 compiler v4.8.4

cspotcode commented 1 year ago

Makes sense; it is compiling the JS and generating a new sourcemap. Possible workarounds: