src,lib: print prinstine source when source map source not found

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: https://github.com/nodejs/node/pull/44052
Refs: https://github.com/nodejs/node/pull/44019
Reviewed-By: Ben Coe <bencoe@gmail.com>
This commit is contained in:
legendecas 2022-08-06 19:56:27 +08:00
parent 994081f914
commit 0a2c910ae9
No known key found for this signature in database
GPG Key ID: CB3C9EC2BC27057C
7 changed files with 53 additions and 7 deletions

View File

@ -139,7 +139,6 @@ function getErrorSource(
originalLine,
originalColumn
) {
let exceptionLine = '';
const originalSourcePathNoScheme =
StringPrototypeStartsWith(originalSourcePath, 'file://') ?
fileURLToPath(originalSourcePath) : originalSourcePath;
@ -147,9 +146,14 @@ function getErrorSource(
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:
@ -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;
}
@ -184,10 +188,7 @@ function getOriginalSource(payload, originalSourcePath) {
source = readFileSync(originalSourcePathNoScheme, 'utf8');
} catch (err) {
debug(err);
source = '';
}
} else {
source = '';
}
return source;
}

View File

@ -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

9
test/fixtures/source-map/no-source.js vendored Normal file
View File

@ -0,0 +1,9 @@
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"
//# sourceMappingURL=no-source.js.map

View File

@ -0,0 +1 @@
{"version":3,"file":"no-source.js","sourceRoot":"","sources":["file-not-exists.ts"],"names":[],"mappings":"AAAA,SAAS,KAAK;IACZ,MAAM,IAAI,KAAK,CAAC,KAAK,CAAC,CAAC;AACzB,CAAC;AAED,KAAK,EAAE,CAAC;AAER,eAAe;AACf,EAAE;AACF,8FAA8F;AAC9F,kDAAkD"}

10
test/fixtures/source-map/no-source.ts vendored Normal file
View File

@ -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"

View File

@ -0,0 +1,6 @@
// Flags: --enable-source-maps
'use strict';
require('../common');
require('../fixtures/source-map/no-source.js');

View File

@ -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 *