Skip to content

Commit

Permalink
process: add source-map support to stack traces
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
bcoe authored and BridgeAR committed Oct 9, 2019
1 parent 9e5d691 commit ae8b2b4
Show file tree
Hide file tree
Showing 33 changed files with 729 additions and 22 deletions.
10 changes: 10 additions & 0 deletions doc/api/cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,15 @@ added: v6.0.0
Enable FIPS-compliant crypto at startup. (Requires Node.js to be built with
`./configure --openssl-fips`.)

### `--enable-source-maps`
<!-- YAML
added: REPLACEME
-->

> Stability: 1 - Experimental
Enable experimental Source Map V3 support for stack traces.

### `--es-module-specifier-resolution=mode`
<!-- YAML
added: v12.0.0
Expand Down Expand Up @@ -1007,6 +1016,7 @@ node --require "./a.js" --require "./b.js"
Node.js options that are allowed are:
<!-- node-options-node start -->
* `--enable-fips`
* `--enable-source-maps`
* `--es-module-specifier-resolution`
* `--experimental-exports`
* `--experimental-loader`
Expand Down
12 changes: 11 additions & 1 deletion lib/internal/bootstrap/pre_execution.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,15 @@ function prepareMainThreadExecution(expandArgv1 = false) {
setupCoverageHooks(process.env.NODE_V8_COVERAGE);
}

// If source-map support has been enabled, we substitute in a new
// prepareStackTrace method, replacing the default in errors.js.
if (getOptionValue('--enable-source-maps')) {
const { prepareStackTrace } =
require('internal/source_map/source_map_cache');
const { setPrepareStackTraceCallback } = internalBinding('errors');
setPrepareStackTraceCallback(prepareStackTrace);
}

setupDebugEnv();

// Only main thread receives signals.
Expand Down Expand Up @@ -119,7 +128,8 @@ function setupCoverageHooks(dir) {
const cwd = require('internal/process/execution').tryGetCwd();
const { resolve } = require('path');
const coverageDirectory = resolve(cwd, dir);
const { sourceMapCacheToObject } = require('internal/source_map');
const { sourceMapCacheToObject } =
require('internal/source_map/source_map_cache');

if (process.features.inspector) {
internalBinding('profiler').setCoverageDirectory(coverageDirectory);
Expand Down
20 changes: 18 additions & 2 deletions lib/internal/modules/cjs/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,10 @@ const {
} = primordials;

const { NativeModule } = require('internal/bootstrap/loaders');
const { maybeCacheSourceMap } = require('internal/source_map');
const {
maybeCacheSourceMap,
rekeySourceMap
} = require('internal/source_map/source_map_cache');
const { pathToFileURL, fileURLToPath, URL } = require('internal/url');
const { deprecate } = require('internal/util');
const vm = require('vm');
Expand All @@ -52,6 +55,7 @@ const {
loadNativeModule
} = require('internal/modules/cjs/helpers');
const { getOptionValue } = require('internal/options');
const enableSourceMaps = getOptionValue('--enable-source-maps');
const preserveSymlinks = getOptionValue('--preserve-symlinks');
const preserveSymlinksMain = getOptionValue('--preserve-symlinks-main');
const experimentalModules = getOptionValue('--experimental-modules');
Expand Down Expand Up @@ -708,7 +712,19 @@ Module._load = function(request, parent, isMain) {

let threw = true;
try {
module.load(filename);
// Intercept exceptions that occur during the first tick and rekey them
// on error instance rather than module instance (which will immediately be
// garbage collected).
if (enableSourceMaps) {
try {
module.load(filename);
} catch (err) {
rekeySourceMap(Module._cache[filename], err);
throw err; /* node-do-not-add-exception-line */
}
} else {
module.load(filename);
}
threw = false;
} finally {
if (threw) {
Expand Down
2 changes: 1 addition & 1 deletion lib/internal/modules/esm/translators.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ const {
} = require('internal/errors').codes;
const readFileAsync = promisify(fs.readFile);
const JsonParse = JSON.parse;
const { maybeCacheSourceMap } = require('internal/source_map');
const { maybeCacheSourceMap } = require('internal/source_map/source_map_cache');

const debug = debuglog('esm');

Expand Down
Loading

0 comments on commit ae8b2b4

Please sign in to comment.