diff --git a/apps/cli-docs/src/content/docs/migrating-from-v3.md b/apps/cli-docs/src/content/docs/migrating-from-v3.md index 1e7f8d046..8eb2200d2 100644 --- a/apps/cli-docs/src/content/docs/migrating-from-v3.md +++ b/apps/cli-docs/src/content/docs/migrating-from-v3.md @@ -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`. diff --git a/apps/cli-docs/src/fragments/configuration.md b/apps/cli-docs/src/fragments/configuration.md index 32e38c89f..3c99d703b 100644 --- a/apps/cli-docs/src/fragments/configuration.md +++ b/apps/cli-docs/src/fragments/configuration.md @@ -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`. diff --git a/design/sentry-cli.md b/design/sentry-cli.md index aefd9c637..dbb78be56 100644 --- a/design/sentry-cli.md +++ b/design/sentry-cli.md @@ -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 diff --git a/packages/cli/AGENTS.md b/packages/cli/AGENTS.md index 7d3be335d..f956c50a4 100644 --- a/packages/cli/AGENTS.md +++ b/packages/cli/AGENTS.md @@ -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. diff --git a/packages/cli/src/lib/command.ts b/packages/cli/src/lib/command.ts index 0833fe996..63060f009 100644 --- a/packages/cli/src/lib/command.ts +++ b/packages/cli/src/lib/command.ts @@ -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 @@ -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); } } @@ -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; @@ -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 diff --git a/packages/cli/src/lib/formatters/output.ts b/packages/cli/src/lib/formatters/output.ts index 8b8751115..58f01699d 100644 --- a/packages/cli/src/lib/formatters/output.ts +++ b/packages/cli/src/lib/formatters/output.ts @@ -56,6 +56,9 @@ export type HumanRenderer = { * 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; }; @@ -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; }; @@ -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)); } diff --git a/packages/cli/test/commands/agent-conversation/list.test.ts b/packages/cli/test/commands/agent-conversation/list.test.ts index c76fb4c6b..3ac24f14e 100644 --- a/packages/cli/test/commands/agent-conversation/list.test.ts +++ b/packages/cli/test/commands/agent-conversation/list.test.ts @@ -82,7 +82,7 @@ function createMockContext() { const stderrWrite = vi.fn(() => true); return { context: { - stdout: { write: stdoutWrite }, + stdout: { write: stdoutWrite, isTTY: true }, stderr: { write: stderrWrite }, cwd: "/tmp", commandPrefix: ["sentry", "agent-conversation", "list"], diff --git a/packages/cli/test/commands/alert/issues/list.test.ts b/packages/cli/test/commands/alert/issues/list.test.ts index 6c25c1987..99c8e1467 100644 --- a/packages/cli/test/commands/alert/issues/list.test.ts +++ b/packages/cli/test/commands/alert/issues/list.test.ts @@ -36,6 +36,7 @@ type ListFunc = ( function createContext() { const stdout = { output: "", + isTTY: true, write(s: string) { stdout.output += s; }, diff --git a/packages/cli/test/commands/attribute/list.test.ts b/packages/cli/test/commands/attribute/list.test.ts index a0750b92a..d7752c4ea 100644 --- a/packages/cli/test/commands/attribute/list.test.ts +++ b/packages/cli/test/commands/attribute/list.test.ts @@ -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"], diff --git a/packages/cli/test/commands/auth/status.test.ts b/packages/cli/test/commands/auth/status.test.ts index a839f6d37..cbdcbb70e 100644 --- a/packages/cli/test/commands/auth/status.test.ts +++ b/packages/cli/test/commands/auth/status.test.ts @@ -89,6 +89,7 @@ function createContext() { write: vi.fn((s: string) => { stdoutChunks.push(s); }), + isTTY: true, }, stderr: { write: vi.fn((_s: string) => { diff --git a/packages/cli/test/commands/auth/switch.test.ts b/packages/cli/test/commands/auth/switch.test.ts index 3be44860d..46795cd97 100644 --- a/packages/cli/test/commands/auth/switch.test.ts +++ b/packages/cli/test/commands/auth/switch.test.ts @@ -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(() => true) }, cwd: "/tmp", }, diff --git a/packages/cli/test/commands/build/upload.test.ts b/packages/cli/test/commands/build/upload.test.ts index e8f8c5c60..f6d797d25 100644 --- a/packages/cli/test/commands/build/upload.test.ts +++ b/packages/cli/test/commands/build/upload.test.ts @@ -39,6 +39,7 @@ function createContext() { ); return true; }, + isTTY: true, }, stderr: { write: () => true }, cwd: tmpDir, @@ -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 += diff --git a/packages/cli/test/commands/dashboard/list.test.ts b/packages/cli/test/commands/dashboard/list.test.ts index 7f83a5c7c..c04cc2f4a 100644 --- a/packages/cli/test/commands/dashboard/list.test.ts +++ b/packages/cli/test/commands/dashboard/list.test.ts @@ -82,7 +82,7 @@ function createMockContext(cwd = "/tmp") { const stderrWrite = vi.fn(() => true); return { context: { - stdout: { write: stdoutWrite }, + stdout: { write: stdoutWrite, isTTY: true }, stderr: { write: stderrWrite }, cwd, }, diff --git a/packages/cli/test/commands/dashboard/revisions.test.ts b/packages/cli/test/commands/dashboard/revisions.test.ts index 331f7e57e..08d460822 100644 --- a/packages/cli/test/commands/dashboard/revisions.test.ts +++ b/packages/cli/test/commands/dashboard/revisions.test.ts @@ -36,7 +36,7 @@ function createMockContext(cwd = "/tmp") { const stderrWrite = vi.fn(() => true); return { context: { - stdout: { write: stdoutWrite }, + stdout: { write: stdoutWrite, isTTY: true }, stderr: { write: stderrWrite }, cwd, }, diff --git a/packages/cli/test/commands/debug-files/upload.test.ts b/packages/cli/test/commands/debug-files/upload.test.ts index c732739b5..c623d9e27 100644 --- a/packages/cli/test/commands/debug-files/upload.test.ts +++ b/packages/cli/test/commands/debug-files/upload.test.ts @@ -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) { diff --git a/packages/cli/test/commands/event/analytics.func.test.ts b/packages/cli/test/commands/event/analytics.func.test.ts index 7962ae825..11a56edf3 100644 --- a/packages/cli/test/commands/event/analytics.func.test.ts +++ b/packages/cli/test/commands/event/analytics.func.test.ts @@ -21,7 +21,7 @@ function createMockContext() { const stdoutWrite = vi.fn(() => true); return { context: { - stdout: { write: stdoutWrite }, + stdout: { write: stdoutWrite, isTTY: true }, stderr: { write: vi.fn(() => true) }, cwd: "/tmp", }, diff --git a/packages/cli/test/commands/event/list.func.test.ts b/packages/cli/test/commands/event/list.func.test.ts index 377c2aac9..ba3b35715 100644 --- a/packages/cli/test/commands/event/list.func.test.ts +++ b/packages/cli/test/commands/event/list.func.test.ts @@ -132,7 +132,7 @@ describe("event list command func()", () => { const stdoutWrite = vi.fn(() => true); return { context: { - stdout: { write: stdoutWrite }, + stdout: { write: stdoutWrite, isTTY: true }, stderr: { write: vi.fn(() => true) }, cwd: "/tmp", }, diff --git a/packages/cli/test/commands/event/view.test.ts b/packages/cli/test/commands/event/view.test.ts index b9ff4bdcf..e7c031ca7 100644 --- a/packages/cli/test/commands/event/view.test.ts +++ b/packages/cli/test/commands/event/view.test.ts @@ -1144,7 +1144,7 @@ describe("viewCommand.func", () => { const stdoutWrite = vi.fn(() => true); return { context: { - stdout: { write: stdoutWrite }, + stdout: { write: stdoutWrite, isTTY: true }, stderr: { write: vi.fn(() => true) }, cwd: "/tmp", }, diff --git a/packages/cli/test/commands/explore.test.ts b/packages/cli/test/commands/explore.test.ts index 912632177..51ae218d5 100644 --- a/packages/cli/test/commands/explore.test.ts +++ b/packages/cli/test/commands/explore.test.ts @@ -80,6 +80,7 @@ function createContext() { write: vi.fn((s: string) => { stdoutChunks.push(s); }), + isTTY: true, }, stderr: { write: vi.fn((s: string | Uint8Array) => { diff --git a/packages/cli/test/commands/feedback/list.test.ts b/packages/cli/test/commands/feedback/list.test.ts index ce99ff1f4..9e9537500 100644 --- a/packages/cli/test/commands/feedback/list.test.ts +++ b/packages/cli/test/commands/feedback/list.test.ts @@ -73,7 +73,7 @@ function createMockContext() { const stdoutWrite = vi.fn(() => true); return { context: { - stdout: { write: stdoutWrite }, + stdout: { write: stdoutWrite, isTTY: true }, stderr: { write: vi.fn(() => true) }, cwd: "/tmp", commandPrefix: ["sentry", "feedback", "list"], diff --git a/packages/cli/test/commands/games/leaderboard.test.ts b/packages/cli/test/commands/games/leaderboard.test.ts index 0d75aa872..bb88d7387 100644 --- a/packages/cli/test/commands/games/leaderboard.test.ts +++ b/packages/cli/test/commands/games/leaderboard.test.ts @@ -20,7 +20,7 @@ async function run( ): Promise { 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 }, }; diff --git a/packages/cli/test/commands/issue/events.func.test.ts b/packages/cli/test/commands/issue/events.func.test.ts index 587dcfa72..e7589b210 100644 --- a/packages/cli/test/commands/issue/events.func.test.ts +++ b/packages/cli/test/commands/issue/events.func.test.ts @@ -133,7 +133,7 @@ describe("eventsCommand.func()", () => { const stdoutWrite = vi.fn(() => true); return { context: { - stdout: { write: stdoutWrite }, + stdout: { write: stdoutWrite, isTTY: true }, stderr: { write: vi.fn(() => true) }, cwd: "/tmp", }, diff --git a/packages/cli/test/commands/issue/explain.func.test.ts b/packages/cli/test/commands/issue/explain.func.test.ts index f406f36d8..b949daca2 100644 --- a/packages/cli/test/commands/issue/explain.func.test.ts +++ b/packages/cli/test/commands/issue/explain.func.test.ts @@ -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(() => true) }, cwd: "/tmp", }, diff --git a/packages/cli/test/commands/issue/list.test.ts b/packages/cli/test/commands/issue/list.test.ts index cbbd5282c..ec56bf4d6 100644 --- a/packages/cli/test/commands/issue/list.test.ts +++ b/packages/cli/test/commands/issue/list.test.ts @@ -978,7 +978,7 @@ describe("issue list: org-all mode (cursor pagination)", () => { const stderrWrite = vi.fn(() => true); return { context: { - stdout: { write: stdoutWrite }, + stdout: { write: stdoutWrite, isTTY: true }, stderr: { write: stderrWrite }, cwd: "/tmp", }, @@ -1551,7 +1551,7 @@ describe("issue list: collapse parameter optimization", () => { const stderrWrite = vi.fn(() => true); return { context: { - stdout: { write: stdoutWrite }, + stdout: { write: stdoutWrite, isTTY: true }, stderr: { write: stderrWrite }, cwd: "/tmp", }, diff --git a/packages/cli/test/commands/issue/tags.test.ts b/packages/cli/test/commands/issue/tags.test.ts index be7d6a7c5..d5511bd06 100644 --- a/packages/cli/test/commands/issue/tags.test.ts +++ b/packages/cli/test/commands/issue/tags.test.ts @@ -114,6 +114,7 @@ function captureContext() { stdout.push(String(value)); return true; }, + isTTY: true, }; const stderrWriter = { write(value: string | Uint8Array) { diff --git a/packages/cli/test/commands/issue/unlink.func.test.ts b/packages/cli/test/commands/issue/unlink.func.test.ts index 0750c1211..b99968fdd 100644 --- a/packages/cli/test/commands/issue/unlink.func.test.ts +++ b/packages/cli/test/commands/issue/unlink.func.test.ts @@ -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", }, diff --git a/packages/cli/test/commands/log/list.test.ts b/packages/cli/test/commands/log/list.test.ts index 606f81f60..56dbb7768 100644 --- a/packages/cli/test/commands/log/list.test.ts +++ b/packages/cli/test/commands/log/list.test.ts @@ -97,7 +97,7 @@ function createMockContext() { const stderrWrite = vi.fn(() => true); return { context: { - stdout: { write: stdoutWrite }, + stdout: { write: stdoutWrite, isTTY: true }, stderr: { write: stderrWrite }, cwd: "/tmp", process: {}, diff --git a/packages/cli/test/commands/log/view.func.test.ts b/packages/cli/test/commands/log/view.func.test.ts index e02a5fc4d..cd13f0bef 100644 --- a/packages/cli/test/commands/log/view.func.test.ts +++ b/packages/cli/test/commands/log/view.func.test.ts @@ -82,7 +82,7 @@ function createMockContext() { const stdoutWrite = vi.fn(() => true); return { context: { - stdout: { write: stdoutWrite }, + stdout: { write: stdoutWrite, isTTY: true }, stderr: { write: vi.fn(() => true) }, cwd: "/tmp", }, diff --git a/packages/cli/test/commands/org/list.test.ts b/packages/cli/test/commands/org/list.test.ts index 17dd97184..867456c00 100644 --- a/packages/cli/test/commands/org/list.test.ts +++ b/packages/cli/test/commands/org/list.test.ts @@ -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(() => true) }, cwd: "/tmp", }, diff --git a/packages/cli/test/commands/project/list.test.ts b/packages/cli/test/commands/project/list.test.ts index 4a3f745f0..d6ee49a48 100644 --- a/packages/cli/test/commands/project/list.test.ts +++ b/packages/cli/test/commands/project/list.test.ts @@ -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; diff --git a/packages/cli/test/commands/project/view.func.test.ts b/packages/cli/test/commands/project/view.func.test.ts index 573d987e1..d0974a0c7 100644 --- a/packages/cli/test/commands/project/view.func.test.ts +++ b/packages/cli/test/commands/project/view.func.test.ts @@ -56,7 +56,7 @@ function createMockContext() { const stdoutWrite = vi.fn(() => true); return { context: { - stdout: { write: stdoutWrite }, + stdout: { write: stdoutWrite, isTTY: true }, stderr: { write: vi.fn(() => true) }, cwd: "/tmp", commandPrefix: ["sentry", "project", "view"], diff --git a/packages/cli/test/commands/release/propose-version.test.ts b/packages/cli/test/commands/release/propose-version.test.ts index 7306879ed..616f15df6 100644 --- a/packages/cli/test/commands/release/propose-version.test.ts +++ b/packages/cli/test/commands/release/propose-version.test.ts @@ -19,12 +19,16 @@ import type { Writer } from "../../../src/types/index.js"; useTestConfigDir("release-propose-version-"); -function createMockContext(cwd = "/tmp", env: Record = {}) { +function createMockContext( + cwd = "/tmp", + env: Record = {}, + isTTY = false, +) { const stdoutWrite = vi.fn(() => true); const stderrWrite = vi.fn(() => true); return { context: { - stdout: { write: stdoutWrite }, + stdout: { write: stdoutWrite, isTTY }, stderr: { write: stderrWrite }, cwd, env, @@ -72,6 +76,38 @@ describe("release propose-version", () => { expect(output).toContain("abc123def456789012345678901234567890abcd"); }); + test("piped stdout holds only the version; the hint goes to stderr", async () => { + getHeadCommitSpy.mockReturnValue("abc123"); + + const { context, stdoutWrite, stderrWrite } = createMockContext(); + const func = await loadCommand(proposeVersionCommand); + await func.call(context, { json: false }); + + // `$(sentry release propose-version)` must capture the version alone + // (getsentry/toolkit#2531). + const stdout = stdoutWrite.mock.calls.map((c) => c[0]).join(""); + expect(stdout).toBe("abc123\n"); + const stderr = stderrWrite.mock.calls.map((c) => c[0]).join(""); + expect(stderr).toContain("Detected from git HEAD"); + }); + + test("a terminal shows the hint below the version", async () => { + getHeadCommitSpy.mockReturnValue("abc123"); + + const { context, stdoutWrite, stderrWrite } = createMockContext( + "/tmp", + {}, + true, + ); + const func = await loadCommand(proposeVersionCommand); + await func.call(context, { json: false }); + + const stdout = stdoutWrite.mock.calls.map((c) => c[0]).join(""); + expect(stdout).toContain("abc123"); + expect(stdout).toContain("Detected from git HEAD"); + expect(stderrWrite).not.toHaveBeenCalled(); + }); + test("passes cwd to getHeadCommit", async () => { getHeadCommitSpy.mockReturnValue("deadbeef"); diff --git a/packages/cli/test/commands/replay/download.test.ts b/packages/cli/test/commands/replay/download.test.ts index ecc382100..541f7d676 100644 --- a/packages/cli/test/commands/replay/download.test.ts +++ b/packages/cli/test/commands/replay/download.test.ts @@ -83,7 +83,7 @@ function createContext() { }; return { context: { - stdout: { write: collect(writes) }, + stdout: { write: collect(writes), isTTY: true }, stderr: { write: collect(errors) }, cwd: tmpDir, env: {} as NodeJS.ProcessEnv, diff --git a/packages/cli/test/commands/replay/list.test.ts b/packages/cli/test/commands/replay/list.test.ts index 39c6f4c9f..c10ab1749 100644 --- a/packages/cli/test/commands/replay/list.test.ts +++ b/packages/cli/test/commands/replay/list.test.ts @@ -101,7 +101,7 @@ describe("listCommand.func", () => { const stdoutWrite = vi.fn(() => true); return { context: { - stdout: { write: stdoutWrite }, + stdout: { write: stdoutWrite, isTTY: true }, stderr: { write: vi.fn(() => true) }, cwd: "/tmp", commandPrefix: ["sentry", "replay", "list"], diff --git a/packages/cli/test/commands/repo/list.test.ts b/packages/cli/test/commands/repo/list.test.ts index c09c119c8..c4e60709b 100644 --- a/packages/cli/test/commands/repo/list.test.ts +++ b/packages/cli/test/commands/repo/list.test.ts @@ -76,7 +76,7 @@ function createMockContext(cwd = "/tmp") { const stderrWrite = vi.fn(() => true); return { context: { - stdout: { write: stdoutWrite }, + stdout: { write: stdoutWrite, isTTY: true }, stderr: { write: stderrWrite }, cwd, }, diff --git a/packages/cli/test/commands/span/list.test.ts b/packages/cli/test/commands/span/list.test.ts index d876d01c9..92c2f6f49 100644 --- a/packages/cli/test/commands/span/list.test.ts +++ b/packages/cli/test/commands/span/list.test.ts @@ -215,6 +215,7 @@ describe("listCommand.func (trace mode)", () => { write: vi.fn((s: string) => { stdoutChunks.push(s); }), + isTTY: true, }, stderr: { write: vi.fn((_s: string) => { @@ -675,6 +676,7 @@ describe("listCommand.func (project mode)", () => { write: vi.fn((s: string) => { stdoutChunks.push(s); }), + isTTY: true, }, stderr: { write: vi.fn((_s: string) => { diff --git a/packages/cli/test/commands/team/list.test.ts b/packages/cli/test/commands/team/list.test.ts index 6ce1a48b3..c20ff5c89 100644 --- a/packages/cli/test/commands/team/list.test.ts +++ b/packages/cli/test/commands/team/list.test.ts @@ -72,7 +72,7 @@ function createMockContext(cwd = "/tmp") { const stderrWrite = vi.fn(() => true); return { context: { - stdout: { write: stdoutWrite }, + stdout: { write: stdoutWrite, isTTY: true }, stderr: { write: stderrWrite }, cwd, }, diff --git a/packages/cli/test/commands/trace/list.test.ts b/packages/cli/test/commands/trace/list.test.ts index 35edeb3a0..82073b74b 100644 --- a/packages/cli/test/commands/trace/list.test.ts +++ b/packages/cli/test/commands/trace/list.test.ts @@ -300,7 +300,7 @@ describe("listCommand.func", () => { const stdoutWrite = vi.fn(() => true); return { context: { - stdout: { write: stdoutWrite }, + stdout: { write: stdoutWrite, isTTY: true }, stderr: { write: vi.fn(() => true) }, cwd: "/tmp", commandPrefix: ["sentry", "trace", "list"], diff --git a/packages/cli/test/commands/trace/logs.test.ts b/packages/cli/test/commands/trace/logs.test.ts index 2fe334c39..8f83088c9 100644 --- a/packages/cli/test/commands/trace/logs.test.ts +++ b/packages/cli/test/commands/trace/logs.test.ts @@ -111,7 +111,7 @@ function createMockContext() { const stdoutWrite = vi.fn(() => true); return { context: { - stdout: { write: stdoutWrite }, + stdout: { write: stdoutWrite, isTTY: true }, cwd: "/tmp", }, stdoutWrite, diff --git a/packages/cli/test/commands/trace/view.func.test.ts b/packages/cli/test/commands/trace/view.func.test.ts index 4aa9fc848..83f4f2f36 100644 --- a/packages/cli/test/commands/trace/view.func.test.ts +++ b/packages/cli/test/commands/trace/view.func.test.ts @@ -217,7 +217,7 @@ describe("viewCommand.func", () => { const stdoutWrite = vi.fn(() => true); return { context: { - stdout: { write: stdoutWrite }, + stdout: { write: stdoutWrite, isTTY: true }, stderr: { write: vi.fn(() => true) }, cwd: "/tmp", }, diff --git a/packages/cli/test/commands/trial/list.test.ts b/packages/cli/test/commands/trial/list.test.ts index e7226f117..5a634c823 100644 --- a/packages/cli/test/commands/trial/list.test.ts +++ b/packages/cli/test/commands/trial/list.test.ts @@ -52,7 +52,7 @@ function createMockContext(cwd = "/tmp") { const stderrWrite = vi.fn(() => true); return { context: { - stdout: { write: stdoutWrite }, + stdout: { write: stdoutWrite, isTTY: true }, stderr: { write: stderrWrite }, cwd, commandPrefix: ["sentry", "trial", "list"], diff --git a/packages/cli/test/e2e/log.test.ts b/packages/cli/test/e2e/log.test.ts index fe93ff048..096cf180e 100644 --- a/packages/cli/test/e2e/log.test.ts +++ b/packages/cli/test/e2e/log.test.ts @@ -193,7 +193,9 @@ describe("sentry log list (trace mode)", () => { ]); expect(result.exitCode).toBe(0); - expect(result.stdout).toMatch(/no logs found/i); + // Piped stdout holds no data; the hint goes to stderr (#2531) + expect(result.stdout).toBe(""); + expect(result.stderr).toMatch(/no logs found/i); }); test("help shows trace-id as positional argument", async () => { diff --git a/packages/cli/test/lib/command.test.ts b/packages/cli/test/lib/command.test.ts index 877e6efcb..b9da23c40 100644 --- a/packages/cli/test/lib/command.test.ts +++ b/packages/cli/test/lib/command.test.ts @@ -64,28 +64,33 @@ type TestContext = CommandContext & { * Use as: `const ctx = createTestContext();` * Then pass to `run(app, args, ctx as TestContext)`. * Access collected output via `ctx.output`. + * + * stdout is a terminal unless `isTTY` is false, which models piped output. */ -function createTestContext() { +function createTestContext(isTTY = true) { const stdoutCollected: string[] = []; const stderrCollected: string[] = []; const stdoutWriter = { + isTTY, write: (s: string) => { stdoutCollected.push(s); return true; }, }; + const stderrWriter = { + write: (s: string) => { + stderrCollected.push(s); + return true; + }, + }; return { process: { stdout: stdoutWriter, - stderr: { - write: (s: string) => { - stderrCollected.push(s); - return true; - }, - }, + stderr: stderrWriter, }, /** stdout on context — used by buildCommand's return-based output handler */ stdout: stdoutWriter, + stderr: stderrWriter, /** stdout output chunks only */ output: stdoutCollected, /** stderr output chunks only */ @@ -1564,6 +1569,71 @@ describe("buildCommand return-based output", () => { expect(JSON.parse(jsonRaw)).toEqual({ org: "sentry" }); }); + test("piped stdout gets only the data; the hint goes to stderr", async () => { + const command = buildCommand< + { json: boolean; fields?: string[] }, + [], + TestContext, + { version: string } + >({ + auth: false, + docs: { brief: "Test" }, + output: { human: (d: { version: string }) => d.version }, + parameters: {}, + async *func(this: TestContext) { + yield new CommandOutput({ version: "1.2.3" }); + return { hint: "Detected from git HEAD" }; + }, + }); + const app = buildApplication( + buildRouteMap({ routes: { test: command }, docs: { brief: "Test app" } }), + { name: "test" }, + ); + const ctx = createTestContext(false); + await run(app, ["test"], ctx as TestContext); + + expect(ctx.output.join("")).toBe("1.2.3\n"); + expect(ctx.errors.join("")).toContain("Detected from git HEAD"); + }); + + test("piped stdout still gets the renderer's finalize output, without the hint", async () => { + const finalizeHints: (string | undefined)[] = []; + const command = buildCommand< + { json: boolean; fields?: string[] }, + [], + TestContext, + { row: string } + >({ + auth: false, + docs: { brief: "Test" }, + output: { + human: () => ({ + render: (d: { row: string }) => d.row, + finalize: (hint?: string) => { + finalizeHints.push(hint); + return `-- end of table --\n${hint ?? ""}`; + }, + }), + }, + parameters: {}, + async *func(this: TestContext) { + yield new CommandOutput({ row: "a" }); + return { hint: "Showing 1 of 5. Next page: -c next" }; + }, + }); + const app = buildApplication( + buildRouteMap({ routes: { test: command }, docs: { brief: "Test app" } }), + { name: "test" }, + ); + const ctx = createTestContext(false); + await run(app, ["test"], ctx as TestContext); + + expect(finalizeHints).toEqual([undefined]); + expect(ctx.output.join("")).toContain("-- end of table --"); + expect(ctx.output.join("")).not.toContain("Next page"); + expect(ctx.errors.join("")).toContain("Showing 1 of 5. Next page: -c next"); + }); + test("OutputError renders data and exits with error code", async () => { const command = buildCommand< { json: boolean; fields?: string[] },