Skip to content

feat(node): Skip context lines lookup when source maps are enabled #17405

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
wants to merge 1 commit into from

Conversation

mydea
Copy link
Member

@mydea mydea commented Aug 13, 2025

This improves lookup of files for the node context lines integration, when --enable-source-maps is used.

With this setting, error stacks will already be sourcemapped. This can lead to file lookups repeatedly failing, because we try to load files that do not exist in node modules. To avoid this, when we detect that this setting exists, we skip this in cases where we feel confident that this is unnecessary. For now, this is only one case:

  • It is a non-in-app frame
  • It is a .ts file - this can only happen if the file is source mapped, realistically

In this case, we'll skip trying to lookup the file.

We determine if the setting is defined by looking at process.env.NODE_OPTIONS and process.argv.
Additionally, in nest.js this is always defined, so we always handle it this way there.

Additionally, I also adjusted the log message when we fail to lookup the file, as this looked pretty ominous and unclear how problematic that was, even if this is not really an error per-se.

@mydea mydea requested review from Lms24 and andreiborza August 13, 2025 13:36
@mydea mydea self-assigned this Aug 13, 2025
Copy link
Contributor

size-limit report 📦

Path Size % Change Change
@sentry/browser 24.16 kB - -
@sentry/browser - with treeshaking flags 22.73 kB - -
@sentry/browser (incl. Tracing) 39.87 kB - -
@sentry/browser (incl. Tracing, Replay) 77.99 kB - -
@sentry/browser (incl. Tracing, Replay) - with treeshaking flags 67.83 kB - -
@sentry/browser (incl. Tracing, Replay with Canvas) 82.69 kB - -
@sentry/browser (incl. Tracing, Replay, Feedback) 94.79 kB - -
@sentry/browser (incl. Feedback) 40.83 kB - -
@sentry/browser (incl. sendFeedback) 28.81 kB - -
@sentry/browser (incl. FeedbackAsync) 33.7 kB - -
@sentry/react 25.88 kB - -
@sentry/react (incl. Tracing) 41.86 kB - -
@sentry/vue 28.64 kB - -
@sentry/vue (incl. Tracing) 41.69 kB - -
@sentry/svelte 24.18 kB - -
CDN Bundle 25.66 kB - -
CDN Bundle (incl. Tracing) 39.76 kB - -
CDN Bundle (incl. Tracing, Replay) 75.82 kB - -
CDN Bundle (incl. Tracing, Replay, Feedback) 81.27 kB - -
CDN Bundle - uncompressed 74.96 kB - -
CDN Bundle (incl. Tracing) - uncompressed 117.59 kB - -
CDN Bundle (incl. Tracing, Replay) - uncompressed 231.88 kB - -
CDN Bundle (incl. Tracing, Replay, Feedback) - uncompressed 244.48 kB - -
@sentry/nextjs (client) 43.89 kB - -
@sentry/sveltekit (client) 40.32 kB - -
@sentry/node-core 48.11 kB +0.26% +123 B 🔺
@sentry/node 147.15 kB +0.09% +130 B 🔺
@sentry/node - without tracing 92.36 kB +0.13% +118 B 🔺
@sentry/aws-serverless 103.8 kB +0.13% +128 B 🔺

View base workflow run

// Nest.js always enabled this, without an easy way for us to detect this
// so we just enable it by default
// see: https://github.com/nestjs/nest-cli/blob/f5dbb573df1fe103139026a36b6d0efe65e8e985/actions/start.action.ts#L220
hasSourceMaps: true,
Copy link
Member

Choose a reason for hiding this comment

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

m: I'm not sure if this is really always the case. I could imagine a deployment config where a nest app isn't started via the Nest CLI but directly via node.In which case, this probably doesn't work as we'd expect it 🤔 Might make sense to check official NestJS deployment guides to be safe.

Copy link
Member

Choose a reason for hiding this comment

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

Looks like the CLI isn't meant to start a Nest app in prod: https://docs.nestjs.com/deployment#node_envproduction

Copy link
Member Author

Choose a reason for hiding this comment

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

hmmm, good point! 😬
I haven't found a reliably way to figure out if this flag is set in Nest.js 😢 I can't seem to get a hold of these args/env vars in nest, probably due to how this is spawned (I suppose?)... I'll think about it some more. Generally speaking, this should mostly be not too dangerous either way, as the presence of .ts file more or less indicates that this is most likely source mapped already anyhow, but it is less safe 🤔

function shouldSkipContextLinesThatAreAlreadySourceMapped(path: string, frame: StackFrame): boolean {
// For non-in-app frames, we skip context lines when we are reasonably certain that the path is already sourcemapped.
// For now, we only consider .ts files because they can never appear otherwise in a stackframe, if not already sourcemapped.
if (frame.in_app === false && path.endsWith('.ts')) {
Copy link
Member

Choose a reason for hiding this comment

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

super-l: We could probably match against /.(c|m)?tsx?/ to cover some more TS file variations. Happy to leave this up to you though

Suggested change
if (frame.in_app === false && path.endsWith('.ts')) {
if (frame.in_app === false && path.endsWith('.ts')) {

Copy link
Member Author

Choose a reason for hiding this comment

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

yeah good point, likely a good idea 👍

@mydea mydea closed this Aug 14, 2025
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