Skip to content
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

fix: Normalize filename before parsing module name #699

Merged
merged 2 commits into from
Jul 13, 2023
Merged
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
14 changes: 7 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,17 +58,17 @@
"e2e": "cross-env TS_NODE_PROJECT=tsconfig.json xvfb-maybe mocha --require ts-node/register/transpile-only --retries 3 ./test/e2e/*.ts"
},
"dependencies": {
"@sentry/browser": "7.57.0",
"@sentry/core": "7.57.0",
"@sentry/node": "7.57.0",
"@sentry/types": "7.57.0",
"@sentry/utils": "7.57.0",
"@sentry/browser": "7.58.0",
"@sentry/core": "7.58.0",
"@sentry/node": "7.58.0",
"@sentry/types": "7.58.0",
"@sentry/utils": "7.58.0",
"deepmerge": "4.3.0",
"tslib": "^2.5.0"
},
"devDependencies": {
"@sentry-internal/eslint-config-sdk": "7.57.0",
"@sentry-internal/typescript": "7.57.0",
"@sentry-internal/eslint-config-sdk": "7.58.0",
"@sentry-internal/typescript": "7.58.0",
"@types/busboy": "^0.2.3",
"@types/chai": "^4.2.10",
"@types/chai-as-promised": "^7.1.5",
Expand Down
5 changes: 5 additions & 0 deletions src/main/sdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
SentryMinidump,
} from './integrations';
import { configureIPC } from './ipc';
import { defaultStackParser } from './stack-parse';
import { ElectronOfflineTransportOptions, makeElectronOfflineTransport } from './transports/electron-offline-net';
import { SDK_VERSION } from './version';

Expand Down Expand Up @@ -113,6 +114,10 @@ export function init(userOptions: ElectronMainOptions): void {
options.autoSessionTracking = false;
}

if (options.stackParser === undefined) {
options.stackParser = defaultStackParser;
}

setDefaultIntegrations(defaults, options);

