Skip to content
Open
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
3 changes: 2 additions & 1 deletion apps/cli-docs/src/fragments/commands/sourcemap.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ Upload sourcemaps to Sentry
- `--strip-prefix <value> - 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:**

Expand Down
47 changes: 34 additions & 13 deletions packages/cli/src/commands/sourcemap/upload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -77,6 +78,16 @@ function formatUploadResult(data: UploadCommandResult): string {

const usageHint = () => sentry`sourcemap upload <directory>`;

/** Whether `path` does not exist. Other stat failures (e.g. EACCES) are not "missing". */
async function isMissingPath(path: string): Promise<boolean> {
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.
*
Expand Down Expand Up @@ -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/",
Expand Down Expand Up @@ -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,
},
Expand All @@ -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);
}
Comment thread
sentry-junior[bot] marked this conversation as resolved.
Resolved

const extensions = flags.ext?.split(",").map((e) => e.trim());
const extSet = extensions
Expand All @@ -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"]) {
Expand All @@ -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.",
};
}

Expand Down
11 changes: 11 additions & 0 deletions packages/cli/test/commands/sourcemap/upload.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Loading