From 0d76ca7bac6e2782e75cf671fee2eb60ac2f09d2 Mon Sep 17 00:00:00 2001 From: "sentry-junior[bot]" <264270552+sentry-junior[bot]@users.noreply.github.com> Date: Mon, 30 Nov 2026 12:19:05 +0000 Subject: [PATCH] fix(cli): Send a runtime-registered debug ID with upload --no-rewrite A bundler plugin can leave only its runtime snippet in the output, with no `//# debugId=` comment and no map ID. `sourcemap upload --no-rewrite` read the ID only from the comment or the map, so it sent none for such a file. Without `--release`, a bundle with no debug IDs is rejected, and otherwise the file can't be found by the ID its events carry. Fall back to the ID the file registers at runtime, as `inject` does since #2270. Binary bundles are not read, and a file that registers several different IDs still sends none. Fixes #2595 --- .../src/fragments/commands/sourcemap.md | 9 +- packages/cli/src/lib/sourcemap/inject.ts | 12 ++- .../test/commands/sourcemap/upload.test.ts | 86 +++++++++++++++++++ 3 files changed, 102 insertions(+), 5 deletions(-) diff --git a/apps/cli-docs/src/fragments/commands/sourcemap.md b/apps/cli-docs/src/fragments/commands/sourcemap.md index 04e8889a4..6a20e1476 100644 --- a/apps/cli-docs/src/fragments/commands/sourcemap.md +++ b/apps/cli-docs/src/fragments/commands/sourcemap.md @@ -7,9 +7,12 @@ Existing debug IDs from JavaScript comments or sourcemaps are preserved. If a bundler emits an ID without Sentry's runtime registration snippet, `inject` adds the snippet and adjusts the sourcemap mappings. Bundles that already -register their ID are left unchanged. `sourcemap upload` applies the same -injection by default, unless `--no-rewrite` is passed. With `--no-rewrite`, -files upload unchanged, along with any debug IDs they already carry. +register their ID are left unchanged. A bundle that registers an ID at runtime +but has no comment and no sourcemap ID keeps that ID, and `inject` writes it to +the comment and the sourcemap. `sourcemap upload` applies the same injection by +default, unless `--no-rewrite` is passed. With `--no-rewrite`, files upload +unchanged, along with any debug IDs they already carry, including one they only +register at runtime. Binary bundles with an existing debug ID in their sourcemap are left unchanged. ## Resolving sourcemap linkage diff --git a/packages/cli/src/lib/sourcemap/inject.ts b/packages/cli/src/lib/sourcemap/inject.ts index f385ed142..1491edfd4 100644 --- a/packages/cli/src/lib/sourcemap/inject.ts +++ b/packages/cli/src/lib/sourcemap/inject.ts @@ -226,6 +226,8 @@ export async function injectDirectory( * Read the debug IDs that discovered pairs already carry, without changing * any file. Used by `sourcemap upload --no-rewrite`, which uploads the files * as-is but must still send the IDs a bundler or an earlier `inject` wrote. + * A file with no comment and no map ID may still register one at runtime; + * that ID is sent, as `inject` would reuse it. * * @param pairs - Pairs from {@link discoverFilePairs} * @returns One result per pair, with `injected: false` and `debugId` set to @@ -236,13 +238,19 @@ export async function readExistingDebugIds( ): Promise { const results: InjectResult[] = []; for (const { jsPath, map } of pairs) { - const js = await readFile(jsPath, "utf-8"); + const jsBytes = await readFile(jsPath); + const js = jsBytes.toString("utf-8"); + const existing = await readPairDebugId(js, map); + const registered = + existing || isLikelyBinary(jsBytes) + ? undefined + : readRegisteredDebugId(js); results.push({ jsPath, map, mapPath: map.kind === "external" ? map.mapPath : undefined, injected: false, - debugId: (await readPairDebugId(js, map)) ?? "", + debugId: existing ?? registered ?? "", }); } return results; diff --git a/packages/cli/test/commands/sourcemap/upload.test.ts b/packages/cli/test/commands/sourcemap/upload.test.ts index 9469d428a..753ae66b0 100644 --- a/packages/cli/test/commands/sourcemap/upload.test.ts +++ b/packages/cli/test/commands/sourcemap/upload.test.ts @@ -712,6 +712,92 @@ describe("sourcemap upload command — --allow-empty behavior", () => { } }); + // getsentry/toolkit#2595: a bundler plugin can leave only its runtime + // snippet in the output, with no comment and no map ID. `--no-rewrite` must + // send the ID the snippet registers, which is the one events carry. + test.each([ + { + name: "plugin snippet", + js: `${getDebugIdSnippet(existingId)}\nconsole.log(1)\n//# sourceMappingURL=app.js.map\n`, + }, + { + name: "Oxc-minified snippet", + js: `(function(){try{var e=typeof globalThis<\`u\`?globalThis:{},t=new e.Error().stack;t&&(e._sentryDebugIds=e._sentryDebugIds||{},e._sentryDebugIds[t]=\`${existingId}\`)}catch{}})();\nconsole.log(1)\n`, + }, + ])( + "--no-rewrite: sends the debug ID the bundle registers ($name), files untouched", + async ({ js }) => { + const jsPath = join(dir, "app.js"); + const mapPath = join(dir, "app.js.map"); + const mapJson = JSON.stringify(plainMap); + await writeFile(jsPath, js); + await writeFile(mapPath, mapJson); + + const uploadSpy = vi + .spyOn(sourcemapsApi, "uploadSourcemaps") + .mockResolvedValue(undefined); + try { + const ctx = makeContext(); + await func.call(ctx, { "no-rewrite": true }, dir); + const files = uploadSpy.mock.calls[0]?.[0]?.files ?? []; + expect(files.map((f) => [f.type, f.debugId])).toEqual([ + ["minified_source", existingId], + ["source_map", existingId], + ]); + expect(await readFile(jsPath, "utf-8")).toBe(js); + expect(await readFile(mapPath, "utf-8")).toBe(mapJson); + } finally { + uploadSpy.mockRestore(); + } + }, + ); + + test("--no-rewrite + inline map: sends the debug ID the bundle registers", async () => { + const jsPath = join(dir, "inline-norw-registered.js"); + const dataUrl = `data:application/json;base64,${Buffer.from(JSON.stringify(plainMap)).toString("base64")}`; + const js = `${getDebugIdSnippet(existingId)}\nconsole.log(1)\n//# sourceMappingURL=${dataUrl}\n`; + await writeFile(jsPath, js); + + const uploadSpy = vi + .spyOn(sourcemapsApi, "uploadSourcemaps") + .mockResolvedValue(undefined); + try { + const ctx = makeContext(); + await func.call(ctx, { "no-rewrite": true }, dir); + const files = uploadSpy.mock.calls[0]?.[0]?.files ?? []; + expect(files.map((f) => [f.type, f.debugId])).toEqual([ + ["minified_source", existingId], + ["source_map", existingId], + ]); + expect(await readFile(jsPath, "utf-8")).toBe(js); + } finally { + uploadSpy.mockRestore(); + } + }); + + test("--no-rewrite: sends no debug ID when the bundle registers two", async () => { + const otherId = "99999999-8888-4777-9666-555555555555"; + await writeFile( + join(dir, "app.js"), + `${getDebugIdSnippet(existingId)}\n${getDebugIdSnippet(otherId)}\nconsole.log(1)\n`, + ); + await writeFile(join(dir, "app.js.map"), JSON.stringify(plainMap)); + + const uploadSpy = vi + .spyOn(sourcemapsApi, "uploadSourcemaps") + .mockResolvedValue(undefined); + try { + const ctx = makeContext(); + await func.call(ctx, { "no-rewrite": true }, dir); + expect(uploadSpy).toHaveBeenCalledTimes(1); + for (const file of uploadSpy.mock.calls[0]?.[0]?.files ?? []) { + expect(file.debugId).toBeUndefined(); + } + } finally { + uploadSpy.mockRestore(); + } + }); + test("--ext: discovers files with custom extensions", async () => { await writeFile(join(dir, "app.ts"), "console.log(1)\n"); await writeFile(