Skip to content

Commit 82348bd

Browse files
committed
lib: fix assert shows diff messages in ESM and CJS
This PR addresses issue #50593, 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 82348bd

File tree

1 file changed

+9
-1
lines changed

1 file changed

+9
-1
lines changed

lib/assert.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,7 @@ function getErrMessage(message, fn) {
296296
overrideStackTrace.set(err, (_, stack) => stack);
297297
const call = err.stack[0];
298298

299-
const filename = call.getFileName();
299+
let filename = call.getFileName();
300300
const line = call.getLineNumber() - 1;
301301
let column = call.getColumnNumber() - 1;
302302
let identifier;
@@ -330,6 +330,14 @@ function getErrMessage(message, fn) {
330330
const { StringDecoder } = require('string_decoder');
331331
decoder = new StringDecoder('utf8');
332332
}
333+
334+
// ESM file prop is a file proto. Convert that to path.
335+
// This ensure opensync will not throw ENOENT for ESM files.
336+
const fileProtoPrefix = 'file://';
337+
if (StringPrototypeStartsWith(filename, fileProtoPrefix)) {
338+
filename = StringPrototypeReplace(filename, fileProtoPrefix, '');
339+
}
340+
333341
fd = openSync(filename, 'r', 0o666);
334342
// Reset column and message.
335343
({ 0: column, 1: message } = getCode(fd, line, column));

0 commit comments

Comments
 (0)