Skip to content

Commit bef74dd

Browse files
committed
lib: fix ERR_INVALID_ARG_TYPE with --enable-source-maps
1 parent bbf51ad commit bef74dd

2 files changed

Lines changed: 76 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: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
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 fs = require('node:fs');
12+
const path = require('node:path');
13+
const tmpdir = require('../common/tmpdir');
14+
15+
tmpdir.refresh();
16+
17+
function runWithSourceMaps(filename, code) {
18+
const filePath = path.join(tmpdir.path, filename);
19+
fs.writeFileSync(filePath, code);
20+
const result = spawnSync(process.execPath, [
21+
'--enable-source-maps',
22+
filePath,
23+
]);
24+
return result;
25+
}
26+
27+
test('assert(false) in .mjs with --enable-source-maps throws AssertionError', () => {
28+
const result = runWithSourceMaps('repro.mjs', `
29+
import { strict as assert } from 'node:assert';
30+
try {
31+
assert(false);
32+
} catch (e) {
33+
process.stdout.write(e.constructor.name + '\\n');
34+
process.stdout.write(e.code + '\\n');
35+
process.stdout.write(e.message + '\\n');
36+
process.exit(0);
37+
}
38+
process.exit(1);
39+
`);
40+
41+
assert.strictEqual(result.status, 0, `process exited with ${result.status}: ${result.stderr}`);
42+
const lines = result.stdout.toString().trim().split('\n');
43+
assert.strictEqual(lines[0], 'AssertionError');
44+
assert.strictEqual(lines[1], 'ERR_ASSERTION');
45+
assert.match(lines[2], /assert\(false\)/);
46+
});
47+
48+
test('assert.ok(false) in .cjs with --enable-source-maps throws AssertionError', () => {
49+
const result = runWithSourceMaps('repro.cjs', `
50+
const assert = require('node:assert');
51+
try {
52+
assert.ok(false);
53+
} catch (e) {
54+
process.stdout.write(e.constructor.name + '\\n');
55+
process.stdout.write(e.code + '\\n');
56+
process.stdout.write(e.message + '\\n');
57+
process.exit(0);
58+
}
59+
process.exit(1);
60+
`);
61+
62+
assert.strictEqual(result.status, 0, `process exited with ${result.status}: ${result.stderr}`);
63+
const lines = result.stdout.toString().trim().split('\n');
64+
assert.strictEqual(lines[0], 'AssertionError');
65+
assert.strictEqual(lines[1], 'ERR_ASSERTION');
66+
assert.match(lines[2], /assert\.ok\(false\)/);
67+
});

0 commit comments

Comments
 (0)