Skip to content
Open
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
8 changes: 8 additions & 0 deletions doc/api/cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -1273,6 +1273,9 @@ is permitted.
<!-- YAML
added: v12.12.0
changes:
- version: REPLACEME
pr-url: https://github.com/nodejs/node/pull/67627
description: Source maps in `node_modules` and generated code are skipped.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Given there's an option to unskip, it's probably a good idea add "by default"

Suggested change
description: Source maps in `node_modules` and generated code are skipped.
description: Source maps in `node_modules` and generated code are skipped by default.

- version:
- v15.11.0
- v14.18.0
Expand All @@ -1287,6 +1290,10 @@ application reference the transpiled code, not the original source position.
`--enable-source-maps` enables caching of Source Maps and makes a best
effort to report stack traces relative to the original source file.

Source maps of files in `node_modules` and of generated code from `eval` or
`new Function` are skipped. Use [`module.setSourceMapsSupport()`][] to enable
source maps for them.

Overriding `Error.prepareStackTrace` may prevent `--enable-source-maps` from
modifying the stack trace. Call and return the results of the original
`Error.prepareStackTrace` in the overriding function to modify the stack trace
Expand Down Expand Up @@ -4997,6 +5004,7 @@ node --stack-trace-limit=12 -p -e "Error.stackTraceLimit" # prints 12
[`dnsPromises.lookup()`]: dns.md#dnspromiseslookuphostname-options
[`import.meta.url`]: esm.md#importmetaurl
[`inspector.open()`]: inspector.md#inspectoropenport-host-wait
[`module.setSourceMapsSupport()`]: module.md#modulesetsourcemapssupportenabled-options
[`net.getDefaultAutoSelectFamilyAttemptTimeout()`]: net.md#netgetdefaultautoselectfamilyattempttimeout
[`node:ffi`]: ffi.md
[`node:sqlite`]: sqlite.md
Expand Down
2 changes: 1 addition & 1 deletion doc/api/module.md
Original file line number Diff line number Diff line change
Expand Up @@ -1268,7 +1268,7 @@ This function enables or disables the [Source Map v3][Source Map] support for
stack traces.

It provides same features as launching Node.js process with commandline options
`--enable-source-maps`, with additional options to alter the support for files
`--enable-source-maps`, with additional options to enable the support for files
in `node_modules` or generated codes.

Only source maps in JavaScript files that are loaded after source maps has been
Expand Down
3 changes: 3 additions & 0 deletions doc/node.1
Original file line number Diff line number Diff line change
Expand Up @@ -777,6 +777,9 @@ When using a transpiler, such as TypeScript, stack traces thrown by an
application reference the transpiled code, not the original source position.
\fB--enable-source-maps\fR enables caching of Source Maps and makes a best
effort to report stack traces relative to the original source file.
Source maps of files in \fBnode_modules\fR and of generated code from \fBeval\fR or
\fBnew Function\fR are skipped. Use \fBmodule.setSourceMapsSupport()\fR to enable
source maps for them.
Overriding \fBError.prepareStackTrace\fR may prevent \fB--enable-source-maps\fR from
modifying the stack trace. Call and return the results of the original
\fBError.prepareStackTrace\fR in the overriding function to modify the stack trace
Expand Down
11 changes: 2 additions & 9 deletions lib/internal/process/pre_execution.js
Original file line number Diff line number Diff line change
Expand Up @@ -803,15 +803,8 @@ function initializeSourceMapsHandlers() {
const {
setSourceMapsSupport,
} = require('internal/source_map/source_map_cache');
const enabled = getOptionValue('--enable-source-maps');
setSourceMapsSupport(enabled, {
__proto__: null,
// TODO(legendecas): In order to smoothly improve the source map support,
// skip source maps in node_modules and generated code with
// `--enable-source-maps` in a semver major version.
nodeModules: enabled,
generatedCode: enabled,
});
// Source maps in node_modules and generated code are skipped by default.
setSourceMapsSupport(getOptionValue('--enable-source-maps'));
}

