Skip to content

Commit f1cf1c3

Browse files
MrJithilUlisesGascon
authored andcommitted
lib: fix assert shows diff messages in ESM and CJS
This PR addresses an issue which was caused by the design in the ESM loader. The ESM loader was modifying the file path and replacing the 'file' property with the file proto in the stack trace. This, in turn, led to unhandled exceptions when the assert module attempted to open the file to display erroneous code. The changes in this PR resolve this issue by handling the file path correctly, ensuring that the remaining message formatting code can execute as expected. PR-URL: #50634 Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent 9043ba4 commit f1cf1c3

File tree

2 files changed

+61
-1
lines changed

2 files changed

+61
-1
lines changed

lib/assert.js

+10-1
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ const CallTracker = require('internal/assert/calltracker');
7373
const {
7474
validateFunction,
7575
} = require('internal/validators');
76+
const { fileURLToPath } = require('internal/url');
7677

7778
let isDeepEqual;
7879
let isDeepStrictEqual;
@@ -296,7 +297,7 @@ function getErrMessage(message, fn) {
296297
overrideStackTrace.set(err, (_, stack) => stack);
297298
const call = err.stack[0];
298299

299-
const filename = call.getFileName();
300+
let filename = call.getFileName();
300301
const line = call.getLineNumber() - 1;
301302
let column = call.getColumnNumber() - 1;
302303
let identifier;
@@ -330,6 +331,14 @@ function getErrMessage(message, fn) {
330331
const { StringDecoder } = require('string_decoder');
331332
decoder = new StringDecoder('utf8');
332333
}
334+
335+
// ESM file prop is a file proto. Convert that to path.
336+
// This ensure opensync will not throw ENOENT for ESM files.
337+
const fileProtoPrefix = 'file://';
338+
if (StringPrototypeStartsWith(filename, fileProtoPrefix)) {
339+
filename = fileURLToPath(filename);
340+
}
341+
333342
fd = openSync(filename, 'r', 0o666);
334343
// Reset column and message.
335344
({ 0: column, 1: message } = getCode(fd, line, column));
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
'use strict';
2+
3+
const { spawnPromisified } = require('../common');
4+
const tmpdir = require('../common/tmpdir');
5+
const assert = require('assert');
6+
const { writeFileSync, unlink } = require('fs');
7+
const { describe, after, it } = require('node:test');
8+
9+
tmpdir.refresh();
10+
11+
const fileImports = {
12+
cjs: 'const assert = require("assert");',
13+
mjs: 'import assert from "assert";',
14+
};
15+
16+
const fileNames = [];
17+
18+
for (const [ext, header] of Object.entries(fileImports)) {
19+
const fileName = `test-file.${ext}`;
20+
// Store the generated filesnames in an array
21+
fileNames.push(`${tmpdir.path}/${fileName}`);
22+
23+
writeFileSync(tmpdir.resolve(fileName), `${header}\nassert.ok(0 === 2);`);
24+
}
25+
26+
describe('ensure the assert.ok throwing similar error messages for esm and cjs files', () => {
27+
const nodejsPath = `${process.execPath}`;
28+
const errorsMessages = [];
29+
30+
it('should return code 1 for each command', async () => {
31+
for (const fileName of fileNames) {
32+
const { stderr, code } = await spawnPromisified(nodejsPath, [fileName]);
33+
assert.strictEqual(code, 1);
34+
// For each error message, filter the lines which will starts with AssertionError
35+
errorsMessages.push(
36+
stderr.split('\n').find((s) => s.startsWith('AssertionError'))
37+
);
38+
}
39+
});
40+
41+
after(() => {
42+
assert.strictEqual(errorsMessages.length, 2);
43+
assert.deepStrictEqual(errorsMessages[0], errorsMessages[1]);
44+
45+
for (const fileName of fileNames) {
46+
unlink(fileName, () => {});
47+
}
48+
49+
tmpdir.refresh();
50+
});
51+
});

0 commit comments

Comments
 (0)