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
  • Loading branch information
legendecas committed Jul 30, 2022
1 parent f2afcad commit 9beec4f
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 6 deletions.
12 changes: 7 additions & 5 deletions lib/internal/source_map/prepare_stack_trace.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,17 +139,22 @@ function getErrorSource(
originalLine,
originalColumn
) {
let exceptionLine = '';
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 Down Expand Up @@ -184,10 +189,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 9beec4f

Please sign in to comment.