Skip to content

Worktrunk is a CLI for Git worktree management, designed for parallel AI agent workflows

fix(config): keep `wt config show` from suspending on the zsh compinit probe (#3327)

Worktrunk Botadb417ede817
diff --git a/src/commands/config/show.rs b/src/commands/config/show.rs
index 7f4d9ae5b..08c347324 100644
--- a/src/commands/config/show.rs
+++ b/src/commands/config/show.rs
@@ -247,10 +247,29 @@ fn git_version() -> Option<String> {
.map(|s| s.to_string())
}
+/// Args for the interactive zsh compinit probe.
+///
+/// `+m` (disable job control) is load-bearing: without it the interactive
+/// `zsh -ic` grabs wt's controlling terminal and, if the timeout kills it
+/// first, leaves the pager to suspend with SIGTTOU. See `check_zsh_compinit_missing`
+/// and issue #3322.
+const ZSH_COMPINIT_PROBE_ARGS: [&str; 4] =
+ ["--no-globalrcs", "+m", "-ic", "(( $+functions[compdef] ))"];
+
/// Check if zsh has compinit enabled by spawning an interactive shell
///
/// Returns true if compinit is NOT enabled (i.e., user needs to add it).
/// Returns false if compinit is enabled or we can't determine (fail-safe: don't warn).
+///
+// TODO(zsh-compinit-probe-unify): this duplicates `shell::detect_zsh_compinit`
+// — both spawn an interactive `zsh -ic` to probe for the `compdef` function, and
+// #3322 had to add the `+m` job-control fix to both. They diverge on purpose, so
+// folding them into one parameterized probe isn't a mechanical rename: this one
+// passes `--no-globalrcs` (checks the USER's config so we only nudge when *they*
+// haven't enabled compinit) while `detect_zsh_compinit` intentionally sources
+// global rcs (so shell setup won't re-add what /etc/zshrc already enables); they
+// also differ in result type (bool vs Option<bool>) and runner (`Cmd` vs raw
+// `Command`). Unify behind one `probe_zsh_compdef(no_globalrcs: bool)` helper.
fn check_zsh_compinit_missing() -> bool {
// Allow tests to bypass this check since zsh subprocess behavior varies across CI envs
if std::env::var("WORKTRUNK_TEST_COMPINIT_CONFIGURED").is_ok() {
@@ -276,8 +295,14 @@ fn check_zsh_compinit_missing() -> bool {
// bypasses both the stdin=null default and the stderr suppression above)
// would otherwise hang `wt config show` indefinitely. On timeout run()
// returns Err, so the `else` below declines to warn rather than misreport.
+ // `+m` disables job control so this interactive probe doesn't grab wt's
+ // controlling terminal. An interactive zsh with job control on `tcsetpgrp`s
+ // to claim the terminal foreground; if the 2s timeout kills it before it
+ // restores that, wt is left in a background process group and the pager
+ // spawned moments later (config show always pages) raises SIGTTOU,
+ // suspending the command (`suspended (tty output)`). See issue #3322.
let Ok(output) = Cmd::new("zsh")
- .args(["--no-globalrcs", "-ic", "(( $+functions[compdef] ))"])
+ .args(ZSH_COMPINIT_PROBE_ARGS)
.env("ZSH_DISABLE_COMPFIX", "true")
.timeout(std::time::Duration::from_secs(2))
.run()
@@ -1550,6 +1575,25 @@ mod tests {
assert!(!is_newer_version("0.23.2", "invalid"));
}
+ /// Regression guard for #3322: the interactive zsh compinit probe must
+ /// disable job control (`+m`). Without it, the probe grabs wt's controlling
+ /// terminal; a timeout-kill then leaves wt in a background process group and
+ /// the `wt config show` pager suspends with SIGTTOU (`suspended (tty output)`).
+ #[test]
+ fn test_compinit_probe_disables_job_control() {
+ assert!(
+ ZSH_COMPINIT_PROBE_ARGS.contains(&"+m"),
+ "compinit probe must pass +m to disable job control (#3322); got {ZSH_COMPINIT_PROBE_ARGS:?}"
+ );
+ // `+m` must precede `-ic` so it's parsed as an option, not a command arg.
+ let m = ZSH_COMPINIT_PROBE_ARGS.iter().position(|a| *a == "+m");
+ let ic = ZSH_COMPINIT_PROBE_ARGS.iter().position(|a| *a == "-ic");
+ assert!(
+ m < ic,
+ "+m must come before -ic: {ZSH_COMPINIT_PROBE_ARGS:?}"
+ );
+ }
+
#[test]
fn test_format_version_status() {
// A version far above the current crate version is "newer".
diff --git a/src/shell/utils.rs b/src/shell/utils.rs
index 16d3361be..5ddf3305b 100644
--- a/src/shell/utils.rs
+++ b/src/shell/utils.rs
@@ -96,6 +96,13 @@ pub fn current_shell() -> Option<Shell> {
None
}
+/// Leading flags for the interactive zsh compinit probe.
+///
+/// `+m` (disable job control) is load-bearing — see the call site and issue
+/// #3322. It must precede `-ic` so zsh parses it as an option, not a command
+/// argument.
+const ZSH_PROBE_FLAGS: [&str; 2] = ["+m", "-ic"];
+
/// Detect if user's zsh has compinit enabled by probing for the compdef function.
///
/// Zsh's completion system (compinit) must be explicitly enabled - it's not on by default.
@@ -110,6 +117,11 @@ pub fn current_shell() -> Option<Shell> {
/// - `Some(true)` if compinit is enabled (compdef function exists)
/// - `Some(false)` if compinit is NOT enabled
/// - `None` if detection failed (zsh not installed, timeout, error)
+///
+// TODO(zsh-compinit-probe-unify): see `config::show::check_zsh_compinit_missing`
+// for the matching probe and why the two haven't been merged (intentional
+// `--no-globalrcs` divergence, different result types and runners). #3322 had to
+// apply the `+m` job-control fix in both places.
pub fn detect_zsh_compinit() -> Option<bool> {
// Allow tests to bypass this check since zsh subprocess behavior varies across CI envs
if std::env::var("WORKTRUNK_TEST_COMPINIT_CONFIGURED").is_ok() {
@@ -130,7 +142,13 @@ pub fn detect_zsh_compinit() -> Option<bool> {
tracing::debug!(command = %probe_cmd, "$ zsh -ic '{}' (probe)", probe_cmd);
let mut cmd = Command::new("zsh");
- cmd.arg("-ic")
+ // `+m` disables job control so the interactive probe doesn't grab wt's
+ // controlling terminal. An interactive zsh with job control on `tcsetpgrp`s
+ // to claim the terminal foreground; if the 2s timeout kills it before it
+ // restores that, wt is left in a background process group and the next
+ // terminal write raises SIGTTOU. See issue #3322. `+m` must precede `-ic`
+ // so it's parsed as an option rather than a command argument.
+ cmd.args(ZSH_PROBE_FLAGS)
.arg(probe_cmd)
.stdin(Stdio::null()) // Prevent compinit from prompting interactively
.stdout(Stdio::piped())
@@ -182,6 +200,19 @@ mod tests {
use super::*;
use rstest::rstest;
+ /// Regression guard for #3322: the interactive zsh compinit probe must
+ /// disable job control (`+m`) so it can't grab wt's controlling terminal —
+ /// otherwise a timeout-kill leaves the parent in a background process group
+ /// and the next terminal write raises SIGTTOU.
+ #[test]
+ fn test_zsh_probe_disables_job_control() {
+ assert_eq!(
+ ZSH_PROBE_FLAGS[0], "+m",
+ "job control must be disabled before -ic (#3322)"
+ );
+ assert!(ZSH_PROBE_FLAGS.contains(&"-ic"));
+ }
+
// ==========================================================================
// Path extraction tests (Issue #348)
// ==========================================================================