Worktrunk is a CLI for Git worktree management, designed for parallel AI agent workflows
fix(shell): skip the shell config scan when the install offer can't prompt (#4522)
Maximilian Roos54b6de1b5204
diff --git a/src/output/shell_integration.rs b/src/output/shell_integration.rsindex e38c1c1f4..6c4570cc4 100644--- a/src/output/shell_integration.rs+++ b/src/output/shell_integration.rs@@ -512,6 +512,14 @@ pub fn prompt_shell_integration(return Ok(false);}+ // Can't or shouldn't prompt - show install hint. Check this before the+ // scan below: only the prompt uses its result, and it spawns `pwsh` and+ // `nu` to find their config files.+ if config.skip_shell_integration_prompt || !is_tty || skip_prompt {+ print_shell_integration_hint(repo);+ return Ok(false);+ }+// Scan ALL shells (same as `wt config shell install`)// Only includes shells where config files already existlet mut scan = scan_shell_configs(None, true, binary_name)@@ -523,12 +531,6 @@ pub fn prompt_shell_integration(return Ok(false);}- // Can't or shouldn't prompt - show install hint- if config.skip_shell_integration_prompt || !is_tty || skip_prompt {- print_shell_integration_hint(repo);- return Ok(false);- }-scan.completion_results = preview_shell_completions(&scan.configured, binary_name).map_err(|e| anyhow::anyhow!("Failed to preview shell completions: {e}"))?;diff --git a/tests/integration_tests/shell_integration_prompt.rs b/tests/integration_tests/shell_integration_prompt.rsindex 985994469..7a6a53a62 100644--- a/tests/integration_tests/shell_integration_prompt.rs+++ b/tests/integration_tests/shell_integration_prompt.rs@@ -344,45 +344,37 @@ fn test_switch_stops_after_cancelled_powershell_query(assert!(!stderr.contains("Cannot change directory"), "{stderr}");}-/// With a current shell other than PowerShell, the first profile query is the-/// install offer's all-shell scan. Cancelling it stops the switch before the-/// `--execute` program runs; a query that merely fails (a plain exit 130)-/// falls back and the program runs.+/// A switch that can't prompt — here stdin isn't a terminal — prints the+/// install hint without scanning every shell's config, so it spawns neither+/// the `pwsh` nor the `nu` probe that scan runs.#[cfg(unix)]#[rstest]-#[case::sigint("kill -INT $$", 130, false)]-#[case::sigterm("kill -TERM $$", 143, false)]-#[case::ordinary_failure("exit 130", 0, true)]-fn test_switch_offer_after_powershell_query(- mut repo: TestRepo,- #[case] pwsh_body: &str,- #[case] exit_code: i32,- #[case] executed: bool,-) {- use std::os::unix::fs::PermissionsExt;+fn test_switch_hint_skips_shell_config_scan(mut repo: TestRepo) {+ use crate::common::mock_commands::{MockConfig, MockResponse, mock_calls};use std::os::unix::process::CommandExt;repo.setup_mock_ci_tools_unauthenticated();- let pwsh = repo.mock_bin_path().unwrap().join("pwsh");- fs::write(&pwsh, format!("#!/bin/sh\n{pwsh_body}\n")).unwrap();- fs::set_permissions(&pwsh, fs::Permissions::from_mode(0o755)).unwrap();+ let mock_bin = repo.mock_bin_path().unwrap();+ for tool in ["pwsh", "nu"] {+ MockConfig::new(tool)+ .command("_default", MockResponse::output("\n"))+ .write(mock_bin);+ }- let marker = repo.root_path().join("executed");+ let call_log = tempfile::TempDir::new().unwrap();let mut cmd = repo.wt_command();cmd.env("WORKTRUNK_TEST_PARENT_SHELL", "zsh")+ .env("WORKTRUNK_TEST_MOCK_CALL_LOG_DIR", call_log.path()).arg0("wt")- .args(["switch", "--create", "feature", "--execute", "touch", "--"])- .arg(&marker)+ .args(["switch", "--create", "feature"]).stdin(std::process::Stdio::null());let output = cmd.output().unwrap();let stderr = String::from_utf8_lossy(&output.stderr);- assert_eq!(- crate::common::shell_exit_code(&output.status),- Some(exit_code),- "{stderr}"- );- assert_eq!(marker.exists(), executed, "{stderr}");+ assert!(output.status.success(), "{stderr}");+ assert!(stderr.contains("wt config shell install"), "{stderr}");+ assert_eq!(mock_calls(call_log.path(), "pwsh"), Vec::<String>::new());+ assert_eq!(mock_calls(call_log.path(), "nu"), Vec::<String>::new());}/// An ordinary shell scan failure — an rc path that can't be read as a file —@@ -553,6 +545,58 @@ mod pty_tests {});}+ /// On a terminal, the install offer scans every shell's config, which asks+ /// `pwsh` for its profile. Cancelling that query stops the switch, failing+ /// and with no offer; a query that merely fails (a plain exit 130) falls+ /// back, and the offer appears. The PTY harness reports a signal death as+ /// exit 1, so the cases differ by success rather than by exit code.+ #[rstest]+ #[case::sigint("kill -INT $$", false)]+ #[case::sigterm("kill -TERM $$", false)]+ #[case::ordinary_failure("exit 130", true)]+ fn test_offer_stops_on_cancelled_powershell_query(+ repo: TestRepo,+ #[case] pwsh_body: &str,+ #[case] offered: bool,+ ) {+ use std::os::unix::fs::PermissionsExt;++ let temp_home = TempDir::new().unwrap();+ fs::write(temp_home.path().join(".bashrc"), "# empty bashrc\n").unwrap();+ let bin_dir = temp_home.path().join("bin");+ fs::create_dir_all(&bin_dir).unwrap();+ let pwsh = bin_dir.join("pwsh");+ fs::write(&pwsh, format!("#!/bin/sh\n{pwsh_body}\n")).unwrap();+ fs::set_permissions(&pwsh, fs::Permissions::from_mode(0o755)).unwrap();++ let mut env_vars = repo.test_env_vars();+ env_vars.push(("SHELL".to_string(), "/bin/bash".to_string()));+ env_vars.push((+ "PATH".to_string(),+ crate::common::setup_minimal_path_with_git(&bin_dir),+ ));++ let cmd = build_pty_command(+ wt_bin().to_str().unwrap(),+ &["switch", "--create", "feature"],+ repo.root_path(),+ &env_vars,+ Some(temp_home.path()),+ );+ let (output, exit_code) = if offered {+ exec_cmd_in_pty_prompted(cmd, &["n\n"], "[y/N")+ } else {+ exec_cmd_in_pty(cmd, "")+ };++ assert_eq!(exit_code == 0, offered, "exit {exit_code}: {output}");+ assert_eq!(+ output.contains("Install shell integration"),+ offered,+ "{output}"+ );+ }+/// Test: Not installed, user accepts → install and show success#[rstest]fn test_user_accepts_prompt(repo: TestRepo) {