function initializeFrozenIntrinsics() {
Expand Down
2 changes: 1 addition & 1 deletion lib/internal/source_map/source_map_cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ function maybeCacheSourceMap(filename, content, moduleInstance, isGeneratedSourc
*/
function maybeCacheGeneratedSourceMap(content) {
const support = getSourceMapsSupport();
if (!(process.env.NODE_V8_COVERAGE || support.enabled || support.generated)) return;
if (!(process.env.NODE_V8_COVERAGE || (support.enabled && support.generatedCode))) return;

const sourceURL = extractSourceURLMagicComment(content);
if (sourceURL === null) {
Expand Down
4 changes: 2 additions & 2 deletions test/fixtures/source-map/output/source_map_disabled_by_api.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ Error.stackTraceLimit = 5;
assert.deepStrictEqual(Module.getSourceMapsSupport(), {
__proto__: null,
enabled: true,
nodeModules: true,
generatedCode: true,
nodeModules: false,
generatedCode: false,
});
Module.setSourceMapsSupport(false);
assert.deepStrictEqual(Module.getSourceMapsSupport(), {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Flags: --enable-source-maps

'use strict';
require('../../../common');
const assert = require('node:assert');
const Module = require('node:module');
const fs = require('node:fs');
Error.stackTraceLimit = 2;

// --enable-source-maps skips source maps in node_modules and generated code.
assert.deepStrictEqual(Module.getSourceMapsSupport(), {
__proto__: null,
enabled: true,
nodeModules: false,
generatedCode: false,
});

try {
require('../node_modules/error-stack/enclosing-call-site-min.js').simpleErrorStack();
} catch (e) {
console.log(e);
}

const content = fs.readFileSync(require.resolve('../tabs-source-url.js'), 'utf8');
try {
// SourceURL magic comment is hardcoded in the source content.
eval(content);
} catch (e) {
console.log(e);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Error: an error!
at functionD (<project-root>/test/fixtures/source-map/node_modules/error-stack/enclosing-call-site-min.js:1:156)
at functionC (<project-root>/test/fixtures/source-map/node_modules/error-stack/enclosing-call-site-min.js:1:97)
ReferenceError: alert is not defined
at Object.eval (./synthesized/workspace/tabs-source-url.coffee:39:5)
at eval (./synthesized/workspace/tabs-source-url.coffee:58:4)
7 changes: 5 additions & 2 deletions test/fixtures/source-map/output/source_map_eval.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
// Flags: --enable-source-maps

'use strict';
require('../../../common');
const Module = require('node:module');
Error.stackTraceLimit = 3;

Module.setSourceMapsSupport(true, {
generatedCode: true,
});

const fs = require('fs');

const content = fs.readFileSync(require.resolve('../tabs-source-url.js'), 'utf8');
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/source-map/output/source_map_eval.snapshot
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@
ReferenceError: alert is not defined
at Object.eval (<project-root>/synthesized/workspace/tabs-source-url.coffee:26:2)
at eval (<project-root>/synthesized/workspace/tabs-source-url.coffee:1:14)
at Object.<anonymous> (<project-root>/test/fixtures/source-map/output/source_map_eval.js:11:1)
at Object.<anonymous> (<project-root>/test/fixtures/source-map/output/source_map_eval.js:14:1)

Node.js <node-version>
1 change: 1 addition & 0 deletions test/parallel/test-node-output-sourcemaps.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ describe('sourcemaps output', { concurrency: !process.env.TEST_PARALLEL }, () =>
{ name: 'source-map/output/source_map_assert_ok_no_source_map.cjs' },
{ name: 'source-map/output/source_map_disabled_by_api.js' },
{ name: 'source-map/output/source_map_disabled_by_process_api.js' },
{ name: 'source-map/output/source_map_enable_source_maps_defaults.js' },
{ name: 'source-map/output/source_map_enabled_by_api.js' },
{ name: 'source-map/output/source_map_enabled_by_api_node_modules.js' },
{ name: 'source-map/output/source_map_enabled_by_process_api.js' },
Expand Down
Loading