diff --git a/packages/cli/src/commands/debug-files/upload.ts b/packages/cli/src/commands/debug-files/upload.ts index 6b3be15ee..3a2d3d4e6 100644 --- a/packages/cli/src/commands/debug-files/upload.ts +++ b/packages/cli/src/commands/debug-files/upload.ts @@ -12,7 +12,8 @@ * and `max_wait` (clamps the processing wait). `.zip` archives are scanned in * place (disable with `--no-zips`); `--derived-data` additionally scans Xcode's * DerivedData folder on macOS. Managed PE assemblies that embed a Portable PDB - * have it extracted and uploaded automatically as a separate DIF, and + * have it extracted and uploaded automatically as a separate DIF, so does the + * `.wasm.map` source map next to a WebAssembly module, and * `--il2cpp-mapping` uploads Unity IL2CPP line mappings. BCSymbolMap * resolution (legacy `--symbol-maps`) is intentionally not supported: it only * applies to Apple Bitcode, which Apple has deprecated and the App Store no @@ -270,6 +271,35 @@ function appendIl2cppMapping(difs: DebugFileUpload[], file: PreparedDif): void { }); } +/** + * Append the source map next to a WebAssembly module as a separate DIF + * carrying the module's debug id. + * + * Emscripten (`-gsource-map`) and dart2wasm write the map as `.map`. + * Sentry detects the format from the JSON content and pairs the map with the + * module by debug id, so the map must carry the module's build id; the scanner + * only prepares modules that have one. A module scanned from inside a `.zip` + * has no file next to it, so no map is found. An unreadable map is skipped + * with a warning so it never aborts the upload of the module itself. + */ +function appendWasmSourceMap(difs: DebugFileUpload[], file: PreparedDif): void { + if (!file.objects.some((object) => object.fileFormat === "wasm")) { + return; + } + const mapPath = `${file.path}.map`; + if (!existsSync(mapPath)) { + return; + } + let content: Buffer; + try { + content = readFileSync(mapPath); + } catch (err) { + log.warn(`Could not read WebAssembly source map ${mapPath}, skipping`, err); + return; + } + difs.push({ name: basename(mapPath), debugId: file.debugId, content }); +} + /** * Convert prepared files into the DIF upload list. * @@ -300,6 +330,8 @@ function buildDifList( content: file.content, }); + appendWasmSourceMap(difs, file); + if (il2cppMapping) { appendIl2cppMapping(difs, file); } @@ -596,6 +628,10 @@ export const uploadCommand = buildCommand({ "recursed.\n\n" + "Managed PE assemblies (.NET) that embed a Portable PDB have it extracted " + "and uploaded automatically as a separate .pdb debug file.\n\n" + + "A WebAssembly module's source map (.wasm.map, next to the " + + "module) is uploaded automatically with the module's debug id, so " + + "Sentry can symbolicate it without DWARF. The module needs a build id; " + + "add one with sentry wasm-split.\n\n" + "With --il2cpp-mapping, Unity IL2CPP C++->C# line mappings are computed " + "from each file's referenced generated C++ sources and uploaded as " + "separate il2cpp debug files; combine with --include-sources to also " + diff --git a/packages/cli/test/commands/debug-files/upload.test.ts b/packages/cli/test/commands/debug-files/upload.test.ts index c623d9e27..19014a10f 100644 --- a/packages/cli/test/commands/debug-files/upload.test.ts +++ b/packages/cli/test/commands/debug-files/upload.test.ts @@ -31,6 +31,12 @@ import { import { setAuthToken } from "../../../src/lib/db/auth.js"; import { DEFAULT_SENTRY_URL } from "../../../src/lib/constants.js"; import { mintSntrysToken, useTestConfigDir } from "../../helpers.js"; +import { + byteVector, + customSection, + fromHex, + wasmModule, +} from "../../lib/wasm/helpers.js"; vi.mock("node:os", async (importOriginal) => { const actual = await importOriginal(); @@ -143,6 +149,31 @@ async function writeIl2cppFixture(): Promise { await writeFile(join(tempDir, "example.sym"), sym); } +/** Debug id the scanner reports for the WASM fixture: its build id as a UUID. */ +const WASM_DEBUG_ID = "a1b2c3d4-e5f6-4788-99aa-bbccddeeff00"; + +/** A minimal version 3 source map; the CLI uploads it without parsing it. */ +const WASM_SOURCE_MAP = JSON.stringify({ + version: 3, + sources: ["app.c"], + names: [], + mappings: "AAAA", +}); + +/** Write `app.wasm`: a module that carries only a `build_id` section. */ +async function writeWasm(): Promise { + const buildId = fromHex(WASM_DEBUG_ID.replaceAll("-", "")); + await writeFile( + join(tempDir, "app.wasm"), + wasmModule([customSection("build_id", byteVector(buildId))]), + ); +} + +/** Write `app.wasm.map` next to it, where Emscripten and dart2wasm put it. */ +async function writeWasmSourceMap(): Promise { + await writeFile(join(tempDir, "app.wasm.map"), WASM_SOURCE_MAP); +} + /** Run `debug-files upload` and capture stdout + exit code. */ async function runUpload(args: string[]): Promise<{ output: string; @@ -995,4 +1026,57 @@ describe("sentry debug-files upload", () => { expect(il2cpp?.debugId).toBe(KNOWN_DEBUG_ID); expect(il2cpp?.content).toBeInstanceOf(Buffer); }); + + // WASM source maps + + test("uploads a .wasm.map next to its .wasm with the module's debug id (dry-run)", async () => { + await writeWasm(); + await writeWasmSourceMap(); + const { output, exitCode } = await runUpload([ + tempDir, + "--no-upload", + "--json", + ]); + expect(exitCode).toBe(0); + const files = JSON.parse(output).files as { + name: string; + debugId?: string; + }[]; + expect(files).toEqual([ + { name: "app.wasm", debugId: WASM_DEBUG_ID }, + { name: "app.wasm.map", debugId: WASM_DEBUG_ID }, + ]); + }); + + test("a .wasm without a source map uploads alone", async () => { + await writeWasm(); + const { output } = await runUpload([tempDir, "--no-upload", "--json"]); + const files = JSON.parse(output).files as { name: string }[]; + expect(files.map((f) => f.name)).toEqual(["app.wasm"]); + }); + + test("a source map whose .wasm has no build id is not uploaded", async () => { + // The scanner drops a module with a nil debug id, so its map has nothing to pair with. + await writeFile(join(tempDir, "app.wasm"), wasmModule([])); + await writeWasmSourceMap(); + const { output } = await runUpload([tempDir, "--no-upload", "--json"]); + expect(JSON.parse(output).files).toEqual([]); + }); + + test("threads the source map's bytes through to uploadDebugFiles", async () => { + process.env["SENTRY_ORG"] = "test-org"; + process.env["SENTRY_PROJECT"] = "test-project"; + await writeWasm(); + await writeWasmSourceMap(); + const spy = vi + .spyOn(debugFilesApi, "uploadDebugFiles") + .mockResolvedValue([]); + + await runUpload([tempDir]); + expect(spy).toHaveBeenCalledTimes(1); + const difs = spy.mock.calls[0]?.[0]?.difs ?? []; + const map = difs.find((d) => d.name === "app.wasm.map"); + expect(map?.debugId).toBe(WASM_DEBUG_ID); + expect(map?.content.toString("utf8")).toBe(WASM_SOURCE_MAP); + }); });