diff --git a/lib/internal/errors/error_source.js b/lib/internal/errors/error_source.js index eddd6af230801b..c17f5a151c7014 100644 --- a/lib/internal/errors/error_source.js +++ b/lib/internal/errors/error_source.js @@ -32,6 +32,10 @@ function getErrorSourceLocation(error) { startColumn, } = pos; + if (!sourceLine) { + return; + } + // Source map is not enabled. Return the source line directly. if (!getSourceMapsSupport().enabled) { return { sourceLine, startColumn }; @@ -39,7 +43,8 @@ function getErrorSourceLocation(error) { const sm = findSourceMap(scriptResourceName); if (sm === undefined) { - return; + // No source map for this file; use the generated source line. + return { sourceLine, startColumn }; } const { originalLine, @@ -49,7 +54,9 @@ function getErrorSourceLocation(error) { const originalSourceLine = getSourceLine(sm, originalSource, originalLine, originalColumn); if (!originalSourceLine) { - return; + // Source map exists but original source is unavailable; use the + // generated source line rather than returning undefined. + return { sourceLine, startColumn }; } return { diff --git a/test/fixtures/source-map/output/source_map_assert_no_source_map.mjs b/test/fixtures/source-map/output/source_map_assert_no_source_map.mjs new file mode 100644 index 00000000000000..d6035109d56db8 --- /dev/null +++ b/test/fixtures/source-map/output/source_map_assert_no_source_map.mjs @@ -0,0 +1,9 @@ +// Flags: --enable-source-maps + +import '../../../common/index.mjs'; +import { strict as assert } from 'node:assert'; + +// Regression test for https://github.com/nodejs/node/issues/63169 +// Under --enable-source-maps with no source map for this file, a failing +// assert(value) must throw AssertionError, not TypeError ERR_INVALID_ARG_TYPE. +assert(false); diff --git a/test/fixtures/source-map/output/source_map_assert_no_source_map.snapshot b/test/fixtures/source-map/output/source_map_assert_no_source_map.snapshot new file mode 100644 index 00000000000000..0a2ea1a45fe6f3 --- /dev/null +++ b/test/fixtures/source-map/output/source_map_assert_no_source_map.snapshot @@ -0,0 +1,20 @@ +node:internal/modules/run_main: + triggerUncaughtException( + ^ + +AssertionError [ERR_ASSERTION]: The expression evaluated to a falsy value: + + assert(false) + + at file:///test/fixtures/source-map/output/source_map_assert_no_source_map.mjs:9:1 + at + at { + generatedMessage: true, + code: 'ERR_ASSERTION', + actual: false, + expected: true, + operator: '==', + diff: 'simple' +} + +Node.js diff --git a/test/fixtures/source-map/output/source_map_assert_ok_no_source_map.cjs b/test/fixtures/source-map/output/source_map_assert_ok_no_source_map.cjs new file mode 100644 index 00000000000000..21f2ec8965fb05 --- /dev/null +++ b/test/fixtures/source-map/output/source_map_assert_ok_no_source_map.cjs @@ -0,0 +1,10 @@ +// Flags: --enable-source-maps + +'use strict'; +require('../../../common'); +const assert = require('node:assert'); + +// Regression test for https://github.com/nodejs/node/issues/63169 +// Under --enable-source-maps with no source map for this file, a failing +// assert.ok(value) must throw AssertionError, not TypeError ERR_INVALID_ARG_TYPE. +assert.ok(false); diff --git a/test/fixtures/source-map/output/source_map_assert_ok_no_source_map.snapshot b/test/fixtures/source-map/output/source_map_assert_ok_no_source_map.snapshot new file mode 100644 index 00000000000000..8ee5ff2ee29498 --- /dev/null +++ b/test/fixtures/source-map/output/source_map_assert_ok_no_source_map.snapshot @@ -0,0 +1,20 @@ +node:internal/assert/utils: + throw error; + ^ + +AssertionError [ERR_ASSERTION]: The expression evaluated to a falsy value: + + assert.ok(false) + + at Object. (/test/fixtures/source-map/output/source_map_assert_ok_no_source_map.cjs:10:8) + at + at { + generatedMessage: true, + code: 'ERR_ASSERTION', + actual: false, + expected: true, + operator: '==', + diff: 'simple' +} + +Node.js diff --git a/test/parallel/test-node-output-sourcemaps.mjs b/test/parallel/test-node-output-sourcemaps.mjs index 26baa0cd53afed..c96eac2bd0310c 100644 --- a/test/parallel/test-node-output-sourcemaps.mjs +++ b/test/parallel/test-node-output-sourcemaps.mjs @@ -5,6 +5,8 @@ import { describe, it } from 'node:test'; describe('sourcemaps output', { concurrency: !process.env.TEST_PARALLEL }, () => { const tests = [ + { name: 'source-map/output/source_map_assert_no_source_map.mjs' }, + { 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_enabled_by_api.js' },