Skip to content

feat(node): Rewrite absolute stackframes by default #16307

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 7 commits into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@ describe('ContextLines integration in ESM', () => {
stacktrace: {
frames: expect.arrayContaining([
{
filename: expect.stringMatching(/\/scenario with space.mjs$/),
filename: '/suites/contextLines/filename-with-spaces/scenario with space.mjs',
abs_path: expect.stringContaining(
'/dev-packages/node-integration-tests/suites/contextLines/filename-with-spaces/scenario with space.mjs',
),
context_line: "Sentry.captureException(new Error('Test Error'));",
pre_context: ["import * as Sentry from '@sentry/node';", ''],
post_context: ['', '// some more post context'],
Expand Down Expand Up @@ -54,7 +57,10 @@ describe('ContextLines integration in CJS', () => {
stacktrace: {
frames: expect.arrayContaining([
{
filename: expect.stringMatching(/\/scenario with space.cjs$/),
filename: '/suites/contextLines/filename-with-spaces/scenario with space.cjs',
abs_path: expect.stringContaining(
'/dev-packages/node-integration-tests/suites/contextLines/filename-with-spaces/scenario with space.cjs',
),
context_line: "Sentry.captureException(new Error('Test Error'));",
pre_context: [
'',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ test('should work inside catch block', async () => {
stacktrace: {
frames: expect.arrayContaining([
expect.objectContaining({
filename: '/suites/public-api/captureException/catched-error/scenario.ts',
abs_path: expect.stringContaining(
'/dev-packages/node-integration-tests/suites/public-api/captureException/catched-error/scenario.ts',
),
context_line: " throw new Error('catched_error');",
pre_context: [
'Sentry.init({',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,14 @@ test('should capture a simple error with message', async () => {
handled: true,
},
stacktrace: {
frames: expect.any(Array),
frames: expect.arrayContaining([
expect.objectContaining({
filename: '/suites/public-api/captureException/simple-error/scenario.ts',
abs_path: expect.stringContaining(
'/dev-packages/node-integration-tests/suites/public-api/captureException/simple-error/scenario.ts',
),
}),
]),
},
},
],
Expand Down
213 changes: 0 additions & 213 deletions dev-packages/node-integration-tests/test.txt

This file was deleted.

8 changes: 5 additions & 3 deletions packages/core/src/integrations/rewriteframes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ interface RewriteFramesOptions {
*/
export const rewriteFramesIntegration = defineIntegration((options: RewriteFramesOptions = {}) => {
const root = options.root;
const prefix = options.prefix || 'app:///';
const prefix = options.prefix ?? 'app:///';

const isBrowser = 'window' in GLOBAL_OBJ && !!GLOBAL_OBJ.window;

Expand Down Expand Up @@ -137,13 +137,15 @@ export function generateIteratee({
}
} else {
if (isWindowsFrame || startsWithSlash) {
const abs_path = frame.filename;
const filename = isWindowsFrame
? frame.filename
? abs_path
.replace(/^[a-zA-Z]:/, '') // remove Windows-style prefix
.replace(/\\/g, '/') // replace all `\\` instances with `/`
: frame.filename;
: abs_path;
const base = root ? relative(root, filename) : basename(filename);
frame.filename = `${prefix}${base}`;
frame.abs_path = abs_path;
}
}

Expand Down
10 changes: 10 additions & 0 deletions packages/core/test/lib/integrations/rewriteframes.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,16 @@ describe('RewriteFrames', () => {
expect(event.exception!.values![0]?.stacktrace!.frames![0]?.filename).toEqual('foobar/file1.js');
expect(event.exception!.values![0]?.stacktrace!.frames![1]?.filename).toEqual('foobar/file2.js');
});

it('handles empty string as prefix', () => {
rewriteFrames = rewriteFramesIntegration({
prefix: '',
});

const event = rewriteFrames.processEvent?.(exceptionEvent, {}, {} as any) as Event;
expect(event.exception!.values![0]?.stacktrace!.frames![0]?.filename).toEqual('file1.js');
expect(event.exception!.values![0]?.stacktrace!.frames![1]?.filename).toEqual('file2.js');
});
});

describe('default iteratee appends basename to `app:///` if frame starts with Windows path prefix', () => {
Expand Down
6 changes: 6 additions & 0 deletions packages/node/src/sdk/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
logger,
propagationContextFromHeaders,
requestDataIntegration,
rewriteFramesIntegration,
stackParserFromStackParserOptions,
} from '@sentry/core';
import {
Expand Down Expand Up @@ -69,6 +70,11 @@ export function getDefaultIntegrationsWithoutPerformance(): Integration[] {
nodeContextIntegration(),
childProcessIntegration(),
processSessionIntegration(),
// This has to run after the other integrations, because it rewrites the frames
rewriteFramesIntegration({
root: process.cwd(),
prefix: '/',
}),
...getCjsOnlyIntegrations(),
];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ describe('rewriteFramesIteratee', () => {

expect(result.exception?.values?.[0]?.stacktrace?.frames?.[0]).toEqual({
filename: 'app:///3-ab34d22f.js',
abs_path: '/some/path/to/server/chunks/3-ab34d22f.js',
lineno: 1,
colno: 1,
});
Expand Down