Stop identifying Apple Terminal from TERM_SESSION_ID (#53438)

## Why

`TERM_SESSION_ID` is not specific to Apple Terminal. Treating it as an Apple Terminal identifier can mask Windows Terminal detection and the `TERM` fallback.

## What changed

Remove the `TERM_SESSION_ID` detection heuristic. Apple Terminal remains detectable through `TERM_PROGRAM=Apple_Terminal`.

## Testing

Update tests to cover Windows Terminal detection and the `TERM` fallback when `TERM_SESSION_ID` is present, and Apple Terminal detection with its version and user-agent token.

GitOrigin-RevId: 27db11cb7bcca8f94888f9e21a6cdfcf31561b1b
This commit is contained in:
Eric Traut
2026-10-12 17:52:12 +00:00
committed by copyberry
parent 6f47fcb0be
commit 7c1b64c5df
2 changed files with 14 additions and 33 deletions
+2 -9
View File
@@ -248,7 +248,8 @@ pub fn terminal_info() -> TerminalInfo {
/// - `TERM_PROGRAM` (plus `TERM_PROGRAM_VERSION`) drives the detected terminal name, /// - `TERM_PROGRAM` (plus `TERM_PROGRAM_VERSION`) drives the detected terminal name,
/// except when it identifies tmux rather than the underlying terminal. /// except when it identifies tmux rather than the underlying terminal.
/// This means `TERM_PROGRAM` can mask later probes (for example `WT_SESSION`). /// This means `TERM_PROGRAM` can mask later probes (for example `WT_SESSION`).
/// - Next, terminal-specific variables (WEZTERM, iTerm2, Apple Terminal, kitty, etc.) are checked. /// - Next, terminal-specific variables (WEZTERM, iTerm2, kitty, etc.) are checked.
/// `TERM_SESSION_ID` is not terminal-specific and cannot identify Apple Terminal.
/// - Finally, `TERM` is used as the capability fallback. /// - Finally, `TERM` is used as the capability fallback.
fn detect_terminal_info_from_env(env: &dyn Environment) -> TerminalInfo { fn detect_terminal_info_from_env(env: &dyn Environment) -> TerminalInfo {
let multiplexer = detect_multiplexer(env); let multiplexer = detect_multiplexer(env);
@@ -274,14 +275,6 @@ fn detect_terminal_info_from_env(env: &dyn Environment) -> TerminalInfo {
return TerminalInfo::from_name(TerminalName::Iterm2, /*version*/ None, multiplexer); return TerminalInfo::from_name(TerminalName::Iterm2, /*version*/ None, multiplexer);
} }
if env.has("TERM_SESSION_ID") {
return TerminalInfo::from_name(
TerminalName::AppleTerminal,
/*version*/ None,
multiplexer,
);
}
if env.has("KITTY_WINDOW_ID") if env.has("KITTY_WINDOW_ID")
|| env || env
.var("TERM") .var("TERM")
@@ -153,14 +153,17 @@ fn detects_iterm2() {
#[test] #[test]
fn detects_apple_terminal() { fn detects_apple_terminal() {
let env = FakeEnvironment::new().with_var("TERM_PROGRAM", "Apple_Terminal"); let env = FakeEnvironment::new()
.with_var("TERM_PROGRAM", "Apple_Terminal")
.with_var("TERM_PROGRAM_VERSION", "455")
.with_var("TERM_SESSION_ID", "A1B2C3");
let terminal = detect_terminal_info_from_env(&env); let terminal = detect_terminal_info_from_env(&env);
assert_eq!( assert_eq!(
terminal, terminal,
terminal_info( terminal_info(
TerminalName::AppleTerminal, TerminalName::AppleTerminal,
Some("Apple_Terminal"), Some("Apple_Terminal"),
/*version*/ None, Some("455"),
/*term*/ None, /*term*/ None,
/*multiplexer*/ None, /*multiplexer*/ None,
), ),
@@ -168,28 +171,9 @@ fn detects_apple_terminal() {
); );
assert_eq!( assert_eq!(
terminal.user_agent_token(), terminal.user_agent_token(),
"Apple_Terminal", "Apple_Terminal/455",
"apple_term_program_user_agent" "apple_term_program_user_agent"
); );
let env = FakeEnvironment::new().with_var("TERM_SESSION_ID", "A1B2C3");
let terminal = detect_terminal_info_from_env(&env);
assert_eq!(
terminal,
terminal_info(
TerminalName::AppleTerminal,
/*term_program*/ None,
/*version*/ None,
/*term*/ None,
/*multiplexer*/ None
),
"apple_term_session_id_info"
);
assert_eq!(
terminal.user_agent_token(),
"Apple_Terminal",
"apple_term_session_id_user_agent"
);
} }
#[test] #[test]
@@ -735,7 +719,9 @@ fn detects_vte() {
#[test] #[test]
fn detects_windows_terminal() { fn detects_windows_terminal() {
let env = FakeEnvironment::new().with_var("WT_SESSION", "1"); let env = FakeEnvironment::new()
.with_var("WT_SESSION", "1")
.with_var("TERM_SESSION_ID", "A1B2C3");
let terminal = detect_terminal_info_from_env(&env); let terminal = detect_terminal_info_from_env(&env);
assert_eq!( assert_eq!(
terminal, terminal,
@@ -778,7 +764,9 @@ fn detects_windows_terminal() {
#[test] #[test]
fn detects_term_fallbacks() { fn detects_term_fallbacks() {
let env = FakeEnvironment::new().with_var("TERM", "xterm-256color"); let env = FakeEnvironment::new()
.with_var("TERM", "xterm-256color")
.with_var("TERM_SESSION_ID", "A1B2C3");
let terminal = detect_terminal_info_from_env(&env); let terminal = detect_terminal_info_from_env(&env);
assert_eq!( assert_eq!(
terminal, terminal,