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

Node.js + sourcemaps = incorrect code positions in Sentry #1873

Closed
3 tasks
o2genum opened this issue Feb 6, 2019 · 6 comments
Closed
3 tasks

Node.js + sourcemaps = incorrect code positions in Sentry #1873

o2genum opened this issue Feb 6, 2019 · 6 comments

Comments

@o2genum
Copy link

o2genum commented Feb 6, 2019

Package + Version

  • @sentry/browser 4.5.3
  • @sentry/node 4.5.3
  • raven-js

Description

This is more of an issue with Node.js, I guess Sentry for Node should handle in some way.

Node.js wraps source files in an IIFE. A simple example:

And I know why: Node.js wraps javascript sources in an IIFE. This shifts the first line of the file by the length of the wrapper. For example:

example.js
throw new Error("Hello!")

Now run node example.js:

C:\Users\user\Desktop>node example.js
C:\Users\user\Desktop\example.js:1
(function (exports, require, module, __filename, __dirname) { throw new Error("Hello!")
                                                              ^
Error: Hello!
    at Object.<anonymous> (C:\Users\user\Desktop\example.js:1:69)
    at Module._compile (internal/modules/cjs/loader.js:689:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:700:10)
    at Module.load (internal/modules/cjs/loader.js:599:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:538:12)
    at Function.Module._load (internal/modules/cjs/loader.js:530:3)
    at Function.Module.runMain (internal/modules/cjs/loader.js:742:12)
    at startup (internal/bootstrap/node.js:279:19)
    at bootstrapNodeJSCore (internal/bootstrap/node.js:752:3)

This wrapper shifts the first line of every source file, making stacktrace column positions for the first line incorrect. Which in its turn breaks minified code + Sentry sourcemap processing, and code positions in Sentry are not even close.

I wonder what can be done about this?

@kamilogorek
Copy link
Contributor

Do you have a repro-case I can use to debug this? I tested source maps quite a lot of time in Sentry using Node and it worked just fine.

@o2genum
Copy link
Author

o2genum commented Feb 6, 2019

Ok, here is the repo that reproduces the issue (needs Sentry credentials for testing):
https://github.com/o2genum/node-stacktrace-issue-reproduce
And the error: https://sentry.io/share/issue/c3961143a9ee4c6fb796d773217f0169/

I think most users don't have this issue because of non-removable comments like this:

/*!
 * @overview es6-promise - a tiny implementation of Promises/A+.
 * @copyright Copyright (c) 2014 Yehuda Katz, Tom Dale, Stefan Penner and contributors (Conversion to ES6 API by Jake Archibald)
 * @license   Licensed under MIT license
 *            See https://raw.githubusercontent.com/stefanpenner/es6-promise/master/LICENSE
 * @version   v4.2.5+7f2b526d
 */

These comments get into the minified file, cutting it with newlines. I turned off bundling of external modules in the Webpack config, so I don't get these comments from various modules in my bundle.

@kamilogorek
Copy link
Contributor

kamilogorek commented Feb 7, 2019

image

Changes required for your repro to work:

  • remove nodeExernals from externals, as it's meant to be run in node env
  • add integrations: [new Sentry.Integrations.RewriteFrames()] to your Sentry.init call
  • change include: ['.'] to include: ['./build'] in your webpack config

https://docs.sentry.io/platforms/node/sourcemaps/#updating-sentry-sdk-configuration-to-support-source-maps

Also because you are using windows locally, you may want to look at this comment – #1857 (comment)

(dsn are public, so no need to hide it on my screen :p)

@kamilogorek
Copy link
Contributor

Closing due to inactivity

@o2genum
Copy link
Author

o2genum commented Feb 21, 2019

I got this working in my Electron app with @sentry/electron.
I had to do some configuration and some hacking, though.

So again, I have a main bundle + an external module, both minified.

  • I used include: ['.'], this made Sentry match paths properly. Plus ignoreFile: '.sentryignore' to control what to upload to Sentry.
  • As for my external module in node_modules/my_external_module (which is very hard to build into the main bundle), I include it in .sentryignore.
  • My my_external_module is a local package, so I had to use yarn, because npm symlinks local dependencies, not copies them, and @sentry/webpack-plugin does not follow symlinks on Windows. This way, my_external_module's sourcemaps and sources are uploaded to Sentry too.
  • I still had to force a newline in the beginning of the minified files. Both for the main bundle and my_external_module. With PreamblePlugin in Webpack, and preamble option in Rollup. Node.js indeed wraps sources that it loads with an IIFE. I still think this may cause problems in rare cases, when the minified file is not cut with /*! License */ comments of the bundled dependencies, and all errors thus occur on the first line.

@kamilogorek
Copy link
Contributor

Thanks for the detailed response @o2genum, appreciate it!

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

No branches or pull requests

2 participants