From cbe55ddffeeba29942afeced623211844714432f Mon Sep 17 00:00:00 2001 From: YrFnS Date: Fri, 4 Dec 2026 12:19:18 +0300 Subject: [PATCH] Fix OSC 11 background color query leaking after --version/--help Skip the terminal OSC color query for non-interactive early-exit invocations (--version/-v/--help/-h) and when NO_COLOR is set, so the terminal's reply is never left in the shell input buffer. --- cli/src/index.tsx | 17 ++++++++++++++++- cli/src/types/env.ts | 1 + cli/src/utils/env.ts | 1 + cli/src/utils/terminal-color-detection.ts | 21 +++++++++++++++++++++ 4 files changed, 39 insertions(+), 1 deletion(-) diff --git a/cli/src/index.tsx b/cli/src/index.tsx index 67df78ca3a..d315ab6b8e 100644 --- a/cli/src/index.tsx +++ b/cli/src/index.tsx @@ -325,8 +325,23 @@ async function main(): Promise { // sequence to the tty ('CON' on win32) and toggles stdin raw mode for a // reply that cannot come. That branch resolves the same env/fallback // evidence the query path fell back to, without touching stdin. + // Skip the OSC query for non-interactive early-exit invocations and when + // color is disabled: those print and exit before any terminal reply can be + // read, which would leak the OSC 11 response into the shell's input buffer. + const oscNonInteractive = + Boolean(process.env.NO_COLOR) || + (Array.isArray(process.argv) && + process.argv.some( + (arg) => + arg === '--version' || + arg === '-v' || + arg === '--help' || + arg === '-h', + )) const oscQuerySupported = - process.stdin.isTTY === true && process.platform !== 'win32' + !oscNonInteractive && + process.stdin.isTTY === true && + process.platform !== 'win32' const oscThemePromise: Promise<'dark' | 'light' | null> = oscQuerySupported ? detectTerminalTheme() : Promise.resolve(detectOscEvidenceTheme()) diff --git a/cli/src/types/env.ts b/cli/src/types/env.ts index 7a8439bde2..3a336b18c3 100644 --- a/cli/src/types/env.ts +++ b/cli/src/types/env.ts @@ -51,6 +51,7 @@ export type CliEnv = BaseEnv & { ZED_TERM?: string ZED_SHELL?: string COLORTERM?: string + NO_COLOR?: string // VS Code family detection VSCODE_THEME_KIND?: string diff --git a/cli/src/utils/env.ts b/cli/src/utils/env.ts index bbc2c5d655..ca25b11856 100644 --- a/cli/src/utils/env.ts +++ b/cli/src/utils/env.ts @@ -51,6 +51,7 @@ export const getCliEnv = (): CliEnv => ({ ZED_TERM: process.env.ZED_TERM, ZED_SHELL: process.env.ZED_SHELL, COLORTERM: process.env.COLORTERM, + NO_COLOR: process.env.NO_COLOR, // VS Code family detection VSCODE_THEME_KIND: process.env.VSCODE_THEME_KIND, diff --git a/cli/src/utils/terminal-color-detection.ts b/cli/src/utils/terminal-color-detection.ts index 6e2d4c9b97..463ab47645 100644 --- a/cli/src/utils/terminal-color-detection.ts +++ b/cli/src/utils/terminal-color-detection.ts @@ -26,6 +26,27 @@ const GLOBAL_OSC_TIMEOUT_MS = 2000 // Global timeout for entire detection proces export function terminalSupportsOSC( env: CliEnv = getCliEnv(), ): boolean { + // Color is explicitly disabled: never query the terminal (and never risk an + // OSC reply leaking into the shell on early exit). + if (env.NO_COLOR || process.env.NO_COLOR) { + return false + } + + // Non-interactive early-exit invocations (version/help) print and exit + // before any terminal reply can be read, so an OSC query would leak its + // response into the shell's input buffer. Skip the query entirely. + if ( + Array.isArray(process.argv) && + process.argv.some((arg) => + arg === '--version' || + arg === '-v' || + arg === '--help' || + arg === '-h', + ) + ) { + return false + } + const term = env.TERM || '' const termProgram = env.TERM_PROGRAM || ''