From 69012b90a99292b91ce1ff60251cd53751572d52 Mon Sep 17 00:00:00 2001 From: Andrew Bradley Date: Fri, 29 Apr 2022 18:04:15 +0000 Subject: [PATCH] Ensure source snippets also output URLs/paths in the correct format, matching stack frame --- source-map-support.js | 6 +++--- test.js | 18 +++++++++--------- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/source-map-support.js b/source-map-support.js index 3d3dc03..5ee9249 100644 --- a/source-map-support.js +++ b/source-map-support.js @@ -617,12 +617,12 @@ function getErrorSource(error) { // Support the inline sourceContents inside the source map var contents = getFileContentsCache(source); - source = tryFileURLToPath(source); + const sourceAsPath = tryFileURLToPath(source); // Support files on disk - if (!contents && fs && fs.existsSync(source)) { + if (!contents && fs && fs.existsSync(sourceAsPath)) { try { - contents = fs.readFileSync(source, 'utf8'); + contents = fs.readFileSync(sourceAsPath, 'utf8'); } catch (er) { contents = ''; } diff --git a/test.js b/test.js index 279f662..0e1f41f 100644 --- a/test.js +++ b/test.js @@ -446,7 +446,7 @@ it('native function', async function() { '[1].map(function(x) { throw new Error(x); });' ], [ 'Error: 1', - re`[/\\].original-${id}.js`, + re`${stackFramePathStartsWith()}(?:.*[/\\])?.original-${id}.js`, /at Array\.map \((native|)\)/ ]); }); @@ -475,7 +475,7 @@ it('throw in Timeout with empty source map', function(done) { ' throw new Error("this is the error")', '})' ], [ - re`[/\\].generated-${id}.${extension}:3$`, + re`${stackFramePathStartsWith()}(?:.*[/\\])?.generated-${id}.${extension}:3$`, ' throw new Error("this is the error")', /^ \^$/, 'Error: this is the error', @@ -544,7 +544,7 @@ it('default options', function(done) { 'process.nextTick(foo);', 'process.nextTick(function() { process.exit(1); });' ], [ - re`[/\\].original-${id}\.js:1$`, + re`${stackFramePathStartsWith()}(?:.*[/\\])?.original-${id}\.js:1$`, 'this is the original code', '^', 'Error: this is the error', @@ -559,7 +559,7 @@ it('handleUncaughtExceptions is true', function(done) { 'require("./source-map-support").install({ handleUncaughtExceptions: true });', 'process.nextTick(foo);' ], [ - re`[/\\].original-${id}\.js:1$`, + re`${stackFramePathStartsWith()}(?:.*[/\\])?.original-${id}\.js:1$`, 'this is the original code', '^', 'Error: this is the error', @@ -574,7 +574,7 @@ it('handleUncaughtExceptions is false', function(done) { 'require("./source-map-support").install({ handleUncaughtExceptions: false });', 'process.nextTick(foo);' ], [ - re`[/\\].generated-${id}.${extension}:2$`, + re`${stackFramePathStartsWith()}(?:.*[/\\])?.generated-${id}.${extension}:2$`, 'function foo() { throw new Error("this is the error"); }', ' ^', @@ -591,7 +591,7 @@ it('default options with empty source map', function(done) { 'require("./source-map-support").install();', 'process.nextTick(foo);' ], [ - re`[/\\].generated-${id}.${extension}:2$`, + re`${stackFramePathStartsWith()}(?:.*[/\\])?.generated-${id}.${extension}:2$`, 'function foo() { throw new Error("this is the error"); }', ' ^', 'Error: this is the error', @@ -606,7 +606,7 @@ it('default options with source map with gap', function(done) { 'require("./source-map-support").install();', 'process.nextTick(foo);' ], [ - re`[/\\].generated-${id}.${extension}:2$`, + re`${stackFramePathStartsWith()}(?:.*[/\\])?.generated-${id}.${extension}:2$`, 'function foo() { throw new Error("this is the error"); }', ' ^', 'Error: this is the error', @@ -637,7 +637,7 @@ it('sourcesContent', function(done) { 'process.nextTick(foo);', 'process.nextTick(function() { process.exit(1); });' ], [ - re`[/\\]original-${id}\.js:1002$`, + re`${stackFramePathStartsWith()}(?:.*[/\\])?original-${id}\.js:1002$`, ' line 2', ' ^', 'Error: this is the error', @@ -851,7 +851,7 @@ it('supports multiple instances', function(done) { ' process.nextTick(function() { process.exit(1); });', '})();' ], [ - re`[/\\].original2-${id}\.js:1$`, + re`${stackFramePathStartsWith()}(?:.*[/\\])?.original2-${id}\.js:1$`, 'this is some other original code', '^', 'Error: this is the error',