From 94aac24dbb555ba3e5e54326aefbc5c7ef96b6e0 Mon Sep 17 00:00:00 2001 From: Chirag Bitoni Date: Sat, 14 Nov 2026 23:52:11 +0530 Subject: [PATCH] fix: clean file:// URL prefixes when stripping cwd in clean() and parseLine() When error stacks originate in ESM modules, paths are formatted as file:// URLs. Previously, clean() only stripped \/ leaving file:// at the start of relative paths. Similarly, parseLine() failed to strip cwd from file:// URLs in setFile(). Handle file:// and file:/// URL prefixes matching cwd when normalizing paths. Fixes #60 Signed-off-by: Chirag Bitoni --- index.js | 9 +++++++-- test/test.js | 27 +++++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index f567133..01b19ef 100644 --- a/index.js +++ b/index.js @@ -34,6 +34,7 @@ class StackUtils { } this._cwd = opts.cwd.replace(/\\/g, '/'); + this._cwdRegexp = new RegExp(`(?:file:///?)${escapeStringRegexp(this._cwd)}/|${escapeStringRegexp(this._cwd)}/`, 'g'); this._internals = [].concat( opts.internals, ignoredPackagesRegExp(opts.ignoredPackages) @@ -79,7 +80,7 @@ class StackUtils { } } - st = st.replace(`${this._cwd}/`, ''); + st = st.replace(this._cwdRegexp, ''); if (st) { if (isAtLine) { @@ -295,7 +296,11 @@ class StackUtils { function setFile (result, filename, cwd) { if (filename) { filename = filename.replace(/\\/g, '/'); - if (filename.startsWith(`${cwd}/`)) { + if (filename.startsWith(`file:///${cwd}/`)) { + filename = filename.slice(cwd.length + 9); + } else if (filename.startsWith(`file://${cwd}/`)) { + filename = filename.slice(cwd.length + 8); + } else if (filename.startsWith(`${cwd}/`)) { filename = filename.slice(cwd.length + 1); } diff --git a/test/test.js b/test/test.js index 8721f6c..4bb70f0 100644 --- a/test/test.js +++ b/test/test.js @@ -71,6 +71,12 @@ t.test('clean: truncates cwd', t => { const expectIndented = utils.join(expectedArray.map(a => ' ' + a)); t.equal(stack.clean(WindowsStack1, 4), expectIndented, 'indentation'); + const linuxFileUrlStack = LinuxStack1.replace(/\/user\/dev\/project\//g, 'file:///user/dev/project/'); + t.equal(new StackUtils({cwd: '/user/dev/project', internals: []}).clean(linuxFileUrlStack), expected, 'strips file:// on linux'); + + const winFileUrlStack = WindowsStack1.replace(/Z:\\user\\dev\\project\\/g, 'file:///Z:/user/dev/project/'); + t.equal(stack.clean(winFileUrlStack), expected, 'strips file:// on windows'); + t.end(); }); @@ -386,6 +392,27 @@ t.test('parseLine: handles parens', t => { t.end(); }); +t.test('parseLine: handles file:// URLs', t => { + const line = ' at run (file:///user/dev/project/test.mjs:6:9)'; + const stackUtils = new StackUtils({cwd: '/user/dev/project'}); + t.same(stackUtils.parseLine(line), { + line: 6, + column: 9, + file: 'test.mjs', + function: 'run' + }); + + const winLine = ' at run (file:///Z:/user/dev/project/test.mjs:6:9)'; + const winStackUtils = new StackUtils({cwd: 'Z:\\user\\dev\\project'}); + t.same(winStackUtils.parseLine(winLine), { + line: 6, + column: 9, + file: 'test.mjs', + function: 'run' + }); + t.end(); +}); + function linuxStack1() { return [ 'Error: foo',