Skip to content

Commit 45ef67e

Browse files
authored
fix(node): Remove leading slash in Windows filenames (#10147)
1 parent 307ec28 commit 45ef67e

File tree

2 files changed

+63
-1
lines changed

2 files changed

+63
-1
lines changed

packages/node/test/stacktrace.test.ts

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,63 @@ describe('Stack parsing', () => {
354354
]);
355355
});
356356

357+
test('parses with async frames Windows', () => {
358+
// https://github.com/getsentry/sentry-javascript/issues/4692#issuecomment-1063835795
359+
const err = new Error();
360+
err.stack =
361+
'Error: Client request error\n' +
362+
' at Object.httpRequestError (file:///C:/code/node_modules/@waroncancer/gaia/lib/error/error-factory.js:17:73)\n' +
363+
' at Object.run (file:///C:/code/node_modules/@waroncancer/gaia/lib/http-client/http-client.js:81:36)\n' +
364+
' at processTicksAndRejections (node:internal/process/task_queues:96:5)\n' +
365+
' at async Object.send (file:///C:/code/lib/post-created/send-post-created-notification-module.js:17:27)\n' +
366+
' at async each (file:///C:/code/lib/process-post-events-module.js:14:21)\n';
367+
368+
const frames = parseStackFrames(stackParser, err);
369+
370+
expect(frames).toEqual([
371+
{
372+
filename: 'C:/code/lib/process-post-events-module.js',
373+
module: 'process-post-events-module',
374+
function: 'each',
375+
lineno: 14,
376+
colno: 21,
377+
in_app: true,
378+
},
379+
{
380+
filename: 'C:/code/lib/post-created/send-post-created-notification-module.js',
381+
module: 'send-post-created-notification-module',
382+
function: 'Object.send',
383+
lineno: 17,
384+
colno: 27,
385+
in_app: true,
386+
},
387+
{
388+
filename: 'node:internal/process/task_queues',
389+
module: 'task_queues',
390+
function: 'processTicksAndRejections',
391+
lineno: 96,
392+
colno: 5,
393+
in_app: false,
394+
},
395+
{
396+
filename: 'C:/code/node_modules/@waroncancer/gaia/lib/http-client/http-client.js',
397+
module: '@waroncancer.gaia.lib.http-client:http-client',
398+
function: 'Object.run',
399+
lineno: 81,
400+
colno: 36,
401+
in_app: false,
402+
},
403+
{
404+
filename: 'C:/code/node_modules/@waroncancer/gaia/lib/error/error-factory.js',
405+
module: '@waroncancer.gaia.lib.error:error-factory',
406+
function: 'Object.httpRequestError',
407+
lineno: 17,
408+
colno: 73,
409+
in_app: false,
410+
},
411+
]);
412+
});
413+
357414
test('parses with colons in paths', () => {
358415
const err = new Error();
359416
err.stack =

packages/utils/src/node-stack-trace.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ export function filenameIsInApp(filename: string, isNative: boolean = false): bo
3535
// It's not internal if it's an absolute linux path
3636
!filename.startsWith('/') &&
3737
// It's not internal if it's an absolute windows path
38-
!filename.includes(':\\') &&
38+
!filename.match(/^[A-Z]:/) &&
3939
// It's not internal if the path is starting with a dot
4040
!filename.startsWith('.') &&
4141
// It's not internal if the frame has a protocol. In node, this is usually the case if the file got pre-processed with a bundler like webpack
@@ -103,6 +103,11 @@ export function node(getModule?: GetModuleFn): StackLineParserFn {
103103
let filename = lineMatch[2] && lineMatch[2].startsWith('file://') ? lineMatch[2].slice(7) : lineMatch[2];
104104
const isNative = lineMatch[5] === 'native';
105105

106+
// If it's a Windows path, trim the leading slash so that `/C:/foo` becomes `C:/foo`
107+
if (filename && filename.match(/\/[A-Z]:/)) {
108+
filename = filename.slice(1);
109+
}
110+
106111
if (!filename && lineMatch[5] && !isNative) {
107112
filename = lineMatch[5];
108113
}

0 commit comments

Comments
 (0)