From fc006cbc6680a5346dfa990db8f36a000bb94a6b Mon Sep 17 00:00:00 2001 From: Ethan Setnik Date: Sat, 14 Nov 2026 10:02:36 -0500 Subject: [PATCH 1/4] fix(cli): Recognize debug ID snippets that use template literals `sourcemap inject` and `sourcemap upload` decide whether a file already registers its debug ID by matching the registry write, and only accepted a quoted ID. Oxc, the minifier in Vite 8, rewrites a bundler plugin's `e._sentryDebugIds[n]=""` to a template literal, so the check missed it and a second snippet was prepended on a new first line. Through @sentry/vite-plugin this shifted every uploaded source map one line ahead of the deployed chunk, because the plugin uploads prepared copies and the deployed files are not touched. Accept backticks wherever a quote is accepted. Fixes #2267 Co-Authored-By: Claude Fable 5.2 --- packages/cli/src/lib/sourcemap/debug-id.ts | 5 ++++- packages/cli/test/lib/sourcemap/inject.test.ts | 4 ++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/packages/cli/src/lib/sourcemap/debug-id.ts b/packages/cli/src/lib/sourcemap/debug-id.ts index c2ec98824..59d4a7258 100644 --- a/packages/cli/src/lib/sourcemap/debug-id.ts +++ b/packages/cli/src/lib/sourcemap/debug-id.ts @@ -59,9 +59,12 @@ const DEBUGID_REGISTRY = "(?:_sentryDebugIds|_debugIds)"; * Indexed registry writes emitted by Sentry's bundler plugins, the legacy CLI * (`e._sentryDebugIds[n]=""`), and Turbopack, which initializes the * registry inline: `(e._debugIds|| (e._debugIds={}))[n]=""`. + * + * A string may be quoted with backticks as well as quotes: minifiers such as + * Oxc (Vite 8) rewrite a bundler plugin's `""` to a template literal. */ const DEBUGID_REGISTRATION_RE = new RegExp( - `(?:\\.\\s*${DEBUGID_REGISTRY}|\\[\\s*["']${DEBUGID_REGISTRY}["']\\s*\\])(?:\\s*=\\s*\\{\\s*\\}\\s*\\)\\s*\\))?\\s*\\[\\s*[$\\w]+\\s*\\]\\s*=\\s*(["'])(${DEBUGID_PATTERN})\\1`, + `(?:\\.\\s*${DEBUGID_REGISTRY}|\\[\\s*["'\`]${DEBUGID_REGISTRY}["'\`]\\s*\\])(?:\\s*=\\s*\\{\\s*\\}\\s*\\)\\s*\\))?\\s*\\[\\s*[$\\w]+\\s*\\]\\s*=\\s*(["'\`])(${DEBUGID_PATTERN})\\1`, "g", ); diff --git a/packages/cli/test/lib/sourcemap/inject.test.ts b/packages/cli/test/lib/sourcemap/inject.test.ts index 0d42a88a2..c1d3caad7 100644 --- a/packages/cli/test/lib/sourcemap/inject.test.ts +++ b/packages/cli/test/lib/sourcemap/inject.test.ts @@ -638,6 +638,10 @@ describe.each(["external", "inline"] as const)( })();`, // Emitted by Next.js 16 with `turbopack.debugIds` (getsentry/toolkit#2189). turbopack: `;!function(){try { var e="undefined"!=typeof globalThis?globalThis:"undefined"!=typeof global?global:"undefined"!=typeof window?window:"undefined"!=typeof self?self:{},n=(new e.Error).stack;n&&((e._debugIds|| (e._debugIds={}))[n]="${debugId}")}catch(e){}}();`, + // A bundler plugin's snippet after Oxc minification in Vite 8, which + // rewrites its string literals to template literals + // (getsentry/toolkit#2267). + templateLiterals: `(function(){try{var e=typeof globalThis<\`u\`?globalThis:typeof window<\`u\`?window:typeof global<\`u\`?global:typeof self<\`u\`?self:{};var t=new e.Error().stack;t&&(e._sentryDebugIds=e._sentryDebugIds||{},e._sentryDebugIds[t]=\`${debugId}\`,e._sentryDebugIdIdentifier=\`sentry-dbid-${debugId}\`)}catch{}})();`, }; test.each(Object.keys(snippets))( From c136771a64ad7222b20a1563403651ec48e3badc Mon Sep 17 00:00:00 2001 From: Ethan Setnik Date: Sat, 14 Nov 2026 10:22:34 -0500 Subject: [PATCH 2/4] fix(cli): Recognize a debug ID registry initialized with ||= Oxc also folds Turbopack's inline registry initialization, `(e._debugIds||(e._debugIds={}))[n]`, into a logical assignment, `(e._debugIds||={})[n]`. Accept that form, and `??=`, so a Turbopack chunk minified by Oxc is not given a second snippet either. Co-Authored-By: Claude Fable 5.2 --- packages/cli/src/lib/sourcemap/debug-id.ts | 8 +++++--- packages/cli/test/lib/sourcemap/inject.test.ts | 3 +++ 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/packages/cli/src/lib/sourcemap/debug-id.ts b/packages/cli/src/lib/sourcemap/debug-id.ts index 59d4a7258..d293010f4 100644 --- a/packages/cli/src/lib/sourcemap/debug-id.ts +++ b/packages/cli/src/lib/sourcemap/debug-id.ts @@ -60,11 +60,13 @@ const DEBUGID_REGISTRY = "(?:_sentryDebugIds|_debugIds)"; * (`e._sentryDebugIds[n]=""`), and Turbopack, which initializes the * registry inline: `(e._debugIds|| (e._debugIds={}))[n]=""`. * - * A string may be quoted with backticks as well as quotes: minifiers such as - * Oxc (Vite 8) rewrite a bundler plugin's `""` to a template literal. + * Minifiers such as Oxc (Vite 8) rewrite these, so two more forms are + * accepted: a string quoted with backticks (`""` becomes a template + * literal), and the inline initialization written with a logical assignment, + * `(e._debugIds||={})[n]` or `??=`. */ const DEBUGID_REGISTRATION_RE = new RegExp( - `(?:\\.\\s*${DEBUGID_REGISTRY}|\\[\\s*["'\`]${DEBUGID_REGISTRY}["'\`]\\s*\\])(?:\\s*=\\s*\\{\\s*\\}\\s*\\)\\s*\\))?\\s*\\[\\s*[$\\w]+\\s*\\]\\s*=\\s*(["'\`])(${DEBUGID_PATTERN})\\1`, + `(?:\\.\\s*${DEBUGID_REGISTRY}|\\[\\s*["'\`]${DEBUGID_REGISTRY}["'\`]\\s*\\])(?:\\s*=\\s*\\{\\s*\\}\\s*\\)\\s*\\)|\\s*(?:\\|\\||\\?\\?)=\\s*\\{\\s*\\}\\s*\\))?\\s*\\[\\s*[$\\w]+\\s*\\]\\s*=\\s*(["'\`])(${DEBUGID_PATTERN})\\1`, "g", ); diff --git a/packages/cli/test/lib/sourcemap/inject.test.ts b/packages/cli/test/lib/sourcemap/inject.test.ts index c1d3caad7..e9f1ed38e 100644 --- a/packages/cli/test/lib/sourcemap/inject.test.ts +++ b/packages/cli/test/lib/sourcemap/inject.test.ts @@ -642,6 +642,9 @@ describe.each(["external", "inline"] as const)( // rewrites its string literals to template literals // (getsentry/toolkit#2267). templateLiterals: `(function(){try{var e=typeof globalThis<\`u\`?globalThis:typeof window<\`u\`?window:typeof global<\`u\`?global:typeof self<\`u\`?self:{};var t=new e.Error().stack;t&&(e._sentryDebugIds=e._sentryDebugIds||{},e._sentryDebugIds[t]=\`${debugId}\`,e._sentryDebugIdIdentifier=\`sentry-dbid-${debugId}\`)}catch{}})();`, + // Turbopack's snippet after the same minification, which also folds + // the inline registry initialization into a logical assignment. + turbopackLogicalAssignment: `(function(){try{var e=typeof globalThis<\`u\`?globalThis:typeof global<\`u\`?global:typeof window<\`u\`?window:typeof self<\`u\`?self:{},t=new e.Error().stack;t&&((e._debugIds||={})[t]=\`${debugId}\`)}catch{}})();`, }; test.each(Object.keys(snippets))( From 3b36a4adc8b6215a55b7d723850abf3cbf3103f3 Mon Sep 17 00:00:00 2001 From: "sentry-junior[bot]" <264270552+sentry-junior[bot]@users.noreply.github.com> Date: Sat, 14 Nov 2026 15:38:07 +0000 Subject: [PATCH 3/4] test(cli): Pin every debug ID registration form and its near misses Each form the registration check accepts now has a test that fails when that form is removed: `??=` and a bracketed registry name in backticks had none. Add near misses (an interpolated template, `&&=`, and `==`) that must still get a snippet, since this check gates skipping injection. Co-Authored-By: BYK <126780+BYK@users.noreply.github.com> --- .../cli/test/lib/sourcemap/inject.test.ts | 49 +++++++++++++++++++ 1 file changed, 49 insertions(+) diff --git a/packages/cli/test/lib/sourcemap/inject.test.ts b/packages/cli/test/lib/sourcemap/inject.test.ts index e9f1ed38e..1d8a04bf8 100644 --- a/packages/cli/test/lib/sourcemap/inject.test.ts +++ b/packages/cli/test/lib/sourcemap/inject.test.ts @@ -645,6 +645,10 @@ describe.each(["external", "inline"] as const)( // Turbopack's snippet after the same minification, which also folds // the inline registry initialization into a logical assignment. turbopackLogicalAssignment: `(function(){try{var e=typeof globalThis<\`u\`?globalThis:typeof global<\`u\`?global:typeof window<\`u\`?window:typeof self<\`u\`?self:{},t=new e.Error().stack;t&&((e._debugIds||={})[t]=\`${debugId}\`)}catch{}})();`, + // No minifier tested emits these two, but the registration check + // accepts them, so they pin those branches. + nullishAssignment: `(function(){var e=globalThis,t=new e.Error().stack;t&&((e._debugIds??={})[t]=\`${debugId}\`)})();`, + bracketTemplateRegistry: `(function(){var e=globalThis,t=new e.Error().stack;e[\`_sentryDebugIds\`]=e[\`_sentryDebugIds\`]||{};e[\`_sentryDebugIds\`][t]=\`${debugId}\`})();`, }; test.each(Object.keys(snippets))( @@ -768,6 +772,51 @@ describe.each(["external", "inline"] as const)( ); }, ); + + // Near misses of the template literal and logical assignment forms that + // do not register the ID, so injection must still happen. + const nearMisses: Record = { + interpolatedTemplate: `var e=globalThis,t="s";e._sentryDebugIds=e._sentryDebugIds||{};e._sentryDebugIds[t]=\`\${t}${debugId}\`;`, + andAssignment: `var e=globalThis,t="s";try{(e._debugIds&&={})[t]=\`${debugId}\`}catch{}`, + comparison: `var e=globalThis,t="s";e._sentryDebugIds=e._sentryDebugIds||{};e._sentryDebugIds[t]==\`${debugId}\`;`, + }; + + test.each(Object.keys(nearMisses))( + "injects despite a %s near miss", + async (format) => { + const jsPath = join(dir, "main.js"); + const mapContent = JSON.stringify({ + version: 3, + sources: ["main.ts"], + names: [], + mappings: "AAAA", + debugId, + }); + const mapUrl = + kind === "inline" + ? `data:application/json;base64,${Buffer.from(mapContent).toString("base64")}` + : "main.js.map"; + const js = `${nearMisses[format]}\n//# debugId=${debugId}\n//# sourceMappingURL=${mapUrl}\n`; + writeFileSync(jsPath, js); + if (kind === "external") { + writeFileSync(`${jsPath}.map`, mapContent); + } + + expect((await injectDirectory(dir))[0]).toMatchObject({ + debugId, + injected: true, + }); + const sandbox: { _sentryDebugIds?: Record } = {}; + runInNewContext(readFileSync(jsPath, "utf-8"), sandbox, { + filename: "main.js", + }); + expect( + Object.values(sandbox._sentryDebugIds ?? {}).filter( + (id) => id === debugId, + ), + ).toHaveLength(1); + }, + ); }, ); From db3441c9bfe9c2eacc9c713d9ce445c8c4ca17a0 Mon Sep 17 00:00:00 2001 From: Ethan Setnik Date: Sat, 14 Nov 2026 10:50:22 -0500 Subject: [PATCH 4/4] fix(cli): Recognize a debug ID registered on a bare global The React Native Metro plugin registers on a global variable, `_sentryDebugIds[stack]=""`, not on a property of the global object. The check required a `.` or a bracketed name before the registry, so a Metro bundle was given a second snippet as well. This one does not depend on a minifier. Accept the registry name when it is not part of a longer identifier. Co-Authored-By: Claude Fable 5.2 --- packages/cli/src/lib/sourcemap/debug-id.ts | 6 +++++- packages/cli/test/lib/sourcemap/inject.test.ts | 9 +++++++-- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/packages/cli/src/lib/sourcemap/debug-id.ts b/packages/cli/src/lib/sourcemap/debug-id.ts index d293010f4..e90301bd6 100644 --- a/packages/cli/src/lib/sourcemap/debug-id.ts +++ b/packages/cli/src/lib/sourcemap/debug-id.ts @@ -60,13 +60,17 @@ const DEBUGID_REGISTRY = "(?:_sentryDebugIds|_debugIds)"; * (`e._sentryDebugIds[n]=""`), and Turbopack, which initializes the * registry inline: `(e._debugIds|| (e._debugIds={}))[n]=""`. * + * The registry may also be a bare global rather than a property: the React + * Native Metro plugin emits `_sentryDebugIds[stack]=""`. A longer + * identifier that merely ends with the registry name does not count. + * * Minifiers such as Oxc (Vite 8) rewrite these, so two more forms are * accepted: a string quoted with backticks (`""` becomes a template * literal), and the inline initialization written with a logical assignment, * `(e._debugIds||={})[n]` or `??=`. */ const DEBUGID_REGISTRATION_RE = new RegExp( - `(?:\\.\\s*${DEBUGID_REGISTRY}|\\[\\s*["'\`]${DEBUGID_REGISTRY}["'\`]\\s*\\])(?:\\s*=\\s*\\{\\s*\\}\\s*\\)\\s*\\)|\\s*(?:\\|\\||\\?\\?)=\\s*\\{\\s*\\}\\s*\\))?\\s*\\[\\s*[$\\w]+\\s*\\]\\s*=\\s*(["'\`])(${DEBUGID_PATTERN})\\1`, + `(?:(? = { interpolatedTemplate: `var e=globalThis,t="s";e._sentryDebugIds=e._sentryDebugIds||{};e._sentryDebugIds[t]=\`\${t}${debugId}\`;`, andAssignment: `var e=globalThis,t="s";try{(e._debugIds&&={})[t]=\`${debugId}\`}catch{}`, comparison: `var e=globalThis,t="s";e._sentryDebugIds=e._sentryDebugIds||{};e._sentryDebugIds[t]==\`${debugId}\`;`, + // A different identifier that only ends with the registry name. + longerIdentifier: `var my_sentryDebugIds={},t="s";my_sentryDebugIds[t]="${debugId}";`, }; test.each(Object.keys(nearMisses))(