Skip to content

Commit 3414ac7

Browse files
committed
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.
1 parent 33704c4 commit 3414ac7

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

lib/assert.js

Lines changed: 11 additions & 1 deletion
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 { URL, 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,15 @@ 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+
const fileUrl = new URL(filename);
340+
filename = fileURLToPath(fileUrl);
341+
}
342+
333343
fd = openSync(filename, 'r', 0o666);
334344
// Reset column and message.
335345
({ 0: column, 1: message } = getCode(fd, line, column));

0 commit comments

Comments
 (0)