Skip to content

Conversation

@s1gr1d
Copy link
Member

@s1gr1d s1gr1d commented Dec 22, 2025

The plugin just injects the debug ID, but doesn't check if it is already in the file. Injecting multiple debug IDs result in problems (like the linked issue below):
getsentry/sentry-javascript#18519

This PR checks the beginning and end of the file for debug ID content. The numbers for that are a best estimate, so we don't need to read the whole file (potentially resource-intense).

Comment on lines 272 to 281
// Check if a debug ID has already been injected to avoid duplicate injection (e.g. by another plugin or Sentry CLI)
const chunkStartSnippet = code.slice(0, 2000);
const chunkEndSnippet = code.slice(-500);

if (
chunkStartSnippet.includes("_sentryDebugIdIdentifier") ||
chunkEndSnippet.includes("//# debugId=")
) {
return null; // Debug ID already present, skip injection
}
Copy link
Member

Choose a reason for hiding this comment

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

l: Looking at the tests, I see that we slice the code here for performance reasons (TIL that String.slice is O(1) while Array.slice is O(n)) .

I think this is a good idea but I do have a concern here: A rollup banner plugin could inject a lengthy license banner into a file which could mean the debugId snippet only comes after the first 2000 characters (unlikely admittedly). We can say we're fine with this for the moment and remove the slicing if someone reports this. I'm just a bit worried that this would be hard for users and for us to find. Any thoughts on this?

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, this is the only concern I have as well. I just checked the starting snippet as it's most likely that the debug ID is in there and checking the whole file might create a performance problem (we don't know how large the file gets).

Or maybe we should just do String.includes() on the whole file as it's probably not as bad as it seems 🤔
It should be O(n+m) and as the snippet should be somewhere at the beginning, the search will be done quite fast. However, most files don't already have the snippet, so it would look through the whole file in most cases which increases search times drastically.

There are 3 options:

  1. String.includes() on whole file: will always detect the snippet but increases search times a lot
  2. String.includes() only for the top snippet (like now) - we would potentially miss some cases
  3. String.includes() only for the top snippet (but a larger snippet) - we would probably catch more cases but still not all of them

If we change something, I would only increase the slicing limit but not include the whole file. It's still better than before as before we didn't check for the existence of the snippet at all

Copy link
Member

Choose a reason for hiding this comment

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

yeah, let's do 3 -- sounds like a reasonable compromise :D thanks for writing up the options! I agree that the performance concern when checking the entire file is valid.

Copy link
Member Author

Choose a reason for hiding this comment

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

I'll increase it to 6000 chars - this is approximately 150 LOC

Comment on lines 34 to 36
expect(result).not.toBeNull();
expect(result?.code).toMatch(/^"use strict";.*;{try/);
});
Copy link
Member

Choose a reason for hiding this comment

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

l: wdyt about using toMatchInlineSnapshot here instead (this applies to most added tests here, but this one in particular would show much nicer where the snippet is actually injected)

@Lms24
Copy link
Member

Lms24 commented Dec 29, 2025

Also, thanks for adding the additional tests!

@s1gr1d s1gr1d merged commit ef6f4bb into main Dec 29, 2025
24 checks passed
@s1gr1d s1gr1d deleted the sig/prevent-debug-id-double-inject branch December 29, 2025 15:19
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.

3 participants