Skip to content

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

fix: check current shell for integration warnings, not all shells (#772)

Maximilian Roos607e373e188a
diff --git a/src/invocation.rs b/src/invocation.rs
index 82ebef115..1a7e53cdd 100644
--- a/src/invocation.rs
+++ b/src/invocation.rs
@@ -85,7 +85,7 @@ pub fn invocation_path() -> String {
/// - **`git wt` subcommand**: When invoked as `git wt`, git dispatches to `git-wt` binary
/// and sets argv\[0\] = `git-wt` (no path separator) → returns `false`. However, shell
/// integration configured for `wt` won't intercept `git wt` — they're different commands.
-/// This is handled separately by `is_integration_configured()` which checks for the
+/// This is handled separately by `Shell::is_shell_configured()` which checks for the
/// actual binary name (`git-wt`), not `wt`.
///
/// # Why Not Other Approaches?
diff --git a/src/output/shell_integration.rs b/src/output/shell_integration.rs
index 7a2a01a32..3fb0960c1 100644
--- a/src/output/shell_integration.rs
+++ b/src/output/shell_integration.rs
@@ -62,7 +62,7 @@ use color_print::cformat;
use worktrunk::config::UserConfig;
use worktrunk::path::format_path_for_display;
-use worktrunk::shell::{Shell, extract_filename_from_path};
+use worktrunk::shell::{Shell, current_shell, extract_filename_from_path};
use worktrunk::styling::hint_message;
/// Shell integration install hint message.
@@ -103,24 +103,27 @@ pub(crate) fn explicit_path_hint(branch: &str) -> String {
}
/// Check if we should show the explicit path hint.
-/// True when: explicit path invocation AND shell integration IS configured.
+/// True when: explicit path invocation AND current shell has integration configured.
pub(crate) fn should_show_explicit_path_hint() -> bool {
crate::was_invoked_with_explicit_path()
- && Shell::is_integration_configured(&crate::binary_name())
- .ok()
- .flatten()
- .is_some()
+ && current_shell()
+ .and_then(|shell| shell.is_shell_configured(&crate::binary_name()).ok())
+ .unwrap_or(false)
}
/// Compute the shell warning reason for display in messages.
///
/// Returns a reason string explaining why shell integration isn't working.
/// See the module documentation for the complete spec of warning messages.
+///
+/// Checks specifically if the CURRENT shell (from $SHELL) has integration configured,
+/// not just any shell. This prevents misleading "shell requires restart" messages
+/// when e.g. bash has integration but the user is running fish.
pub(crate) fn compute_shell_warning_reason() -> String {
- let is_configured = Shell::is_integration_configured(&crate::binary_name())
- .ok()
- .flatten()
- .is_some();
+ // Check if the CURRENT shell has integration configured, not just ANY shell
+ let is_configured = current_shell()
+ .and_then(|shell| shell.is_shell_configured(&crate::binary_name()).ok())
+ .unwrap_or(false);
let explicit_path = crate::was_invoked_with_explicit_path();
let invoked = crate::invocation_path();
let wraps = crate::binary_name();
diff --git a/src/shell/detection.rs b/src/shell/detection.rs
index 6525887ad..2561a1b8f 100644
--- a/src/shell/detection.rs
+++ b/src/shell/detection.rs
@@ -62,7 +62,7 @@ use super::paths::{home_dir_required, powershell_profile_paths};
/// # Usage
///
/// Used by:
-/// - `is_integration_configured()` - detect "configured but not restarted" state
+/// - `Shell::is_shell_configured()` - detect "configured but not restarted" state
/// - `uninstall` - identify lines to remove from shell config
/// - `wt config show` - display shell integration status
///
diff --git a/src/shell/mod.rs b/src/shell/mod.rs
index 959827960..44da014ea 100644
--- a/src/shell/mod.rs
+++ b/src/shell/mod.rs
@@ -96,22 +96,43 @@ impl Shell {
}
}
- /// Check if shell integration is configured for the given command name.
+ /// Check if this shell has integration configured.
///
- /// Returns the path to the first config file with integration if found.
- /// This helps detect the "configured but not restarted shell" state.
- ///
- /// The `cmd` parameter specifies the command name to look for (e.g., "wt" or "git-wt").
- /// This ensures we only consider integration "configured" if it uses the same binary
- /// we're running as - prevents confusion when users have multiple installs.
- pub fn is_integration_configured(
- cmd: &str,
- ) -> Result<Option<std::path::PathBuf>, std::io::Error> {
- let results = scan_for_detection_details(cmd)?;
- Ok(results
- .into_iter()
- .find(|r| !r.matched_lines.is_empty())
- .map(|r| r.path))
+ /// Used for accurate warning messages that need to know about the user's
+ /// current shell specifically (e.g., "shell requires restart" vs "not installed").
+ pub fn is_shell_configured(&self, cmd: &str) -> Result<bool, std::io::Error> {
+ let config_paths = self.config_paths(cmd)?;
+
+ // For fish, also check legacy conf.d location
+ let mut paths_to_check = config_paths;
+ if matches!(self, Shell::Fish)
+ && let Ok(legacy) = Shell::legacy_fish_conf_d_path(cmd)
+ {
+ paths_to_check.push(legacy);
+ }
+
+ for path in paths_to_check {
+ if !path.exists() {
+ continue;
+ }
+ if Self::file_has_integration(&path, cmd)? {
+ return Ok(true);
+ }
+ }
+ Ok(false)
+ }
+
+ /// Check if a file contains shell integration lines for the given command.
+ fn file_has_integration(path: &std::path::Path, cmd: &str) -> Result<bool, std::io::Error> {
+ use std::io::{BufRead, BufReader};
+
+ let file = std::fs::File::open(path)?;
+ for line in BufReader::new(file).lines() {
+ if is_shell_integration_line(&line?, cmd) {
+ return Ok(true);
+ }
+ }
+ Ok(false)
}
}
@@ -395,4 +416,33 @@ mod tests {
"{shell} config_line({prefix:?}) not detected:\n {line}"
);
}
+
+ #[test]
+ fn test_file_has_integration() {
+ use std::io::Write;
+
+ let temp_dir = tempfile::tempdir().unwrap();
+ let bashrc = temp_dir.path().join(".bashrc");
+
+ // Write a valid integration line
+ let mut file = std::fs::File::create(&bashrc).unwrap();
+ writeln!(
+ file,
+ r#"if command -v wt >/dev/null 2>&1; then eval "$(command wt config shell init bash)"; fi"#
+ )
+ .unwrap();
+
+ // Test file_has_integration directly
+ assert!(Shell::file_has_integration(&bashrc, "wt").unwrap());
+ assert!(!Shell::file_has_integration(&bashrc, "git-wt").unwrap());
+
+ // Test with non-matching content
+ let empty_file = temp_dir.path().join(".zshrc");
+ std::fs::write(&empty_file, "# just a comment\n").unwrap();
+ assert!(!Shell::file_has_integration(&empty_file, "wt").unwrap());
+ }
+
+ // Note: is_shell_configured() is not unit-tested because it requires
+ // mutating HOME env var (unsafe). It's tested indirectly via integration
+ // tests that exercise the shell integration warning paths.
}