swc-project / swc-node

Faster ts-node without typecheck
MIT License
1.78k stars 75 forks source link

.swcrc config file sourceMaps option is ignored and always set to "inline", causing incorrect stack traces when using a .swcrc file #741

Closed dsyddall closed 10 months ago

dsyddall commented 10 months ago

Source maps are always being configured as inline when using a .swcrc file. This causes incorrect line numbers to be reported in stack traces because source-map-support requires the hookRequire option to be set to true to process inline source maps, however @swc-node/sourcemap-support does not set this option and there is no way to configure it to do so.

Reproduction

tsconfig.json

{
  "compilerOptions": {
    "module": "CommonJS",
    "sourceMap": true
  }
}

.swcrc

{
  "jsc": {
    "parser": {
      "syntax": "typescript",
    },
  },
  "sourceMaps": true,
  "module": {
    "type": "commonjs",
  }
}

index.ts

throw new Error();

When running node -r @swc-node/register ./index.ts, the line number (1) in the first line of error's stack trace is correct:

$ node -r @swc-node/register ./index.ts 
/swc-node/test/index.ts:2
throw new Error();
^

Error: 
    at Object.<anonymous> (/swc-node/test/index.ts:1:7)
    at Module._compile (node:internal/modules/cjs/loader:1241:14)
    at Module._compile (/swc-node/node_modules/.pnpm/pirates@4.0.5/node_modules/pirates/lib/index.js:136:24)
    at Module._extensions..js (node:internal/modules/cjs/loader:1295:10)
    at Object.newLoader [as .ts] (/swc-node/node_modules/.pnpm/pirates@4.0.5/node_modules/pirates/lib/index.js:141:7)
    at Module.load (node:internal/modules/cjs/loader:1091:32)
    at Function.Module._load (node:internal/modules/cjs/loader:938:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:83:12)
    at node:internal/main/run_main_module:23:47

Node.js v20.9.0

However, when using the .swcrc file with SWCRC=true node -r @swc-node/register ./index.ts, the line number (2) in the first line of the stack trace is incorrect:

$ SWCRC=true node -r @swc-node/register ./index.ts 
/swc-node/test/index.ts:2
throw new Error();
^

Error: 
    at Object.<anonymous> (/swc-node/test/index.ts:2:7)
    at Module._compile (node:internal/modules/cjs/loader:1241:14)
    at Module._compile (/swc-node/node_modules/.pnpm/pirates@4.0.5/node_modules/pirates/lib/index.js:136:24)
    at Module._extensions..js (node:internal/modules/cjs/loader:1295:10)
    at Object.newLoader [as .ts] (/swc-node/node_modules/.pnpm/pirates@4.0.5/node_modules/pirates/lib/index.js:141:7)
    at Module.load (node:internal/modules/cjs/loader:1091:32)
    at Function.Module._load (node:internal/modules/cjs/loader:938:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:83:12)
    at node:internal/main/run_main_module:23:47

Node.js v20.9.0

Although the sourceMaps config option can take the values true | "inline" | false, the value in the .swcrc file is ignored and it is always being being set as "inline" in the options passed to SWC. This causes sourcemaps to not be handled correctly by source-map-support, since inline sourcemaps support requires setting the hookRequire option.