if (options.dsn && options.transport === undefined) {
Expand Down
20 changes: 20 additions & 0 deletions src/main/stack-parse.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { getModuleFromFilename as getModuleFromFilenameNode } from '@sentry/node';
import { StackParser } from '@sentry/types';
import { createStackParser, nodeStackLineParser } from '@sentry/utils';
import { app } from 'electron';

import { normalizeUrl } from '../common';

/** Parses the module name form a filename */
function getModuleFromFilename(filename: string | undefined): string | undefined {
if (!filename) {
return;
}

const normalizedFilename = normalizeUrl(filename, app.getAppPath());

return getModuleFromFilenameNode(normalizedFilename);
}

// node.js stack parser but filename normalized before parsing the module
export const defaultStackParser: StackParser = createStackParser(nodeStackLineParser(getModuleFromFilename));
2 changes: 1 addition & 1 deletion src/renderer/sdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export const defaultIntegrations = [...defaultBrowserIntegrations, new ScopeToMa
export function init<O extends BrowserOptions>(
options: BrowserOptions & O = {} as BrowserOptions & O,
// This parameter name ensures that TypeScript error messages contain a hint for fixing SDK version mismatches
originalInit: (if_you_get_a_typescript_error_ensure_sdks_use_version_v7_57_0: O) => void = browserInit,
originalInit: (if_you_get_a_typescript_error_ensure_sdks_use_version_v7_58_0: O) => void = browserInit,
): void {
ensureProcess('renderer');

Expand Down
1 change: 0 additions & 1 deletion test/e2e/test-apps/other/browser-tracing/recipe.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,3 @@ description: Browser Tracing
command: yarn
# Browser Tracing fails on Electron v24
condition: version.major >= 3 && version.major != 24
```
143 changes: 143 additions & 0 deletions test/e2e/test-apps/other/main-process-module/event.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
{
"method": "envelope",
"sentryKey": "37f8a2ee37c0409d8970bc7559c7c7e4",
"appId": "277345",
"data": {
"sdk": {
"name": "sentry.javascript.electron",
"packages": [
{
"name": "npm:@sentry/electron",
"version": "{{version}}"
}
],
"version": "{{version}}"
},
"contexts": {
"app": {
"app_name": "webpack-main-process",
"app_version": "1.0.0",
"app_start_time": "{{time}}"
},
"browser": {
"name": "Chrome"
},
"chrome": {
"name": "Chrome",
"type": "runtime",
"version": "{{version}}"
},
"device": {
"arch": "{{arch}}",
"family": "Desktop",
"memory_size": 0,
"free_memory": 0,
"processor_count": 0,
"processor_frequency": 0,
"cpu_description": "{{cpu}}",
"screen_resolution":"{{screen}}",
"screen_density": 1,
"language": "{{language}}"
},
"node": {
"name": "Node",
"type": "runtime",
"version": "{{version}}"
},
"os": {
"name": "{{platform}}",
"version": "{{version}}"
},
"runtime": {
"name": "Electron",
"version": "{{version}}"
}
},
"release": "webpack-main-process@1.0.0",
"environment": "development",
"user": {
"ip_address": "{{auto}}",
"id": "abc-123"
},
"debug_meta": {
"images": [
{
"code_file": "app:///dist/main.js",
"type": "sourcemap"
}
]
},
"exception": {
"values": [
{
"type": "Error",
"value": "Some main error",
"stacktrace": {
"frames": [
{
"colno": 0,
"filename": "app:///dist/main.js",
"function": "{{function}}",
"in_app": true,
"lineno": 0,
"module": "main"
}
]
},
"mechanism": {
"handled": true,
"type": "generic"
}
}
]
},
"level": "fatal",
"event_id": "{{id}}",
"platform": "node",
"timestamp": 0,
"breadcrumbs": [
{
"timestamp": 0,
"category": "electron",
"message": "app.will-finish-launching",
"type": "ui"
},
{
"timestamp": 0,
"category": "electron",
"message": "app.ready",
"type": "ui"
},
{
"timestamp": 0,
"category": "electron",
"message": "app.session-created",
"type": "ui"
},
{
"timestamp": 0,
"category": "electron",
"message": "app.web-contents-created",
"type": "ui"
},
{
"timestamp": 0,
"category": "electron",
"message": "app.browser-window-created",
"type": "ui"
},
{
"timestamp": 0,
"category": "electron",
"message": "renderer.dom-ready",
"type": "ui"
}
],
"tags": {
"event.environment": "javascript",
"event.origin": "electron",
"event.process": "browser",
"event_type": "javascript"
}
}
}
20 changes: 20 additions & 0 deletions test/e2e/test-apps/other/main-process-module/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"name": "webpack-main-process",
"version": "1.0.0",
"scripts": {
"start": "electron .",
"build": "webpack"
},
"main": "dist/main.js",
"devDependencies": {
"@sentry/webpack-plugin": "^2.2.0",
"csp-html-webpack-plugin": "^5.1.0",
"html-webpack-plugin": "^5.3.2",
"warnings-to-errors-webpack-plugin": "^2.0.1",
"webpack": "^5.48.0",
"webpack-cli": "^4.7.2"
},
"dependencies": {
"@sentry/electron": "3.0.0"
}
}
4 changes: 4 additions & 0 deletions test/e2e/test-apps/other/main-process-module/recipe.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
description: Webpack app with error in main process
command: yarn && yarn build
condition: supportsContextIsolation && supportsSandbox
timeout: 120
36 changes: 36 additions & 0 deletions test/e2e/test-apps/other/main-process-module/src/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import * as path from 'path';
import * as url from 'url';

import { app, BrowserWindow } from 'electron';
// eslint-disable-next-line import/no-unresolved
import { init } from '@sentry/electron';

init({
dsn: '__DSN__',
debug: true,
autoSessionTracking: false,
onFatalError: () => {},
});

app.on('ready', () => {
const window = new BrowserWindow({
show: false,
webPreferences: {
nodeIntegration: false,
contextIsolation: true,
sandbox: true,
},
});

window.loadURL(
url.format({
pathname: path.join(__dirname, 'index.html'),
protocol: 'file:',
slashes: true,
}),
);

setTimeout(() => {
throw new Error('Some main error');
}, 500);
});
10 changes: 10 additions & 0 deletions test/e2e/test-apps/other/main-process-module/src/renderer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// eslint-disable-next-line import/no-unresolved
import { init, configureScope } from '@sentry/electron';

init({
debug: true,
});

configureScope((scope) => {
scope.setUser({ id: 'abc-123' });
});
51 changes: 51 additions & 0 deletions test/e2e/test-apps/other/main-process-module/webpack.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
const HtmlWebpackPlugin = require('html-webpack-plugin');
const CspHtmlWebpackPlugin = require('csp-html-webpack-plugin');
const WarningsToErrorsPlugin = require('warnings-to-errors-webpack-plugin');
const { sentryWebpackPlugin } = require('@sentry/webpack-plugin');

const sentryWebpackPluginOptions = {
authToken: 'some invalid auth token',
org: 'some invalid org',
project: 'some invalid project',
telemetry: false,
sourcemaps: {
assets: [], // no assets to upload - we just care about injecting debug IDs
},
release: {
inject: false,
},
errorHandler() {
// do nothing on errors :)
// They will happen because of the invalid auth token
},
};

module.exports = [
{
mode: 'production',
entry: './src/main.js',
target: 'electron-main',
output: {
libraryTarget: 'commonjs2',
filename: 'main.js',
},
plugins: [new WarningsToErrorsPlugin(), sentryWebpackPlugin(sentryWebpackPluginOptions)],
},
{
mode: 'production',
entry: './src/renderer.js',
target: 'web',
output: {
filename: 'renderer.js',
},
plugins: [
new HtmlWebpackPlugin(),
new WarningsToErrorsPlugin(),
new CspHtmlWebpackPlugin({
'default-src': "'self'",
'script-src': "'self'",
}),
sentryWebpackPlugin(sentryWebpackPluginOptions),
],
},
];
Loading