-
-
Notifications
You must be signed in to change notification settings - Fork 58
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
Add to docs how to init sentry-electron when using a bundled electron ESM main thread #1007
Comments
I just stumbled across getsentry/sentry-javascript#10940 but am not knowledgeable enough to understand |
sorry for the "spam", but for the sake of documentation if others stumble upon this issue and might struggle like me I might as well comment: I have misunderstood the documentation around asynchronous modules and top level await:
So since we can't run cli flags, I'm leaning towards the esm without cli flag approach: /* mainSentry.ts */
import * as Sentry from '@sentry/electron/main'
Sentry.init({ dsn: process.env.SENTRY_DSN })
/* main.ts */
import './mainSentry.ts'
import someotherThings from 'somewhere'
await somePreReadyThings()
app
.whenReady()
.then(async () => {
await somePostReadyThings()
})
.catch((e) => {
// some error handling
}) So I guess my issue is rather bundling related. I'm assuming that normally people that bundle the main thread still bundle into commonjs or the createRequire style rather than clean esm bundles? |
We have a number of bundled examples that are tested but I suspect they all compile to CJS even though some have the code written in ESM. We also have 65+ individual feature tests that use ESM in the main process without bundling. I guess we need to add another example that bundles to ESM output! |
@timfish Thanks for the swift reply! I should have found the examples in the repo, sorry about that. You might wonder why output ESM after all when bundling and the reason is that I use a lib that
so there's not really another way for me I believe. Apart from that I assume over the next couple of years cjs will fade away anyway, so it's probably inevitable sooner or later. Let me know if I can help! |
By the way enforcing cjs compat import of sentry via import { createRequire } from 'node:module'
import type * as SentryType from '@sentry/electron/main'
const require = createRequire(import.meta.url)
const Sentry: typeof SentryType = require('@sentry/electron/main') so far does work as an intermediary solution, gotta check if that is true when bundling for production as I'm afraid |
Does
Node have recently added support for |
With the following webpack config, I can build working ESM code for the Electron main process: {
mode: 'production',
entry: './src/main.js',
target: 'electron-main',
output: {
library: { type: 'module' },
chunkFormat: 'module',
module: true,
filename: 'main.mjs',
},
experiments: {
topLevelAwait: true,
outputModule: true
},
plugins: [sentryWebpackPlugin(sentryWebpackPluginOptions)],
}, This does result in a couple of webpack build time warnings about |
I disregarded the most obvious option Just using Terribly sorry for not giving the most obvious option a go again and having you waste time on this. |
No worries at all. I've now got a test app to add to the examples! |
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)results in
SentryError: Sentry SDK should be initialized before the Electron app 'ready' event is fired
So 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 fired
or
I'm guessing the second approach is correct and I'm just experiencing bundling issues? What is
@opentelemetry/instrumentation
doing?The text was updated successfully, but these errors were encountered: