Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions apps/cli-docs/src/fragments/commands/sourcemap.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 10 additions & 2 deletions packages/cli/src/lib/sourcemap/inject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -236,13 +238,19 @@ export async function readExistingDebugIds(
): Promise<InjectResult[]> {
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;
Expand Down
86 changes: 86 additions & 0 deletions packages/cli/test/commands/sourcemap/upload.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
Loading