Skip to content

Commit 6a0b05f

Browse files
committed
lib: fix ERR_INVALID_ARG_TYPE with --enable-source-maps
Signed-off-by: June Kim <kimjune01@gmail.com>
1 parent cd58afc commit 6a0b05f

4 files changed

Lines changed: 78 additions & 2 deletions

File tree

lib/internal/errors/error_source.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,19 @@ function getErrorSourceLocation(error) {
3232
startColumn,
3333
} = pos;
3434

35+
if (!sourceLine) {
36+
return;
37+
}
38+
3539
// Source map is not enabled. Return the source line directly.
3640
if (!getSourceMapsSupport().enabled) {
3741
return { sourceLine, startColumn };
3842
}
3943

4044
const sm = findSourceMap(scriptResourceName);
4145
if (sm === undefined) {
42-
return;
46+
// No source map for this file; use the generated source line.
47+
return { sourceLine, startColumn };
4348
}
4449
const {
4550
originalLine,
@@ -49,7 +54,9 @@ function getErrorSourceLocation(error) {
4954
const originalSourceLine = getSourceLine(sm, originalSource, originalLine, originalColumn);
5055

5156
if (!originalSourceLine) {
52-
return;
57+
// Source map exists but original source is unavailable; use the
58+
// generated source line rather than returning undefined.
59+
return { sourceLine, startColumn };
5360
}
5461

5562
return {
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
'use strict';
2+
3+
// Fixture for test/parallel/test-assert-ok-source-maps.js (CommonJS variant).
4+
// See assert-no-source-map.mjs for the rationale (issue #63169).
5+
const assert = require('node:assert');
6+
7+
try {
8+
assert.ok(false);
9+
} catch (e) {
10+
process.stdout.write(e.constructor.name + '\n');
11+
process.stdout.write(e.code + '\n');
12+
process.stdout.write(e.message + '\n');
13+
process.exit(0);
14+
}
15+
process.exit(1);
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Fixture for test/parallel/test-assert-ok-source-maps.js.
2+
// Run with --enable-source-maps; the file has no source map, so the error's
3+
// source line resolution must fall back to the generated line instead of
4+
// throwing ERR_INVALID_ARG_TYPE (regression test for
5+
// https://github.com/nodejs/node/issues/63169).
6+
import { strict as assert } from 'node:assert';
7+
8+
try {
9+
assert(false);
10+
} catch (e) {
11+
process.stdout.write(e.constructor.name + '\n');
12+
process.stdout.write(e.code + '\n');
13+
process.stdout.write(e.message + '\n');
14+
process.exit(0);
15+
}
16+
process.exit(1);
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
'use strict';
2+
3+
// Regression test for https://github.com/nodejs/node/issues/63169
4+
// When --enable-source-maps is enabled but the script has no source map,
5+
// assert(false) should throw AssertionError, not TypeError ERR_INVALID_ARG_TYPE.
6+
7+
require('../common');
8+
const assert = require('node:assert');
9+
const { spawnSync } = require('node:child_process');
10+
const { test } = require('node:test');
11+
const fixtures = require('../common/fixtures');
12+
13+
function runWithSourceMaps(fixture) {
14+
return spawnSync(process.execPath, [
15+
'--enable-source-maps',
16+
fixtures.path('source-map', fixture),
17+
]);
18+
}
19+
20+
test('assert(false) in .mjs with --enable-source-maps throws AssertionError', () => {
21+
const result = runWithSourceMaps('assert-no-source-map.mjs');
22+
23+
assert.strictEqual(result.status, 0, `process exited with ${result.status}: ${result.stderr}`);
24+
const lines = result.stdout.toString().trim().split('\n');
25+
assert.strictEqual(lines[0], 'AssertionError');
26+
assert.strictEqual(lines[1], 'ERR_ASSERTION');
27+
assert.match(lines[2], /assert\(false\)/);
28+
});
29+
30+
test('assert.ok(false) in .cjs with --enable-source-maps throws AssertionError', () => {
31+
const result = runWithSourceMaps('assert-no-source-map.cjs');
32+
33+
assert.strictEqual(result.status, 0, `process exited with ${result.status}: ${result.stderr}`);
34+
const lines = result.stdout.toString().trim().split('\n');
35+
assert.strictEqual(lines[0], 'AssertionError');
36+
assert.strictEqual(lines[1], 'ERR_ASSERTION');
37+
assert.match(lines[2], /assert\.ok\(false\)/);
38+
});

0 commit comments

Comments
 (0)