Skip to content
Merged
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
2 changes: 2 additions & 0 deletions apps/cli-docs/src/content/docs/migrating-from-v3.md
Original file line number Diff line number Diff line change
Expand Up @@ -497,6 +497,8 @@ v4 produces richer human output (Markdown, rendered on a TTY) but stays
**script-friendly**:

- **Piping / non-TTY** automatically emits plain text — no ANSI codes.
Hints and footers (`Detected from git HEAD`, pagination, cache age) go
to stderr, so stdout holds only the result.
- **`--json`** on any command emits stable JSON for machines; combine with
`--fields` to select columns.
- Color respects `NO_COLOR`, `FORCE_COLOR`, and `SENTRY_PLAIN_OUTPUT`.
Expand Down
2 changes: 2 additions & 0 deletions apps/cli-docs/src/fragments/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ export SENTRY_DISABLE_TIPS=1 # per-session, overrides the stored preference

Only the tips are removed. Pagination hints, dry-run and cancellation notices, and the cache-age footer (`cached · 3m ago · use -f to refresh`) are still shown.

Hints print below the output only on a terminal. When stdout is piped or redirected, they go to stderr, so `$(sentry release propose-version)` captures just the version.

## Telemetry

The CLI sends usage data and error reports about itself to Sentry. To opt out, set `SENTRY_CLI_NO_TELEMETRY=1` or `DO_NOT_TRACK=1`, or run `sentry cli defaults telemetry off`.
Expand Down
4 changes: 4 additions & 0 deletions design/sentry-cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ a terminal, with as little setup as the current project allows.
`SENTRY_CLI_NO_TELEMETRY=1` or `DO_NOT_TRACK=1` turns it off.
- `-f` means `--fresh` in every command that can bypass the response cache,
and a cached result's footer names the flag that refreshes that command.
- A command's hint (the line under its output, such as a pagination or
cache-age notice) goes to stdout only when stdout is a terminal. When
stdout is piped, the hint goes to stderr, so a script that captures
stdout gets only the result. `--json` output never has a hint.
- Live output is its own command, such as `sentry log tail`, not a mode flag
on a list or view command. List and view commands return once.
- The library SDK returns an `AsyncIterable` for exactly the invocations that
Expand Down
1 change: 1 addition & 0 deletions packages/cli/AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,7 @@ export const myCommand = buildCommand({

**Key rules:**
- Functions are `async *func()` generators — yield `new CommandOutput(data)`, return `{ hint }`.
- The wrapper writes the hint below the output when stdout is a TTY, and to stderr when stdout is piped, so `$(sentry …)` captures only the data ([#2531](https://github.com/getsentry/toolkit/issues/2531)). Never put data a script needs in the hint. A renderer's `finalize(hint)` gets no hint when piped and must still close its output (table footer).
- `output.human` receives the same data object that gets serialized to JSON — no divergent-data paths.
- The wrapper auto-injects `--json` and `--fields` flags. Do NOT add your own `json` flag.
- A `jsonTransform` replaces the default `--fields` filtering, so it must apply its `fields` argument (`filterFieldsIfSet` from `lib/formatters/json.ts`, or per item for list envelopes). The `sentry-cli/json-transform-uses-fields` lint rule rejects one that never reads it ([#1808](https://github.com/getsentry/toolkit/issues/1808)). Drop a transform that only returns its input; the default path already filters.
Expand Down
32 changes: 25 additions & 7 deletions packages/cli/src/lib/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -686,9 +686,16 @@ export function buildCommand<
/**
* Write post-generator output: either the renderer's `finalize()` result
* or the default `writeFooter(hint)`. Suppressed in JSON mode.
*
* The hint is for a person at a terminal. When stdout is not a TTY, a
* script is reading it (`$(sentry release propose-version)`), so the hint
* goes to stderr and stdout keeps only the command's output. The renderer
* still finalizes on stdout, so a streaming table gets its footer.
* getsentry/toolkit#2531
*/
function writeFinalization(
stdout: Writer,
stderr: Writer,
hint: string | undefined,
json: unknown,
// oxlint-disable-next-line typescript/no-explicit-any -- Renderer type mirrors erased OutputConfig<T>
Expand All @@ -697,15 +704,17 @@ export function buildCommand<
if (json) {
return;
}
const stdoutHint = hint && stdout.isTTY ? hint : undefined;
if (renderer?.finalize) {
const text = renderer.finalize(hint);
const text = renderer.finalize(stdoutHint);
if (text) {
stdout.write(text);
}
return;
} else if (stdoutHint) {
writeFooter(stdout, stdoutHint);
}
if (hint) {
writeFooter(stdout, hint);
if (hint && !stdoutHint) {
writeFooter(stderr, hint);
}
}

Expand Down Expand Up @@ -839,7 +848,10 @@ export function buildCommand<
),
);

const stdout = (this as unknown as { stdout: Writer }).stdout;
const { stdout, stderr } = this as unknown as {
stdout: Writer;
stderr: Writer;
};

// Reset per-invocation state
pendingClear = false;
Expand Down Expand Up @@ -934,14 +946,20 @@ export function buildCommand<
? appendCacheHint(applyTipsPreference(returned.hint), refreshFlag)
: undefined;
await withTracing("render", "cli.command.render", () => {
writeFinalization(stdout, finalHint, cleanFlags["json"], renderer);
writeFinalization(
stdout,
stderr,
finalHint,
cleanFlags["json"],
renderer,
);
});
} catch (err) {
// Finalize before error handling to close streaming state
// (e.g., table footer). No hint since the generator didn't
// complete. Only in human mode — JSON must not be corrupted.
if (!cleanFlags["json"]) {
writeFinalization(stdout, undefined, false, renderer);
writeFinalization(stdout, stderr, undefined, false, renderer);
}

// If a positional arg was "help" and the command failed with a
Expand Down
12 changes: 8 additions & 4 deletions packages/cli/src/lib/formatters/output.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ export type HumanRenderer<T> = {
* the wrapper writes the returned string directly.
*
* When absent, the wrapper falls back to `writeFooter(hint)`.
*
* When stdout is not a TTY, the wrapper passes no hint and writes the
* hint to stderr itself, so finalize must still close any open output.
*/
finalize?: (hint?: string) => string;
};
Expand Down Expand Up @@ -321,7 +324,8 @@ export type CommandReturn = {
* When the renderer has a `finalize()` method, the hint is passed
* to it — the renderer decides how to render it alongside any
* cleanup output (e.g., table footer). Otherwise the wrapper writes
* it via `writeFooter()`.
* it via `writeFooter()`. When stdout is not a TTY, the hint goes to
* stderr instead, so piped stdout holds only the command's output.
*/
hint?: string | undefined;
};
Expand Down Expand Up @@ -528,9 +532,9 @@ export function formatFooter(text: string): string {
}

/**
* Write a formatted footer hint to stdout.
* Write a formatted footer hint to stdout, or to stderr when stdout is piped.
* Adds empty line separator and applies muted styling.
*/
export function writeFooter(stdout: Writer, text: string): void {
stdout.write(formatFooter(text));
export function writeFooter(out: Writer, text: string): void {
out.write(formatFooter(text));
}
2 changes: 1 addition & 1 deletion packages/cli/test/commands/agent-conversation/list.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ function createMockContext() {
const stderrWrite = vi.fn<Writer["write"]>(() => true);
return {
context: {
stdout: { write: stdoutWrite },
stdout: { write: stdoutWrite, isTTY: true },
stderr: { write: stderrWrite },
cwd: "/tmp",
commandPrefix: ["sentry", "agent-conversation", "list"],
Expand Down
1 change: 1 addition & 0 deletions packages/cli/test/commands/alert/issues/list.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ type ListFunc = (
function createContext() {
const stdout = {
output: "",
isTTY: true,
write(s: string) {
stdout.output += s;
},
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/test/commands/attribute/list.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ function createMockContext() {
const stderrWrite = vi.fn((_chunk: string) => true);
return {
context: {
stdout: { write: stdoutWrite },
stdout: { write: stdoutWrite, isTTY: true },
stderr: { write: stderrWrite },
cwd: "/tmp",
commandPrefix: ["sentry", "attribute", "list"],
Expand Down
1 change: 1 addition & 0 deletions packages/cli/test/commands/auth/status.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ function createContext() {
write: vi.fn((s: string) => {
stdoutChunks.push(s);
}),
isTTY: true,
},
stderr: {
write: vi.fn((_s: string) => {
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/test/commands/auth/switch.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ function createContext() {
const chunks: string[] = [];
return {
context: {
stdout: { write: vi.fn((s: string) => chunks.push(s)) },
stdout: { write: vi.fn((s: string) => chunks.push(s)), isTTY: true },
stderr: { write: vi.fn<Writer["write"]>(() => true) },
cwd: "/tmp",
},
Expand Down
3 changes: 2 additions & 1 deletion packages/cli/test/commands/build/upload.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ function createContext() {
);
return true;
},
isTTY: true,
},
stderr: { write: () => true },
cwd: tmpDir,
Expand Down Expand Up @@ -74,7 +75,7 @@ async function runViaApp(
cwd: tmpDir,
homeDir: tmpDir,
configDir: tmpDir,
stdout: { write: () => true },
stdout: { write: () => true, isTTY: true },
stderr: {
write: (data: string | Uint8Array) => {
stderr +=
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/test/commands/dashboard/list.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ function createMockContext(cwd = "/tmp") {
const stderrWrite = vi.fn<Writer["write"]>(() => true);
return {
context: {
stdout: { write: stdoutWrite },
stdout: { write: stdoutWrite, isTTY: true },
stderr: { write: stderrWrite },
cwd,
},
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/test/commands/dashboard/revisions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ function createMockContext(cwd = "/tmp") {
const stderrWrite = vi.fn<Writer["write"]>(() => true);
return {
context: {
stdout: { write: stdoutWrite },
stdout: { write: stdoutWrite, isTTY: true },
stderr: { write: stderrWrite },
cwd,
},
Expand Down
1 change: 1 addition & 0 deletions packages/cli/test/commands/debug-files/upload.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ async function runUpload(args: string[]): Promise<{
typeof data === "string" ? data : new TextDecoder().decode(data);
return true;
},
isTTY: true,
},
stderr: {
write(data: string | Uint8Array) {
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/test/commands/event/analytics.func.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ function createMockContext() {
const stdoutWrite = vi.fn<Writer["write"]>(() => true);
return {
context: {
stdout: { write: stdoutWrite },
stdout: { write: stdoutWrite, isTTY: true },
stderr: { write: vi.fn<Writer["write"]>(() => true) },
cwd: "/tmp",
},
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/test/commands/event/list.func.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ describe("event list command func()", () => {
const stdoutWrite = vi.fn<Writer["write"]>(() => true);
return {
context: {
stdout: { write: stdoutWrite },
stdout: { write: stdoutWrite, isTTY: true },
stderr: { write: vi.fn<Writer["write"]>(() => true) },
cwd: "/tmp",
},
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/test/commands/event/view.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1144,7 +1144,7 @@ describe("viewCommand.func", () => {
const stdoutWrite = vi.fn<Writer["write"]>(() => true);
return {
context: {
stdout: { write: stdoutWrite },
stdout: { write: stdoutWrite, isTTY: true },
stderr: { write: vi.fn<Writer["write"]>(() => true) },
cwd: "/tmp",
},
Expand Down
1 change: 1 addition & 0 deletions packages/cli/test/commands/explore.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ function createContext() {
write: vi.fn((s: string) => {
stdoutChunks.push(s);
}),
isTTY: true,
},
stderr: {
write: vi.fn((s: string | Uint8Array) => {
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/test/commands/feedback/list.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ function createMockContext() {
const stdoutWrite = vi.fn<Writer["write"]>(() => true);
return {
context: {
stdout: { write: stdoutWrite },
stdout: { write: stdoutWrite, isTTY: true },
stderr: { write: vi.fn<Writer["write"]>(() => true) },
cwd: "/tmp",
commandPrefix: ["sentry", "feedback", "list"],
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/test/commands/games/leaderboard.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ async function run(
): Promise<string> {
const chunks: string[] = [];
const context = {
stdout: { write: (s: string) => chunks.push(s) },
stdout: { write: (s: string) => chunks.push(s), isTTY: true },
stderr: { write: () => true },
stdin: { isTTY: false },
};
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/test/commands/issue/events.func.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ describe("eventsCommand.func()", () => {
const stdoutWrite = vi.fn<Writer["write"]>(() => true);
return {
context: {
stdout: { write: stdoutWrite },
stdout: { write: stdoutWrite, isTTY: true },
stderr: { write: vi.fn<Writer["write"]>(() => true) },
cwd: "/tmp",
},
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/test/commands/issue/explain.func.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ function createMockContext() {
const stdoutWrite = vi.fn((_chunk: string) => true);
return {
context: {
stdout: { write: stdoutWrite },
stdout: { write: stdoutWrite, isTTY: true },
stderr: { write: vi.fn<Writer["write"]>(() => true) },
cwd: "/tmp",
},
Expand Down
4 changes: 2 additions & 2 deletions packages/cli/test/commands/issue/list.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -978,7 +978,7 @@ describe("issue list: org-all mode (cursor pagination)", () => {
const stderrWrite = vi.fn<Writer["write"]>(() => true);
return {
context: {
stdout: { write: stdoutWrite },
stdout: { write: stdoutWrite, isTTY: true },
stderr: { write: stderrWrite },
cwd: "/tmp",
},
Expand Down Expand Up @@ -1551,7 +1551,7 @@ describe("issue list: collapse parameter optimization", () => {
const stderrWrite = vi.fn<Writer["write"]>(() => true);
return {
context: {
stdout: { write: stdoutWrite },
stdout: { write: stdoutWrite, isTTY: true },
stderr: { write: stderrWrite },
cwd: "/tmp",
},
Expand Down
1 change: 1 addition & 0 deletions packages/cli/test/commands/issue/tags.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ function captureContext() {
stdout.push(String(value));
return true;
},
isTTY: true,
};
const stderrWriter = {
write(value: string | Uint8Array) {
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/test/commands/issue/unlink.func.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ function createMockContext() {
const stdoutWrite = vi.fn((_chunk: string) => true);
return {
context: {
stdout: { write: stdoutWrite },
stdout: { write: stdoutWrite, isTTY: true },
stderr: { write: vi.fn((_chunk: string) => true) },
cwd: "/tmp/example-project",
},
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/test/commands/log/list.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ function createMockContext() {
const stderrWrite = vi.fn<Writer["write"]>(() => true);
return {
context: {
stdout: { write: stdoutWrite },
stdout: { write: stdoutWrite, isTTY: true },
stderr: { write: stderrWrite },
cwd: "/tmp",
process: {},
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/test/commands/log/view.func.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ function createMockContext() {
const stdoutWrite = vi.fn<Writer["write"]>(() => true);
return {
context: {
stdout: { write: stdoutWrite },
stdout: { write: stdoutWrite, isTTY: true },
stderr: { write: vi.fn<Writer["write"]>(() => true) },
cwd: "/tmp",
},
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/test/commands/org/list.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ function createContext() {
const chunks: string[] = [];
return {
context: {
stdout: { write: vi.fn((s: string) => chunks.push(s)) },
stdout: { write: vi.fn((s: string) => chunks.push(s)), isTTY: true },
stderr: { write: vi.fn<Writer["write"]>(() => true) },
cwd: "/tmp",
},
Expand Down
1 change: 1 addition & 0 deletions packages/cli/test/commands/project/list.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -589,6 +589,7 @@ describe("project list: comma-separated project slugs", () => {
function createCommandContext() {
const stdout = {
output: "",
isTTY: true,
write(value: string) {
stdout.output += value;
return true;
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/test/commands/project/view.func.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ function createMockContext() {
const stdoutWrite = vi.fn<Writer["write"]>(() => true);
return {
context: {
stdout: { write: stdoutWrite },
stdout: { write: stdoutWrite, isTTY: true },
stderr: { write: vi.fn<Writer["write"]>(() => true) },
cwd: "/tmp",
commandPrefix: ["sentry", "project", "view"],
Expand Down
Loading
Loading