Skip to content

Feat: Allow node stackwalk in Electron renderer #3710

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

Closed

Conversation

timfish
Copy link
Collaborator

@timfish timfish commented Jun 20, 2021

Related to: getsentry/sentry-electron#308

This PR:

  • Moves @sentry/node functions eventFromException and eventFromMessage out of backend and into their own file much like in the browser SDK.
  • Re-exports them for use in @sentry/electron
  • Adds readFile as a parameter in the parser functions

Why is this funky readFile stuff required?

The Electron renderer may or may not have access to node functionality. If nodeIntegration is enabled, full node is available. If nodeIntegration is disabled and a bundler is being used, it will complain that fs cannot be found/is unavailable. With this PR we would just not pass readFile when the stack parser is called from the Electron renderer and no source files will be read.

What are the alternatives?

  • Have different entry points in @sentry/electron depending on nodeIntegration (ie. init and initWithNode)
  • Inform those using bundlers that they need to add fs to externals
  • Add a NodeIntegration sentry integration that "patches" in this functionality?
  • Any other ideas?

@timfish timfish requested a review from kamilogorek as a code owner June 20, 2021 21:43
@timfish
Copy link
Collaborator Author

timfish commented Jun 20, 2021

I've also got a branch that uses dynamicRequire(module, 'fs') but that has the significant downside that source file loading will no longer function if a bundler is being used.

Copy link
Contributor

@kamilogorek kamilogorek left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it maybe be possible to just bail out if fs is not available?

function tryToReadFile(filename, resolve): void {
  try {
    const { readFile } = require('fs');
    // move previous logic here
  } catch (_e) {
    return null;
  }
}

Although totally valid, the implementation from this PR adds a lot of indirection which is kinda hard to follow.

@timfish

This comment has been minimized.

@timfish
Copy link
Collaborator Author

timfish commented Jun 21, 2021

Would it maybe be possible to just bail out if fs is not available?

Any usage of require('fs') would be picked up by bundlers and throw errors which would force everyone to add fs to externals.

@timfish
Copy link
Collaborator Author

timfish commented Jun 21, 2021

I've refactored this to be a bit better in that rather than passing around a ref to readFile, the source reading is now in it's own file and I pass around a ref to readFilesAddPrePostContext.

It would still probably be better to refactor the source reading out of parseStack entirely and into its own step that could be applied to the event after.

So in @sentry/node it would be used like this:

  public eventFromException(exception: any, hint?: EventHint): PromiseLike<Event> {
    const event = eventFromException(this._options, exception, hint);
    return addSourcesToEvent(event);
  }

And in @sentry/electron you would simply skip adding the sources:

  public eventFromException(exception: any, hint?: EventHint): PromiseLike<Event> {
    return eventFromException(this._options, exception, hint);
  }

Do you think the unit tests are up to supporting me through that kind of refactor? 🙂

@timfish
Copy link
Collaborator Author

timfish commented Jun 23, 2021

Closing in favour of #3729

@timfish timfish closed this Jun 23, 2021
@timfish timfish deleted the feat/node-stackwalk-electron branch March 15, 2022 13:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants