From 023445850baa3d9a3b7f76bc24bf6cb7fa838756 Mon Sep 17 00:00:00 2001 From: Fabian Cook Date: Mon, 4 Apr 2022 04:21:38 +1200 Subject: [PATCH] lib: source maps filter null prefix Fixes: https://github.com/nodejs/node/issues/42417 PR-URL: https://github.com/nodejs/node/pull/42522 Reviewed-By: Benjamin Gruenbaum Reviewed-By: James M Snell --- lib/internal/source_map/prepare_stack_trace.js | 5 +++-- test/fixtures/source-map/throw-async.mjs | 11 +++++++++++ test/fixtures/source-map/throw-async.mjs.map | 1 + test/fixtures/source-map/throw-async.ts | 13 +++++++++++++ test/parallel/test-source-map-enable.js | 16 ++++++++++++++++ 5 files changed, 44 insertions(+), 2 deletions(-) create mode 100644 test/fixtures/source-map/throw-async.mjs create mode 100644 test/fixtures/source-map/throw-async.mjs.map create mode 100644 test/fixtures/source-map/throw-async.ts diff --git a/lib/internal/source_map/prepare_stack_trace.js b/lib/internal/source_map/prepare_stack_trace.js index 0635ee675418f5..551d3d50fee4b1 100644 --- a/lib/internal/source_map/prepare_stack_trace.js +++ b/lib/internal/source_map/prepare_stack_trace.js @@ -88,8 +88,9 @@ const prepareStackTrace = (globalThis, error, trace) => { } // Construct call site name based on: v8.dev/docs/stack-trace-api: const fnName = t.getFunctionName() ?? t.getMethodName(); - const originalName = `${t.getTypeName() !== 'global' ? - `${t.getTypeName()}.` : ''}${fnName || ''}`; + const typeName = t.getTypeName(); + const namePrefix = typeName !== null && typeName !== 'global' ? `${typeName}.` : ''; + const originalName = `${namePrefix}${fnName || ''}`; // The original call site may have a different symbol name // associated with it, use it: const prefix = (name && name !== originalName) ? diff --git a/test/fixtures/source-map/throw-async.mjs b/test/fixtures/source-map/throw-async.mjs new file mode 100644 index 00000000000000..a44412ef3ecc72 --- /dev/null +++ b/test/fixtures/source-map/throw-async.mjs @@ -0,0 +1,11 @@ +const message = 'Message ' + Math.random(); +export async function Throw() { + throw new Error(message); +} +await Throw(); +// To recreate: +// +// npx tsc --module esnext --target es2017 --outDir test/fixtures/source-map --sourceMap test/fixtures/source-map/throw-async.ts +// + rename js to mjs +// + rename js to mjs in source map comment in mjs file +//# sourceMappingURL=throw-async.mjs.map \ No newline at end of file diff --git a/test/fixtures/source-map/throw-async.mjs.map b/test/fixtures/source-map/throw-async.mjs.map new file mode 100644 index 00000000000000..33d1eaed6dd239 --- /dev/null +++ b/test/fixtures/source-map/throw-async.mjs.map @@ -0,0 +1 @@ +{"version":3,"file":"throw-async.mjs","sourceRoot":"","sources":["throw-async.ts"],"names":[],"mappings":"AAAA,MAAM,OAAO,GAAG,UAAU,GAAG,IAAI,CAAC,MAAM,EAAE,CAAC;AAE3C,MAAM,CAAC,KAAK,UAAU,KAAK;IACzB,MAAM,IAAI,KAAK,CAAC,OAAO,CAAC,CAAA;AAC1B,CAAC;AAED,MAAM,KAAK,EAAE,CAAC;AAEd,eAAe;AACf,EAAE;AACF,gIAAgI;AAChI,qBAAqB;AACrB,uDAAuD"} \ No newline at end of file diff --git a/test/fixtures/source-map/throw-async.ts b/test/fixtures/source-map/throw-async.ts new file mode 100644 index 00000000000000..080b07afa58184 --- /dev/null +++ b/test/fixtures/source-map/throw-async.ts @@ -0,0 +1,13 @@ +const message = 'Message ' + Math.random(); + +export async function Throw() { + throw new Error(message) +} + +await Throw(); + +// To recreate: +// +// npx tsc --module esnext --target es2017 --outDir test/fixtures/source-map --sourceMap test/fixtures/source-map/throw-async.ts +// + rename js to mjs +// + rename js to mjs in source map comment in mjs file \ No newline at end of file diff --git a/test/parallel/test-source-map-enable.js b/test/parallel/test-source-map-enable.js index 8e4f8e3a028062..dc00cf624ae8ea 100644 --- a/test/parallel/test-source-map-enable.js +++ b/test/parallel/test-source-map-enable.js @@ -343,6 +343,22 @@ function nextdir() { assert.ok(sourceMap); } +// Does not include null for async/await with esm +// Refs: https://github.com/nodejs/node/issues/42417 +{ + const output = spawnSync(process.execPath, [ + '--enable-source-maps', + require.resolve('../fixtures/source-map/throw-async.mjs'), + ]); + // Error in original context of source content: + assert.match( + output.stderr.toString(), + /throw new Error\(message\)\r?\n.*\^/ + ); + // Rewritten stack trace: + assert.match(output.stderr.toString(), /at Throw \([^)]+throw-async\.ts:4:9\)/); +} + function getSourceMapFromCache(fixtureFile, coverageDirectory) { const jsonFiles = fs.readdirSync(coverageDirectory); for (const jsonFile of jsonFiles) {