diff --git a/lib/assert.js b/lib/assert.js index 8ca9b211394ba8..89eebfa793fc39 100644 --- a/lib/assert.js +++ b/lib/assert.js @@ -65,6 +65,7 @@ const { isPromise, isRegExp } = require('internal/util/types'); const { EOL } = require('internal/constants'); const { NativeModule } = require('internal/bootstrap/loaders'); const { isError } = require('internal/util'); +const { fileURLToPath } = require('internal/url'); const errorCache = new SafeMap(); const CallTracker = require('internal/assert/calltracker'); @@ -291,12 +292,18 @@ 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; let code; + if (filename && StringPrototypeStartsWith(filename, 'file:')) { + try { + filename = fileURLToPath(filename); + } catch {} + } + if (filename) { identifier = `${filename}${line}${column}`; diff --git a/lib/internal/modules/cjs/loader.js b/lib/internal/modules/cjs/loader.js index 0c69d547d40b9a..7921cd237ed875 100644 --- a/lib/internal/modules/cjs/loader.js +++ b/lib/internal/modules/cjs/loader.js @@ -76,7 +76,12 @@ const { maybeCacheSourceMap, rekeySourceMap } = require('internal/source_map/source_map_cache'); -const { pathToFileURL, fileURLToPath, isURLInstance } = require('internal/url'); +const { + pathToFileURL, + fileURLToPath, + isURLInstance, + URL +} = require('internal/url'); const { deprecate } = require('internal/util'); const vm = require('vm'); const assert = require('internal/assert'); @@ -1005,12 +1010,18 @@ Module.prototype.require = function(id) { // (needed for setting breakpoint when called with --inspect-brk) let resolvedArgv; let hasPausedEntry = false; +const absolutePathToUrlConverter = new URL('file://'); function wrapSafe(filename, content, cjsModuleInstance) { + let filenameUrl = filename; + if (path.isAbsolute(filename)) { + absolutePathToUrlConverter.pathname = filename; + filenameUrl = absolutePathToUrlConverter.href; + } if (patched) { const wrapper = Module.wrap(content); return vm.runInThisContext(wrapper, { - filename, + filename: filenameUrl, lineOffset: 0, displayErrors: true, importModuleDynamically: async (specifier) => { @@ -1023,7 +1034,7 @@ function wrapSafe(filename, content, cjsModuleInstance) { try { compiled = compileFunction( content, - filename, + filenameUrl, 0, 0, undefined, diff --git a/lib/internal/source_map/prepare_stack_trace.js b/lib/internal/source_map/prepare_stack_trace.js index c32f9780c2fb3f..3b16e536682688 100644 --- a/lib/internal/source_map/prepare_stack_trace.js +++ b/lib/internal/source_map/prepare_stack_trace.js @@ -85,10 +85,7 @@ const prepareStackTrace = (globalThis, error, trace) => { const prefix = (name && name !== t.getFunctionName()) ? `\n -> at ${name}` : '\n ->'; - const originalSourceNoScheme = - StringPrototypeStartsWith(originalSource, 'file://') ? - fileURLToPath(originalSource) : originalSource; - str += `${prefix} (${originalSourceNoScheme}:${originalLine + 1}:` + + str += `${prefix} (${originalSource}:${originalLine + 1}:` + `${originalColumn + 1})`; } } diff --git a/test/parallel/test-common-must-not-call.js b/test/parallel/test-common-must-not-call.js index dcea7059dac7f5..936e4601efb824 100644 --- a/test/parallel/test-common-must-not-call.js +++ b/test/parallel/test-common-must-not-call.js @@ -4,6 +4,7 @@ const common = require('../common'); const assert = require('assert'); const path = require('path'); const util = require('util'); +const { fileURLToPath } = require('url'); const message = 'message'; const testFunction1 = common.mustNotCall(message); @@ -13,10 +14,15 @@ const testFunction2 = common.mustNotCall(message); const createValidate = (line, args = []) => common.mustCall((e) => { const prefix = `${message} at `; assert.ok(e.message.startsWith(prefix)); + e.message = e.message.substring(prefix.length); + if (e.message.startsWith('file:')) { + const url = /.*/.exec(e.message)[0]; + e.message = fileURLToPath(url) + e.message.substring(url.length); + } if (process.platform === 'win32') { e.message = e.message.substring(2); // remove 'C:' } - const msg = e.message.substring(prefix.length); + const msg = e.message; const firstColon = msg.indexOf(':'); const fileName = msg.substring(0, firstColon); const rest = msg.substring(firstColon + 1); @@ -26,14 +32,14 @@ const createValidate = (line, args = []) => common.mustCall((e) => { assert.strictEqual(rest, line + argsInfo); }); -const validate1 = createValidate('9'); +const validate1 = createValidate('10'); try { testFunction1(); } catch (e) { validate1(e); } -const validate2 = createValidate('11', ['hello', 42]); +const validate2 = createValidate('12', ['hello', 42]); try { testFunction2('hello', 42); } catch (e) { diff --git a/test/parallel/test-domain-set-uncaught-exception-capture-after-load.js b/test/parallel/test-domain-set-uncaught-exception-capture-after-load.js index 55e2e5368d7d17..0d95ff5fdf0166 100644 --- a/test/parallel/test-domain-set-uncaught-exception-capture-after-load.js +++ b/test/parallel/test-domain-set-uncaught-exception-capture-after-load.js @@ -1,6 +1,7 @@ 'use strict'; const common = require('../common'); const assert = require('assert'); +const { pathToFileURL } = require('url'); (function foobar() { require('domain'); @@ -20,7 +21,7 @@ assert.throws( assert(err.stack.includes('-'.repeat(40)), `expected ${err.stack} to contain dashes`); - const location = `at foobar (${__filename}:`; + const location = `at foobar (${pathToFileURL(__filename)}:`; assert(err.stack.includes(location), `expected ${err.stack} to contain ${location}`); return true; diff --git a/test/parallel/test-http-timeout-client-warning.js b/test/parallel/test-http-timeout-client-warning.js index f11515b95fe340..0e062f0db6633a 100644 --- a/test/parallel/test-http-timeout-client-warning.js +++ b/test/parallel/test-http-timeout-client-warning.js @@ -2,12 +2,13 @@ const common = require('../common'); const http = require('http'); const assert = require('assert'); +const { pathToFileURL } = require('url'); // Checks that the setTimeout duration overflow warning is emitted // synchronously and therefore contains a meaningful stacktrace. process.on('warning', common.mustCall((warning) => { - assert(warning.stack.includes(__filename)); + assert(warning.stack.includes(pathToFileURL(__filename))); })); const server = http.createServer((req, resp) => resp.end()); diff --git a/test/sequential/test-cli-syntax-bad.js b/test/sequential/test-cli-syntax-bad.js index 8298157a57e16e..54bb9b81108b8c 100644 --- a/test/sequential/test-cli-syntax-bad.js +++ b/test/sequential/test-cli-syntax-bad.js @@ -3,6 +3,7 @@ const common = require('../common'); const assert = require('assert'); const { exec } = require('child_process'); +const { pathToFileURL } = require('url'); const fixtures = require('../common/fixtures'); const node = process.execPath; @@ -42,7 +43,8 @@ const syntaxErrorRE = /^SyntaxError: \b/m; assert(syntaxErrorRE.test(stderr), `${syntaxErrorRE} === ${stderr}`); // stderr should include the filename - assert(stderr.startsWith(file), `${stderr} starts with ${file}`); + assert(stderr.startsWith(pathToFileURL(file)), + `${stderr} starts with ${pathToFileURL(file)}`); })); }); }); diff --git a/test/sequential/test-cli-syntax-require.js b/test/sequential/test-cli-syntax-require.js index c309b1f4556c3f..92a6900db6b086 100644 --- a/test/sequential/test-cli-syntax-require.js +++ b/test/sequential/test-cli-syntax-require.js @@ -4,6 +4,7 @@ const common = require('../common'); const assert = require('assert'); const { exec } = require('child_process'); const fixtures = require('../common/fixtures'); +const { pathToFileURL } = require('url'); const node = process.execPath; @@ -29,8 +30,9 @@ const syntaxErrorRE = /^SyntaxError: \b/m; // stderr should have a syntax error message assert(syntaxErrorRE.test(stderr), `${syntaxErrorRE} === ${stderr}`); - // stderr should include the filename - assert(stderr.startsWith(file), `${stderr} starts with ${file}`); + // stderr should include the file URL + const fileUrl = pathToFileURL(file); + assert(stderr.startsWith(fileUrl), `${stderr} starts with ${file}`); })); }); });