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..62c44ebb7 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 { stat } from "node:fs/promises"; import { relative, resolve } from "node:path"; import { dirname as posixDirname, @@ -77,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. * @@ -223,7 +234,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 +346,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 +374,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"] && (await isMissingPath(target.dir)); + if (!missing) { + await assertDirectoryReadable(target.dir); + } const extensions = flags.ext?.split(",").map((e) => e.trim()); const extSet = extensions @@ -377,12 +395,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 +421,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.