From dcb104753e4635e16b3ab51854b899fc19ad3c99 Mon Sep 17 00:00:00 2001 From: "sentry-junior[bot]" <264270552+sentry-junior[bot]@users.noreply.github.com> Date: Thu, 12 Nov 2026 20:19:12 +0000 Subject: [PATCH 1/3] fix(cli): Skip a missing directory in sourcemap upload --allow-empty Bundler plugins upload each build output in turn with allowEmpty. A build output that does not exist threw and stopped the outputs after it. --allow-empty now treats a missing directory like an empty one: it uploads nothing, names the directory, and exits 0. Without the flag, a missing directory still fails. Co-Authored-By: bete <28983162+betegon@users.noreply.github.com> Co-Authored-By: BYK <126780+BYK@users.noreply.github.com> --- .../src/fragments/commands/sourcemap.md | 3 +- .../skills/sentry-cli/references/sourcemap.md | 2 +- packages/cli/src/commands/sourcemap/upload.ts | 37 ++++++++++++------- .../test/commands/sourcemap/upload.test.ts | 11 ++++++ 4 files changed, 38 insertions(+), 15 deletions(-) diff --git a/apps/cli-docs/src/fragments/commands/sourcemap.md b/apps/cli-docs/src/fragments/commands/sourcemap.md index 54adde5e4f..8c373bf073 100644 --- a/apps/cli-docs/src/fragments/commands/sourcemap.md +++ b/apps/cli-docs/src/fragments/commands/sourcemap.md @@ -37,4 +37,5 @@ common cause is a bundler that isn't emitting `.map` files: For CI steps that may run against legitimately-empty directories (e.g., library-only repos, conditional release skips), pass `--allow-empty` to -suppress the error. +suppress the error. With `--allow-empty`, `sourcemap upload` also skips a +directory that does not exist and names it in the output. diff --git a/packages/cli/plugins/sentry-cli/skills/sentry-cli/references/sourcemap.md b/packages/cli/plugins/sentry-cli/skills/sentry-cli/references/sourcemap.md index a13c4d371b..cfcd85612f 100644 --- a/packages/cli/plugins/sentry-cli/skills/sentry-cli/references/sourcemap.md +++ b/packages/cli/plugins/sentry-cli/skills/sentry-cli/references/sourcemap.md @@ -64,7 +64,7 @@ Upload sourcemaps to Sentry - `--strip-prefix - Strip a prefix from uploaded file paths (e.g. 'build/')` - `--strip-common-prefix - Automatically strip the longest common path prefix from all files` - `--no-rewrite - Upload files as-is without injecting debug IDs` -- `--allow-empty - Exit successfully when no JS + sourcemap pairs are found (default: error out to catch silent build misconfigurations)` +- `--allow-empty - Exit successfully when no JS + sourcemap pairs are found or the directory does not exist (default: error out to catch silent build misconfigurations)` **Examples:** diff --git a/packages/cli/src/commands/sourcemap/upload.ts b/packages/cli/src/commands/sourcemap/upload.ts index 328acac1e1..6d35aee4be 100644 --- a/packages/cli/src/commands/sourcemap/upload.ts +++ b/packages/cli/src/commands/sourcemap/upload.ts @@ -6,6 +6,7 @@ * env vars, config defaults) — no slash-separated arg parsing needed. */ +import { existsSync } from "node:fs"; import { relative, resolve } from "node:path"; import { dirname as posixDirname, @@ -223,7 +224,7 @@ export const uploadCommand = buildCommand({ "Exits with an error if zero JS + sourcemap pairs are discovered " + "(typical cause: bundler not emitting .map files). Pass " + "--allow-empty to suppress this check for directories that may " + - "legitimately be empty.", + "legitimately be empty or missing.", examples: [ { description: "Upload sourcemaps from dist/", @@ -335,8 +336,9 @@ export const uploadCommand = buildCommand({ "allow-empty": { kind: "boolean", brief: - "Exit successfully when no JS + sourcemap pairs are found " + - "(default: error out to catch silent build misconfigurations)", + "Exit successfully when no JS + sourcemap pairs are found or " + + "the directory does not exist (default: error out to catch " + + "silent build misconfigurations)", optional: true, default: false, }, @@ -362,7 +364,13 @@ export const uploadCommand = buildCommand({ // don't write debug IDs when the upload won't proceed (empty dir, // typoed path, missing credentials). const target = await resolveUploadTarget(dir); - await assertDirectoryReadable(target.dir); + // --allow-empty treats a missing directory like an empty one, so callers + // that upload several build outputs one at a time (the bundler plugins) + // can skip an output this build didn't produce. + const missing = flags["allow-empty"] && !existsSync(target.dir); + if (!missing) { + await assertDirectoryReadable(target.dir); + } const extensions = flags.ext?.split(",").map((e) => e.trim()); const extSet = extensions @@ -377,12 +385,14 @@ export const uploadCommand = buildCommand({ flags["ignore-file"], ); - const pairs = await discoverFilePairs( - target.dir, - extSet, - ignoreMatcher, - target.include, - ); + const pairs = missing + ? [] + : await discoverFilePairs( + target.dir, + extSet, + ignoreMatcher, + target.include, + ); if (pairs.length === 0) { if (!flags["allow-empty"]) { @@ -401,9 +411,10 @@ export const uploadCommand = buildCommand({ filesUploaded: 0, }); return { - hint: - "No JS + sourcemap pairs found in the target directory. " + - "If this is unexpected, check your bundler emits .map files.", + hint: missing + ? `Directory '${target.dir}' does not exist, so nothing was uploaded from it.` + : "No JS + sourcemap pairs found in the target directory. " + + "If this is unexpected, check your bundler emits .map files.", }; } diff --git a/packages/cli/test/commands/sourcemap/upload.test.ts b/packages/cli/test/commands/sourcemap/upload.test.ts index 8f0d3f2639..d13b28fcc0 100644 --- a/packages/cli/test/commands/sourcemap/upload.test.ts +++ b/packages/cli/test/commands/sourcemap/upload.test.ts @@ -341,6 +341,17 @@ describe("sourcemap upload command — --allow-empty behavior", () => { } }); + test("non-existent directory with --allow-empty: skips it and names it", async () => { + // Bundler plugins upload each build output in turn with allowEmpty, so a + // missing output must not abort the outputs after it. + const missing = join(dir, "does-not-exist"); + const ctx = makeContext(); + await func.call(ctx, { "allow-empty": true }, missing); + const out = ctx.stdout.write.mock.calls.join(""); + expect(out).toMatch(/Files uploaded\W+0/); + expect(out).toContain(`Directory '${missing}' does not exist`); + }); + test("error path does not mutate files (js-only dir)", async () => { // Discovery must be read-only — injection only runs once we've // decided the upload will proceed. From 182764fdba72184b70930bc5ba9dd2fdc3170634 Mon Sep 17 00:00:00 2001 From: "sentry-junior[bot]" <264270552+sentry-junior[bot]@users.noreply.github.com> Date: Thu, 12 Nov 2026 20:31:54 +0000 Subject: [PATCH 2/3] fix(cli): Only treat ENOENT as a missing upload directory existsSync is false for any stat failure, so an inaccessible path was skipped under --allow-empty instead of failing. Co-Authored-By: bete <28983162+betegon@users.noreply.github.com> Co-Authored-By: BYK <126780+BYK@users.noreply.github.com> --- packages/cli/src/commands/sourcemap/upload.ts | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/packages/cli/src/commands/sourcemap/upload.ts b/packages/cli/src/commands/sourcemap/upload.ts index 6d35aee4be..7db0d1204f 100644 --- a/packages/cli/src/commands/sourcemap/upload.ts +++ b/packages/cli/src/commands/sourcemap/upload.ts @@ -6,7 +6,7 @@ * env vars, config defaults) — no slash-separated arg parsing needed. */ -import { existsSync } from "node:fs"; +import { stat } from "node:fs/promises"; import { relative, resolve } from "node:path"; import { dirname as posixDirname, @@ -78,6 +78,16 @@ function formatUploadResult(data: UploadCommandResult): string { const usageHint = () => sentry`sourcemap upload `; +/** Whether `path` does not exist. Other stat failures (e.g. EACCES) are not "missing". */ +async function isMissingPath(path: string): Promise { + try { + await stat(path); + return false; + } catch (err) { + return (err as NodeJS.ErrnoException).code === "ENOENT"; + } +} + /** * Compute the longest common directory prefix across a list of paths. * @@ -367,7 +377,7 @@ export const uploadCommand = buildCommand({ // --allow-empty treats a missing directory like an empty one, so callers // that upload several build outputs one at a time (the bundler plugins) // can skip an output this build didn't produce. - const missing = flags["allow-empty"] && !existsSync(target.dir); + const missing = flags["allow-empty"] && (await isMissingPath(target.dir)); if (!missing) { await assertDirectoryReadable(target.dir); } From 78c72e757facbfcfac17b36796a56dbc5234492c Mon Sep 17 00:00:00 2001 From: "sentry-junior[bot]" <264270552+sentry-junior[bot]@users.noreply.github.com> Date: Thu, 26 Nov 2026 11:02:34 +0000 Subject: [PATCH 3/3] test(cli): Cover what sourcemap upload --allow-empty still rejects A missing glob base is skipped like a missing directory. A path that is a file, or one whose stat fails with anything but ENOENT (here ENOTDIR), still fails with the flag. Without the flag, a missing directory fails with the exact "does not exist" message. Co-Authored-By: BYK <126780+BYK@users.noreply.github.com> --- .../test/commands/sourcemap/upload.test.ts | 32 +++++++++++++++++-- 1 file changed, 30 insertions(+), 2 deletions(-) diff --git a/packages/cli/test/commands/sourcemap/upload.test.ts b/packages/cli/test/commands/sourcemap/upload.test.ts index d13b28fcc0..fd06607662 100644 --- a/packages/cli/test/commands/sourcemap/upload.test.ts +++ b/packages/cli/test/commands/sourcemap/upload.test.ts @@ -336,8 +336,9 @@ describe("sourcemap upload command — --allow-empty behavior", () => { expect.unreachable("should have thrown"); } catch (err) { expect(err).toBeInstanceOf(ValidationError); - const msg = (err as Error).message; - expect(msg).toMatch(/does not exist/i); + expect((err as Error).message).toBe( + `Directory '${missing}' does not exist.`, + ); } }); @@ -352,6 +353,23 @@ describe("sourcemap upload command — --allow-empty behavior", () => { expect(out).toContain(`Directory '${missing}' does not exist`); }); + test("path that is a file with --allow-empty: still throws", async () => { + const file = join(dir, "bundle.js"); + await writeFile(file, "console.log(1)\n"); + await expect( + func.call(makeContext(), { "allow-empty": true }, file), + ).rejects.toThrow(`Path '${file}' is not a directory.`); + }); + + test("stat failure other than ENOENT with --allow-empty: still throws", async () => { + // A path under a file fails with ENOTDIR, which is not a missing directory. + await writeFile(join(dir, "bundle.js"), "console.log(1)\n"); + const target = join(dir, "bundle.js", "dist"); + await expect( + func.call(makeContext(), { "allow-empty": true }, target), + ).rejects.toThrow(`Cannot read directory '${target}'`); + }); + test("error path does not mutate files (js-only dir)", async () => { // Discovery must be read-only — injection only runs once we've // decided the upload will proceed. @@ -715,6 +733,16 @@ describe("sourcemap upload command — --allow-empty behavior", () => { ).rejects.toThrow(`Directory '${join(dir, "missing")}' does not exist.`); }); + test("missing glob base with --allow-empty: skips the base directory", async () => { + const ctx = makeContext(); + await func.call(ctx, { "allow-empty": true }, join(dir, "missing", "**")); + const out = ctx.stdout.write.mock.calls.join(""); + expect(out).toMatch(/Files uploaded\W+0/); + expect(out).toContain( + `Directory '${join(dir, "missing")}' does not exist`, + ); + }); + test("glob that matches no pairs: throws the zero-pairs error", async () => { await writePair("static/app");