From e784c92e3c050dd76f3c6a82182139cc415e729a Mon Sep 17 00:00:00 2001 From: Josh Robson Chase Date: Fri, 6 Mar 2026 14:18:33 -0500 Subject: [PATCH] zed: Clear the FORCE_CLI_MODE environment variable after reading (#46475) This prevents child processes (e.g. shells) from inheriting it. This generally isn't an issue, since zed is the only thing that cares about it, but if you're trying to run zed *from* zed, you would first need to unset it manually. Release Notes: - N/A --- crates/cli/src/cli.rs | 3 +++ crates/zed/src/main.rs | 10 ++++++++-- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/crates/cli/src/cli.rs b/crates/cli/src/cli.rs index 8a2394372faf17281babf2cc9769648d64cd67be..1a3ce059b8116ac7438f3eb0330b47660cc863de 100644 --- a/crates/cli/src/cli.rs +++ b/crates/cli/src/cli.rs @@ -34,4 +34,7 @@ pub enum CliResponse { /// When Zed started not as an *.app but as a binary (e.g. local development), /// there's a possibility to tell it to behave "regularly". +/// +/// Note that in the main zed binary, this variable is unset after it's read for the first time, +/// therefore it should always be accessed through the `FORCE_CLI_MODE` static. pub const FORCE_CLI_MODE_ENV_VAR_NAME: &str = "ZED_FORCE_CLI_MODE"; diff --git a/crates/zed/src/main.rs b/crates/zed/src/main.rs index eccf6b51e01922590752a47589c1cdc1303df966..3bf3fd190f61ffead59d08d4da556468e2bb1fcf 100644 --- a/crates/zed/src/main.rs +++ b/crates/zed/src/main.rs @@ -48,7 +48,7 @@ use std::{ path::{Path, PathBuf}, process, rc::Rc, - sync::{Arc, OnceLock}, + sync::{Arc, LazyLock, OnceLock}, time::Instant, }; use theme::{ActiveTheme, GlobalTheme, ThemeRegistry}; @@ -1577,8 +1577,14 @@ fn init_paths() -> HashMap> { }) } +pub(crate) static FORCE_CLI_MODE: LazyLock = LazyLock::new(|| { + let env_var = std::env::var(FORCE_CLI_MODE_ENV_VAR_NAME).ok().is_some(); + unsafe { std::env::remove_var(FORCE_CLI_MODE_ENV_VAR_NAME) }; + env_var +}); + fn stdout_is_a_pty() -> bool { - std::env::var(FORCE_CLI_MODE_ENV_VAR_NAME).ok().is_none() && io::stdout().is_terminal() + !*FORCE_CLI_MODE && io::stdout().is_terminal() } #[derive(Parser, Debug)]