Skip to content

Commit ab46878

Browse files
committed
lib: skip source maps in node_modules by default
Signed-off-by: Chengzhong Wu <cwu631@bloomberg.net>
1 parent 3cac7b9 commit ab46878

8 files changed

Lines changed: 37 additions & 6 deletions

File tree

‎doc/api/cli.md‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1273,6 +1273,9 @@ is permitted.
12731273
<!-- YAML
12741274
added: v12.12.0
12751275
changes:
1276+
- version: REPLACEME
1277+
pr-url: https://github.com/nodejs/node/pull/67627
1278+
description: Source maps in `node_modules` are skipped by default.
12761279
- version:
12771280
- v15.11.0
12781281
- v14.18.0
@@ -1287,6 +1290,9 @@ application reference the transpiled code, not the original source position.
12871290
`--enable-source-maps` enables caching of Source Maps and makes a best
12881291
effort to report stack traces relative to the original source file.
12891292

1293+
Source maps of files in `node_modules` are skipped. Use
1294+
[`module.setSourceMapsSupport()`][] to enable source maps for them.
1295+
12901296
Overriding `Error.prepareStackTrace` may prevent `--enable-source-maps` from
12911297
modifying the stack trace. Call and return the results of the original
12921298
`Error.prepareStackTrace` in the overriding function to modify the stack trace
@@ -5005,6 +5011,7 @@ node --stack-trace-limit=12 -p -e "Error.stackTraceLimit" # prints 12
50055011
[`dnsPromises.lookup()`]: dns.md#dnspromiseslookuphostname-options
50065012
[`import.meta.url`]: esm.md#importmetaurl
50075013
[`inspector.open()`]: inspector.md#inspectoropenport-host-wait
5014+
[`module.setSourceMapsSupport()`]: module.md#modulesetsourcemapssupportenabled-options
50085015
[`net.getDefaultAutoSelectFamilyAttemptTimeout()`]: net.md#netgetdefaultautoselectfamilyattempttimeout
50095016
[`node:ffi`]: ffi.md
50105017
[`node:sqlite`]: sqlite.md

‎doc/api/module.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1271,7 +1271,7 @@ This function enables or disables the [Source Map v3][Source Map] support for
12711271
stack traces.
12721272
12731273
It provides same features as launching Node.js process with commandline options
1274-
`--enable-source-maps`, with additional options to alter the support for files
1274+
`--enable-source-maps`, with additional options to enable the support for files
12751275
in `node_modules` or generated codes.
12761276
12771277
Only source maps in JavaScript files that are loaded after source maps has been

‎doc/node.1‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -777,6 +777,8 @@ When using a transpiler, such as TypeScript, stack traces thrown by an
777777
application reference the transpiled code, not the original source position.
778778
\fB--enable-source-maps\fR enables caching of Source Maps and makes a best
779779
effort to report stack traces relative to the original source file.
780+
Source maps of files in \fBnode_modules\fR are skipped. Use
781+
\fBmodule.setSourceMapsSupport()\fR to enable source maps for them.
780782
Overriding \fBError.prepareStackTrace\fR may prevent \fB--enable-source-maps\fR from
781783
modifying the stack trace. Call and return the results of the original
782784
\fBError.prepareStackTrace\fR in the overriding function to modify the stack trace

‎lib/internal/process/pre_execution.js‎

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -804,12 +804,9 @@ function initializeSourceMapsHandlers() {
804804
setSourceMapsSupport,
805805
} = require('internal/source_map/source_map_cache');
806806
const enabled = getOptionValue('--enable-source-maps');
807+
// Source maps in node_modules are skipped by default.
807808
setSourceMapsSupport(enabled, {
808809
__proto__: null,
809-
// TODO(legendecas): In order to smoothly improve the source map support,
810-
// skip source maps in node_modules and generated code with
811-
// `--enable-source-maps` in a semver major version.
812-
nodeModules: enabled,
813810
generatedCode: enabled,
814811
});
815812
}

‎test/fixtures/source-map/output/source_map_disabled_by_api.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ Error.stackTraceLimit = 5;
99
assert.deepStrictEqual(Module.getSourceMapsSupport(), {
1010
__proto__: null,
1111
enabled: true,
12-
nodeModules: true,
12+
nodeModules: false,
1313
generatedCode: true,
1414
});
1515
Module.setSourceMapsSupport(false);
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Flags: --enable-source-maps
2+
3+
'use strict';
4+
require('../../../common');
5+
const assert = require('node:assert');
6+
const Module = require('node:module');
7+
Error.stackTraceLimit = 2;
8+
9+
// --enable-source-maps skips source maps in node_modules.
10+
assert.deepStrictEqual(Module.getSourceMapsSupport(), {
11+
__proto__: null,
12+
enabled: true,
13+
nodeModules: false,
14+
generatedCode: true,
15+
});
16+
17+
try {
18+
require('../node_modules/error-stack/enclosing-call-site-min.js').simpleErrorStack();
19+
} catch (e) {
20+
console.log(e);
21+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Error: an error!
2+
at functionD (<project-root>/test/fixtures/source-map/node_modules/error-stack/enclosing-call-site-min.js:1:156)
3+
at functionC (<project-root>/test/fixtures/source-map/node_modules/error-stack/enclosing-call-site-min.js:1:97)

‎test/parallel/test-node-output-sourcemaps.mjs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ describe('sourcemaps output', { concurrency: !process.env.TEST_PARALLEL }, () =>
99
{ name: 'source-map/output/source_map_assert_ok_no_source_map.cjs' },
1010
{ name: 'source-map/output/source_map_disabled_by_api.js' },
1111
{ name: 'source-map/output/source_map_disabled_by_process_api.js' },
12+
{ name: 'source-map/output/source_map_enable_source_maps_defaults.js' },
1213
{ name: 'source-map/output/source_map_enabled_by_api.js' },
1314
{ name: 'source-map/output/source_map_enabled_by_api_node_modules.js' },
1415
{ name: 'source-map/output/source_map_enabled_by_process_api.js' },

0 commit comments

Comments
 (0)