Skip to content

Commit 9beec4f

Browse files
committed
src,lib: print prinstine source when source map source not found
1 parent f2afcad commit 9beec4f

File tree

7 files changed

+53
-6
lines changed

7 files changed

+53
-6
lines changed

lib/internal/source_map/prepare_stack_trace.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -139,17 +139,22 @@ function getErrorSource(
139139
originalLine,
140140
originalColumn
141141
) {
142-
let exceptionLine = '';
142+
let exceptionLine;
143143
const originalSourcePathNoScheme =
144144
StringPrototypeStartsWith(originalSourcePath, 'file://') ?
145145
fileURLToPath(originalSourcePath) : originalSourcePath;
146146
const source = getOriginalSource(
147147
sourceMap.payload,
148148
originalSourcePath
149149
);
150+
if (typeof source !== 'string') {
151+
return;
152+
}
150153
const lines = RegExpPrototypeSymbolSplit(/\r?\n/, source, originalLine + 1);
151154
const line = lines[originalLine];
152-
if (!line) return exceptionLine;
155+
if (!line) {
156+
return;
157+
}
153158

154159
// Display ^ in appropriate position, regardless of whether tabs or
155160
// spaces are used:
@@ -184,10 +189,7 @@ function getOriginalSource(payload, originalSourcePath) {
184189
source = readFileSync(originalSourcePathNoScheme, 'utf8');
185190
} catch (err) {
186191
debug(err);
187-
source = '';
188192
}
189-
} else {
190-
source = '';
191193
}
192194
return source;
193195
}

src/node_errors.cc

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,9 @@ static std::string GetErrorSource(Isolate* isolate,
105105
if (has_source_map_url && env != nullptr && env->source_maps_enabled()) {
106106
std::string source = GetSourceMapErrorSource(
107107
isolate, context, message, added_exception_line);
108-
return *added_exception_line ? source : sourceline;
108+
if (*added_exception_line) {
109+
return source;
110+
}
109111
}
110112

111113
// Because of how node modules work, all scripts are wrapped with a

test/fixtures/source-map/no-source.js

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/fixtures/source-map/no-source.js.map

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
function Throw() {
2+
throw new Error('foo');
3+
}
4+
5+
Throw();
6+
7+
// To recreate:
8+
//
9+
// npx tsc --outDir test/fixtures/source-map --sourceMap test/fixtures/source-map/no-source.ts
10+
// rename the "source.[0]" to "file-not-exists.ts"
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// Flags: --enable-source-maps
2+
3+
'use strict';
4+
require('../common');
5+
6+
require('../fixtures/source-map/no-source.js');
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
*no-source.js:2
2+
throw new Error('foo');
3+
^
4+
5+
Error: foo
6+
at Throw (*file-not-exists.ts:2:9)
7+
at Object.<anonymous> (*file-not-exists.ts:5:1)
8+
at Module._compile (node:internal/modules/cjs/loader:*)
9+
at Module._extensions..js (node:internal/modules/cjs/loader:*)
10+
at Module.load (node:internal/modules/cjs/loader:*)
11+
at Module._load (node:internal/modules/cjs/loader:*)
12+
at Module.require (node:internal/modules/cjs/loader:*)
13+
at require (node:internal/modules/cjs/helpers:*)
14+
at Object.<anonymous> (*source_map_no_source_file.js:6:1)
15+
at Module._compile (node:internal/modules/cjs/loader:*)
16+
17+
Node.js *

0 commit comments

Comments
 (0)