Skip to content

Commit 4443cb7

Browse files
authored
fix: Correctly resolve debug IDs for ANR events with custom appRoot (#14822)
- Ref getsentry/sentry-electron#1011 In #14709 I didn't add a test. If I had, I would have noticed that it was not working. File paths need normalising before we try and match up debug files.
1 parent 9b1ab0e commit 4443cb7

File tree

3 files changed

+70
-9
lines changed

3 files changed

+70
-9
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import * as assert from 'assert';
2+
import * as crypto from 'crypto';
3+
import * as path from 'path';
4+
import * as url from 'url';
5+
6+
import * as Sentry from '@sentry/node';
7+
8+
global._sentryDebugIds = { [new Error().stack]: 'aaaaaaaa-aaaa-4aaa-aaaa-aaaaaaaaaa' };
9+
10+
const __dirname = path.dirname(url.fileURLToPath(import.meta.url));
11+
12+
setTimeout(() => {
13+
process.exit();
14+
}, 10000);
15+
16+
Sentry.init({
17+
dsn: process.env.SENTRY_DSN,
18+
release: '1.0',
19+
autoSessionTracking: false,
20+
integrations: [Sentry.anrIntegration({ captureStackTrace: true, anrThreshold: 100, appRootPath: __dirname })],
21+
});
22+
23+
Sentry.setUser({ email: '[email protected]' });
24+
Sentry.addBreadcrumb({ message: 'important message!' });
25+
26+
function longWork() {
27+
for (let i = 0; i < 20; i++) {
28+
const salt = crypto.randomBytes(128).toString('base64');
29+
const hash = crypto.pbkdf2Sync('myPassword', salt, 10000, 512, 'sha512');
30+
assert.ok(hash);
31+
}
32+
}
33+
34+
setTimeout(() => {
35+
longWork();
36+
}, 1000);

dev-packages/node-integration-tests/suites/anr/test.ts

+24-4
Original file line numberDiff line numberDiff line change
@@ -30,20 +30,20 @@ const ANR_EVENT = {
3030
mechanism: { type: 'ANR' },
3131
stacktrace: {
3232
frames: expect.arrayContaining([
33-
{
33+
expect.objectContaining({
3434
colno: expect.any(Number),
3535
lineno: expect.any(Number),
3636
filename: expect.any(String),
3737
function: '?',
3838
in_app: true,
39-
},
40-
{
39+
}),
40+
expect.objectContaining({
4141
colno: expect.any(Number),
4242
lineno: expect.any(Number),
4343
filename: expect.any(String),
4444
function: 'longWork',
4545
in_app: true,
46-
},
46+
}),
4747
]),
4848
},
4949
},
@@ -122,6 +122,26 @@ describe('should report ANR when event loop blocked', () => {
122122
.start(done);
123123
});
124124

125+
test('Custom appRootPath', done => {
126+
const ANR_EVENT_WITH_SPECIFIC_DEBUG_META: Event = {
127+
...ANR_EVENT_WITH_SCOPE,
128+
debug_meta: {
129+
images: [
130+
{
131+
type: 'sourcemap',
132+
debug_id: 'aaaaaaaa-aaaa-4aaa-aaaa-aaaaaaaaaa',
133+
code_file: 'app:///app-path.mjs',
134+
},
135+
],
136+
},
137+
};
138+
139+
createRunner(__dirname, 'app-path.mjs')
140+
.withMockSentryServer()
141+
.expect({ event: ANR_EVENT_WITH_SPECIFIC_DEBUG_META })
142+
.start(done);
143+
});
144+
125145
test('multiple events via maxAnrEvents', done => {
126146
createRunner(__dirname, 'basic-multiple.mjs')
127147
.withMockSentryServer()

packages/node/src/integrations/anr/worker.ts

+10-5
Original file line numberDiff line numberDiff line change
@@ -91,22 +91,27 @@ function applyDebugMeta(event: Event): void {
9191
return;
9292
}
9393

94+
const normalisedDebugImages = options.appRootPath ? {} : mainDebugImages;
95+
if (options.appRootPath) {
96+
for (const [path, debugId] of Object.entries(mainDebugImages)) {
97+
normalisedDebugImages[normalizeUrlToBase(path, options.appRootPath)] = debugId;
98+
}
99+
}
100+
94101
const filenameToDebugId = new Map<string, string>();
95102

96103
for (const exception of event.exception?.values || []) {
97104
for (const frame of exception.stacktrace?.frames || []) {
98105
const filename = frame.abs_path || frame.filename;
99-
if (filename && mainDebugImages[filename]) {
100-
filenameToDebugId.set(filename, mainDebugImages[filename] as string);
106+
if (filename && normalisedDebugImages[filename]) {
107+
filenameToDebugId.set(filename, normalisedDebugImages[filename] as string);
101108
}
102109
}
103110
}
104111

105112
if (filenameToDebugId.size > 0) {
106113
const images: DebugImage[] = [];
107-
for (const [filename, debug_id] of filenameToDebugId.entries()) {
108-
const code_file = options.appRootPath ? normalizeUrlToBase(filename, options.appRootPath) : filename;
109-
114+
for (const [code_file, debug_id] of filenameToDebugId.entries()) {
110115
images.push({
111116
type: 'sourcemap',
112117
code_file,

0 commit comments

Comments
 (0)