Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit ae8b2b4

Browse filesBrowse files
bcoeBridgeAR
authored andcommitted
process: add source-map support to stack traces
PR-URL: #29564 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent 9e5d691 commit ae8b2b4
Copy full SHA for ae8b2b4
Expand file treeCollapse file tree

33 files changed

+729
-22
lines changed
Open diff view settings
Collapse file

‎doc/api/cli.md‎

Copy file name to clipboardExpand all lines: doc/api/cli.md
+10Lines changed: 10 additions & 0 deletions
  • Display the source diff
  • Display the rich diff
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,15 @@ added: v6.0.0
135135
Enable FIPS-compliant crypto at startup. (Requires Node.js to be built with
136136
`./configure --openssl-fips`.)
137137

138+
### `--enable-source-maps`
139+
<!-- YAML
140+
added: REPLACEME
141+
-->
142+
143+
> Stability: 1 - Experimental
144+
145+
Enable experimental Source Map V3 support for stack traces.
146+
138147
### `--es-module-specifier-resolution=mode`
139148
<!-- YAML
140149
added: v12.0.0
@@ -1007,6 +1016,7 @@ node --require "./a.js" --require "./b.js"
10071016
Node.js options that are allowed are:
10081017
<!-- node-options-node start -->
10091018
* `--enable-fips`
1019+
* `--enable-source-maps`
10101020
* `--es-module-specifier-resolution`
10111021
* `--experimental-exports`
10121022
* `--experimental-loader`
Collapse file

‎lib/internal/bootstrap/pre_execution.js‎

Copy file name to clipboardExpand all lines: lib/internal/bootstrap/pre_execution.js
+11-1Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,15 @@ function prepareMainThreadExecution(expandArgv1 = false) {
2121
setupCoverageHooks(process.env.NODE_V8_COVERAGE);
2222
}
2323

24+
// If source-map support has been enabled, we substitute in a new
25+
// prepareStackTrace method, replacing the default in errors.js.
26+
if (getOptionValue('--enable-source-maps')) {
27+
const { prepareStackTrace } =
28+
require('internal/source_map/source_map_cache');
29+
const { setPrepareStackTraceCallback } = internalBinding('errors');
30+
setPrepareStackTraceCallback(prepareStackTrace);
31+
}
32+
2433
setupDebugEnv();
2534

2635
// Only main thread receives signals.
@@ -119,7 +128,8 @@ function setupCoverageHooks(dir) {
119128
const cwd = require('internal/process/execution').tryGetCwd();
120129
const { resolve } = require('path');
121130
const coverageDirectory = resolve(cwd, dir);
122-
const { sourceMapCacheToObject } = require('internal/source_map');
131+
const { sourceMapCacheToObject } =
132+
require('internal/source_map/source_map_cache');
123133

124134
if (process.features.inspector) {
125135
internalBinding('profiler').setCoverageDirectory(coverageDirectory);
Collapse file

‎lib/internal/modules/cjs/loader.js‎

Copy file name to clipboardExpand all lines: lib/internal/modules/cjs/loader.js
+18-2Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,10 @@ const {
3131
} = primordials;
3232

3333
const { NativeModule } = require('internal/bootstrap/loaders');
34-
const { maybeCacheSourceMap } = require('internal/source_map');
34+
const {
35+
maybeCacheSourceMap,
36+
rekeySourceMap
37+
} = require('internal/source_map/source_map_cache');
3538
const { pathToFileURL, fileURLToPath, URL } = require('internal/url');
3639
const { deprecate } = require('internal/util');
3740
const vm = require('vm');
@@ -52,6 +55,7 @@ const {
5255
loadNativeModule
5356
} = require('internal/modules/cjs/helpers');
5457
const { getOptionValue } = require('internal/options');
58+
const enableSourceMaps = getOptionValue('--enable-source-maps');
5559
const preserveSymlinks = getOptionValue('--preserve-symlinks');
5660
const preserveSymlinksMain = getOptionValue('--preserve-symlinks-main');
5761
const experimentalModules = getOptionValue('--experimental-modules');
@@ -708,7 +712,19 @@ Module._load = function(request, parent, isMain) {
708712

709713
let threw = true;
710714
try {
711-
module.load(filename);
715+
// Intercept exceptions that occur during the first tick and rekey them
716+
// on error instance rather than module instance (which will immediately be
717+
// garbage collected).
718+
if (enableSourceMaps) {
719+
try {
720+
module.load(filename);
721+
} catch (err) {
722+
rekeySourceMap(Module._cache[filename], err);
723+
throw err; /* node-do-not-add-exception-line */
724+
}
725+
} else {
726+
module.load(filename);
727+
}
712728
threw = false;
713729
} finally {
714730
if (threw) {
Collapse file

‎lib/internal/modules/esm/translators.js‎

Copy file name to clipboardExpand all lines: lib/internal/modules/esm/translators.js
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ const {
3232
} = require('internal/errors').codes;
3333
const readFileAsync = promisify(fs.readFile);
3434
const JsonParse = JSON.parse;
35-
const { maybeCacheSourceMap } = require('internal/source_map');
35+
const { maybeCacheSourceMap } = require('internal/source_map/source_map_cache');
3636

3737
const debug = debuglog('esm');
3838

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.