Is there an existing issue for this?
How do you use Sentry?
Sentry Saas (sentry.io)
Electron SDK Version
5.6.0
Electron Version
32.1.2
What platform are you using?
MacOS
Link to Sentry event
No response
Steps to Reproduce
Preface: to my knowledge one should not use top level await to avoid electron deadlocks with the ready event
I'm aware that sentry needs load and initialize before electron, and I saw the recommendation to await generously so my first intention was:
main.ts that gets electron cli'ed (minimal reproduction)
async function initializeSentry() {
const Sentry = await import('@sentry/electron/main')
Sentry.init({ dsn: process.env.SENTRY_DSN })
}
async function bootApp() {
await initializeSentry()
const { app } = await import('electron')
await app.whenReady()
console.log('we are ready')
}
bootApp()
results in SentryError: Sentry SDK should be initialized before the Electron app 'ready' event is fired
So I tried to move up everything:
import * as Sentry from '@sentry/electron/main'
Sentry.init({ dsn: process.env.SENTRY_DSN })
async function bootApp() {
await initializeSentry()
const { app } = await import('electron')
await app.whenReady()
console.log('we are ready')
}
bootApp()
which now results in some cryptic hacky opentelementry require business
TypeError: Cannot read properties of undefined (reading 'require')
at new Hook (file:///<somepath>/main.bundle.dev.js:37874:40)
at __webpack_modules__.../node_modules/@opentelemetry/instrumentation/build/esm/platform/node/RequireInTheMiddleSingleton.js.RequireInTheMiddleSingleton._initialize (file:///<somepath>/main.bundle.dev.js:21426:9)
at new RequireInTheMiddleSingleton (file:///<somepath>/main.bundle.dev.js:21422:14)
at __webpack_modules__.../node_modules/@opentelemetry/instrumentation/build/esm/platform/node/RequireInTheMiddleSingleton.js.RequireInTheMiddleSingleton.getInstance (file:///<somepath>/main.bundle.dev.js:21479:68)
at new InstrumentationBase (file:///<somepath>/main.bundle.dev.js:21584:132)
at new UndiciInstrumentation (file:///<myname>/dev/fugoya/desktop-release/app/dist-dev/main.bundle.dev.js:20399:9)
at Object.setupOnce (file:///<somepath>/main.bundle.dev.js:57026:31)
at setupIntegration (file:///<somepath>/main.bundle.dev.js:43076:17)
at file:///<somepath>/main.bundle.dev.js:43047:7
at Array.forEach (<anonymous>)
Note that as you should for cutting startup time I'm bundling the main thread using webpack. I'm bundling for true ESM output without createRequire and dynamic imports:
const webpackConfig = {
// what we have to do is to essentially tell Webpack to actually use ESM format as well as standard ESM loading mechanism. We need to change our target from node20 to esX like ES2020, ES2022, etc. But changing the target to something other than node means we have to manually feed Webpack information about what built-in modules should be excluded as those can be assumed to be provided by runtime.
target: 'es2022',
externals: electronMainExternals,
entry: {
pdfWorker: [join(mainDir, 'domains', 'templates', 'pdfWorker.ts')],
main: join(mainDir, 'main.ts')
},
output: {
library: {
type: 'module' // used to be commonjs
},
chunkFormat: 'module',
module: true
},
module: {
rules: [
{
test: /\.[jt]sx?$/,
exclude: /node_modules/,
use: {
loader: 'swc-loader',
options: {
module: {
type: 'es6'
},
jsc: {
target: 'es2022',
parser: {
syntax: 'typescript',
dynamicImport: true
}
}
}
}
}
]
},
experiments: {
// enabled top level wait for our generated icon/language files
topLevelAwait: true,
// to enable esm output
outputModule: true
},
resolve: {
extensionAlias: {
// necessary for typescript / esm since we resolve .ts files from .js extensions in imports
'.js': ['.ts', '.tsx', '.js', '.jsx'],
'.jsx': ['.tsx', '.jsx'],
'.mjs': ['.mts', '.mjs'],
'.cjs': ['.cts', '.cjs']
},
// for resolving esm pkg json export fields
conditionNames: ['import', 'node']
},
// necessary for sentry https://github.com/getsentry/sentry-javascript/issues/12077
ignoreWarnings: [{ module: /@opentelemetry\/instrumentation/ }],
plugins: [
// to upload sourcemaps to sentry
sentryWebpackPlugin(<omitted for brevity></omitted>)
]
}
Expected Result
Instructions how to use sentry-electron in a bundled main thread ESM scenario.
Actual Result
SentryError: Sentry SDK should be initialized before the Electron app 'ready' event is fired
or
TypeError: Cannot read properties of undefined (reading 'require')
at new Hook (file:///<somepath>/main.bundle.dev.js:37874:40)
at __webpack_modules__.../node_modules/@opentelemetry/instrumentation/build/esm/platform/node/RequireInTheMiddleSingleton.js.RequireInTheMiddleSingleton._initialize (file:///<somepath>/main.bundle.dev.js:21426:9)
at new RequireInTheMiddleSingleton (file:///<somepath>/main.bundle.dev.js:21422:14)
at __webpack_modules__.../node_modules/@opentelemetry/instrumentation/build/esm/platform/node/RequireInTheMiddleSingleton.js.RequireInTheMiddleSingleton.getInstance (file:///<somepath>/main.bundle.dev.js:21479:68)
at new InstrumentationBase (file:///<somepath>/main.bundle.dev.js:21584:132)
at new UndiciInstrumentation (file:///<myname>/dev/fugoya/desktop-release/app/dist-dev/main.bundle.dev.js:20399:9)
at Object.setupOnce (file:///<somepath>/main.bundle.dev.js:57026:31)
at setupIntegration (file:///<somepath>/main.bundle.dev.js:43076:17)
at file:///<somepath>/main.bundle.dev.js:43047:7
at Array.forEach (<anonymous>)
I'm guessing the second approach is correct and I'm just experiencing bundling issues? What is @opentelemetry/instrumentation doing?
Is there an existing issue for this?
How do you use Sentry?
Sentry Saas (sentry.io)
Electron SDK Version
5.6.0
Electron Version
32.1.2
What platform are you using?
MacOS
Link to Sentry event
No response
Steps to Reproduce
Preface: to my knowledge one should not use top level await to avoid electron deadlocks with the ready event
I'm aware that sentry needs load and initialize before electron, and I saw the recommendation to await generously so my first intention was:
main.tsthat gets electron cli'ed (minimal reproduction)results in
SentryError: Sentry SDK should be initialized before the Electron app 'ready' event is firedSo I tried to move up everything:
which now results in some cryptic hacky opentelementry require business
Note that as you should for cutting startup time I'm bundling the main thread using webpack. I'm bundling for true ESM output without createRequire and dynamic imports:
Expected Result
Instructions how to use sentry-electron in a bundled main thread ESM scenario.
Actual Result
SentryError: Sentry SDK should be initialized before the Electron app 'ready' event is firedor
I'm guessing the second approach is correct and I'm just experiencing bundling issues? What is
@opentelemetry/instrumentationdoing?