From 16baa375762eeb9594cdcd5d83bb812d67f55e9d 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/2] 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> --- .../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 54adde5e4..8c373bf07 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 4ab4df33d..c102f8876 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 ffac92771..a0a80a0db 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 19ba74f34..bd7f80d2e 100644 --- a/packages/cli/test/commands/sourcemap/upload.test.ts +++ b/packages/cli/test/commands/sourcemap/upload.test.ts @@ -340,6 +340,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 49d54634606e92057f04711dec80e37194a88e50 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/2] 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> --- 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 a0a80a0db..62c44ebb7 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); }