diff --git a/lib/assert.js b/lib/assert.js index b7d7a3da01d520..a88efd0b018a47 100644 --- a/lib/assert.js +++ b/lib/assert.js @@ -73,6 +73,7 @@ const CallTracker = require('internal/assert/calltracker'); const { validateFunction, } = require('internal/validators'); +const { URL, fileURLToPath } = require('internal/url'); let isDeepEqual; let isDeepStrictEqual; @@ -296,7 +297,7 @@ function getErrMessage(message, fn) { overrideStackTrace.set(err, (_, stack) => stack); const call = err.stack[0]; - const filename = call.getFileName(); + let filename = call.getFileName(); const line = call.getLineNumber() - 1; let column = call.getColumnNumber() - 1; let identifier; @@ -330,6 +331,15 @@ function getErrMessage(message, fn) { const { StringDecoder } = require('string_decoder'); decoder = new StringDecoder('utf8'); } + + // ESM file prop is a file proto. Convert that to path. + // This ensure opensync will not throw ENOENT for ESM files. + const fileProtoPrefix = 'file://'; + if (StringPrototypeStartsWith(filename, fileProtoPrefix)) { + const fileUrl = new URL(filename); + filename = fileURLToPath(fileUrl); + } + fd = openSync(filename, 'r', 0o666); // Reset column and message. ({ 0: column, 1: message } = getCode(fd, line, column));