Skip to content

Commit ae8b2b4

Browse 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

33 files changed

Lines changed: 729 additions & 22 deletions

‎doc/api/cli.md‎

Lines changed: 10 additions & 0 deletions
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`

‎lib/internal/bootstrap/pre_execution.js‎

Lines 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
constcwd=require('internal/process/execution').tryGetCwd();
120129
const{ resolve }=require('path');
121130
constcoverageDirectory=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);

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

Lines 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
constvm=require('vm');
@@ -52,6 +55,7 @@ const {
5255
loadNativeModule
5356
}=require('internal/modules/cjs/helpers');
5457
const{ getOptionValue }=require('internal/options');
58+
constenableSourceMaps=getOptionValue('--enable-source-maps');
5559
constpreserveSymlinks=getOptionValue('--preserve-symlinks');
5660
constpreserveSymlinksMain=getOptionValue('--preserve-symlinks-main');
5761
constexperimentalModules=getOptionValue('--experimental-modules');
@@ -708,7 +712,19 @@ Module._load = function(request, parent, isMain) {
708712

709713
letthrew=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+
throwerr;/* node-do-not-add-exception-line */
724+
}
725+
}else{
726+
module.load(filename);
727+
}
712728
threw=false;
713729
}finally{
714730
if(threw){

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ const {
3232
}=require('internal/errors').codes;
3333
constreadFileAsync=promisify(fs.readFile);
3434
constJsonParse=JSON.parse;
35-
const{ maybeCacheSourceMap }=require('internal/source_map');
35+
const{ maybeCacheSourceMap }=require('internal/source_map/source_map_cache');
3636

3737
constdebug=debuglog('esm');
3838

0 commit comments

Comments
 (0)