Skip to content

Commit

Permalink
src,lib: print prinstine source when source map source not found
Browse files Browse the repository at this point in the history
Print unmapped source lines when the source map source is not
found. Error stacks should be correctly mapped even when the
source is absent.

PR-URL: nodejs#44052
Refs: nodejs#44019
Reviewed-By: Ben Coe <bencoe@gmail.com>
  • Loading branch information
legendecas authored and Fyko committed Sep 15, 2022
1 parent 3d99a4e commit fb180fb
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 7 deletions.
13 changes: 7 additions & 6 deletions lib/internal/source_map/prepare_stack_trace.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,17 +139,21 @@ function getErrorSource(
originalLine,
originalColumn
) {
let exceptionLine = '';
const originalSourcePathNoScheme =
StringPrototypeStartsWith(originalSourcePath, 'file://') ?
fileURLToPath(originalSourcePath) : originalSourcePath;
const source = getOriginalSource(
sourceMap.payload,
originalSourcePath
);
if (typeof source !== 'string') {
return;
}
const lines = RegExpPrototypeSymbolSplit(/\r?\n/, source, originalLine + 1);
const line = lines[originalLine];
if (!line) return exceptionLine;
if (!line) {
return;
}

// Display ^ in appropriate position, regardless of whether tabs or
// spaces are used:
Expand All @@ -161,7 +165,7 @@ function getErrorSource(
}
prefix = StringPrototypeSlice(prefix, 0, -1); // The last character is '^'.

exceptionLine =
const exceptionLine =
`${originalSourcePathNoScheme}:${originalLine + 1}\n${line}\n${prefix}^\n\n`;
return exceptionLine;
}
Expand All @@ -184,10 +188,7 @@ function getOriginalSource(payload, originalSourcePath) {
source = readFileSync(originalSourcePathNoScheme, 'utf8');
} catch (err) {
debug(err);
source = '';
}
} else {
source = '';
}
return source;
}
Expand Down
4 changes: 3 additions & 1 deletion src/node_errors.cc
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,9 @@ static std::string GetErrorSource(Isolate* isolate,
if (has_source_map_url && env != nullptr && env->source_maps_enabled()) {
std::string source = GetSourceMapErrorSource(
isolate, context, message, added_exception_line);
return *added_exception_line ? source : sourceline;
if (*added_exception_line) {
return source;
}
}

// Because of how node modules work, all scripts are wrapped with a
Expand Down
9 changes: 9 additions & 0 deletions test/fixtures/source-map/no-source.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions test/fixtures/source-map/no-source.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions test/fixtures/source-map/no-source.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
function Throw() {
throw new Error('foo');
}

Throw();

// To recreate:
//
// npx tsc --outDir test/fixtures/source-map --sourceMap test/fixtures/source-map/no-source.ts
// rename the "source.[0]" to "file-not-exists.ts"
6 changes: 6 additions & 0 deletions test/message/source_map_no_source_file.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// Flags: --enable-source-maps

'use strict';
require('../common');

require('../fixtures/source-map/no-source.js');
17 changes: 17 additions & 0 deletions test/message/source_map_no_source_file.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
*no-source.js:2
throw new Error('foo');
^

Error: foo
at Throw (*file-not-exists.ts:2:9)
at Object.<anonymous> (*file-not-exists.ts:5:1)
at Module._compile (node:internal/modules/cjs/loader:*)
at Module._extensions..js (node:internal/modules/cjs/loader:*)
at Module.load (node:internal/modules/cjs/loader:*)
at Module._load (node:internal/modules/cjs/loader:*)
at Module.require (node:internal/modules/cjs/loader:*)
at require (node:internal/modules/cjs/helpers:*)
at Object.<anonymous> (*source_map_no_source_file.js:6:1)
at Module._compile (node:internal/modules/cjs/loader:*)

Node.js *

0 comments on commit fb180fb

Please sign in to comment.