Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}
Comment on lines +149 to +151

This comment was marked as outdated.

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 *