Skip to content

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

feat(config): detect PowerShell via PSModulePath and improve shell diagnostics (#987)

Maximilian Roos7ead04c3b2e7
diff --git a/src/commands/config/show.rs b/src/commands/config/show.rs
index b9265adb2..cfab75a78 100644
--- a/src/commands/config/show.rs
+++ b/src/commands/config/show.rs
@@ -554,8 +554,13 @@ fn render_shell_status(out: &mut String) -> anyhow::Result<()> {
}
// Show $SHELL to help diagnose rc file sourcing issues
- if let Ok(shell_env) = std::env::var("SHELL") {
+ let shell_env = std::env::var("SHELL").ok().filter(|s| !s.is_empty());
+ if let Some(shell_env) = &shell_env {
debug_lines.push(cformat!("$SHELL: <bold>{shell_env}</>"));
+ } else if let Some(detected) = worktrunk::shell::current_shell() {
+ debug_lines.push(cformat!(
+ "Detected shell: <bold>{detected}</> (via PSModulePath)"
+ ));
}
if is_git_subcommand {
@@ -690,6 +695,18 @@ fn render_shell_status(out: &mut String) -> anyhow::Result<()> {
)?;
}
}
+
+ // When configured but not active, show how to verify the wrapper loaded
+ if !shell_active {
+ let verify_cmd = match shell {
+ Shell::PowerShell => format!("Get-Command {cmd}"),
+ _ => format!("type {cmd}"),
+ };
+ let hint = hint_message(cformat!(
+ "To verify wrapper loaded: <bright-black>{verify_cmd}</>"
+ ));
+ writeln!(out, "{hint}")?;
+ }
}
ConfigAction::WouldAdd | ConfigAction::WouldCreate => {
// For fish, check if we have valid integration at the legacy conf.d location
diff --git a/src/output/shell_integration.rs b/src/output/shell_integration.rs
index b35e938f0..e8488c78f 100644
--- a/src/output/shell_integration.rs
+++ b/src/output/shell_integration.rs
@@ -123,9 +123,10 @@ pub(crate) fn should_show_explicit_path_hint() -> bool {
/// 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.
+/// Checks specifically if the CURRENT shell (detected via $SHELL or PSModulePath
+/// fallback) 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 {
// Check if the CURRENT shell has integration configured, not just ANY shell
let is_configured = current_shell()
@@ -373,7 +374,7 @@ pub fn prompt_shell_integration(
let is_tty = std::io::stdin().is_terminal() && std::io::stderr().is_terminal();
- // Check the current shell (from $SHELL)
+ // Check the current shell (via $SHELL or PSModulePath fallback)
// Only prompt if current shell is supported (so they benefit immediately)
let shell_env = std::env::var("SHELL").ok();
if current_shell().is_none() {
diff --git a/src/shell/utils.rs b/src/shell/utils.rs
index c98ffeab1..565e524d4 100644
--- a/src/shell/utils.rs
+++ b/src/shell/utils.rs
@@ -56,19 +56,36 @@ pub fn shell_from_name(shell_name: &str) -> Option<Shell> {
}
}
-/// Get the current shell from `$SHELL` environment variable.
+/// Detect the current shell from the environment.
///
-/// Returns `None` if `$SHELL` is not set or doesn't match a known shell.
-/// Handles versioned/prefixed binaries like `/nix/store/.../zsh-5.9` or `bash5`
-/// by checking if the name starts with a known shell.
+/// Uses two strategies:
+/// 1. `$SHELL` environment variable (Unix standard, also set by Git Bash on Windows)
+/// 2. `PSModulePath` environment variable (indicates PowerShell on all platforms)
+///
+/// Returns `None` if neither heuristic matches a known shell.
///
/// Works on both Unix and Windows:
/// - Unix: `/usr/bin/bash` -> Bash
/// - Windows Git Bash: `C:\Program Files\Git\usr\bin\bash.exe` -> Bash
+/// - Windows PowerShell: `PSModulePath` set -> PowerShell
pub fn current_shell() -> Option<Shell> {
- let shell_path = std::env::var("SHELL").ok()?;
- let shell_name = extract_filename_from_path(&shell_path)?;
- shell_from_name(shell_name)
+ // Primary: $SHELL (Unix standard, also set by Git Bash on Windows)
+ if let Ok(shell_path) = std::env::var("SHELL")
+ && let Some(name) = extract_filename_from_path(&shell_path)
+ {
+ return shell_from_name(name);
+ }
+
+ // Fallback: PSModulePath indicates PowerShell (set on all platforms when
+ // running inside PowerShell). On Windows this has some false positives
+ // (PSModulePath can be set system-wide), but for diagnostic purposes
+ // that's acceptable — a slightly less accurate message is better than
+ // "shell integration not installed" when it IS installed.
+ if std::env::var_os("PSModulePath").is_some() {
+ return Some(Shell::PowerShell);
+ }
+
+ None
}
/// Detect if user's zsh has compinit enabled by probing for the compdef function.
diff --git a/tests/common/mod.rs b/tests/common/mod.rs
index 0daafa244..2872e9c87 100644
--- a/tests/common/mod.rs
+++ b/tests/common/mod.rs
@@ -2683,9 +2683,18 @@ fn setup_snapshot_settings_for_paths_with_home(
"sh: $1: command not found",
);
- // Filter out PowerShell lines on Windows - these appear only on Windows
- // and would cause snapshot mismatches with Unix snapshots
- settings.add_filter(r"(?m)^.*[Pp]owershell.*\n", "");
+ // Filter out PowerShell lines that differ between Windows and Unix.
+ // On Windows, PowerShell profile paths use Documents\PowerShell\... while Unix uses
+ // ~/.config/powershell/..., and PowerShell scanning is auto-enabled on Windows.
+ // These targeted patterns strip platform-dependent output without affecting:
+ // - "Detected shell: powershell" diagnostics (no colon after powershell)
+ // - Clap help/error messages listing available shells
+ // ANSI codes can appear between "powershell" and ":" in styled output (e.g.,
+ // "\x1b[1mpowershell\x1b[22m:"), so we allow optional escape sequences in the match.
+ settings.add_filter(r"(?m)^.*[Pp]owershell(?:\x1b\[[0-9;]*m)*:.*\n", ""); // status: "○ powershell: ..."
+ settings.add_filter(r"(?m)^.*No .*powershell.* shell extension.*\n", ""); // uninstall hints
+ settings.add_filter(r"(?m)^.*shell init powershell.*\n", ""); // gutter config content
+ settings.add_filter(r"(?m)^.*for powershell .*\n", ""); // install success lines
// Normalize Windows executable extension in help output
// On Windows, clap shows "wt.exe" instead of "wt"
@@ -2788,9 +2797,11 @@ pub fn setup_home_snapshot_settings(temp_home: &TempDir) -> insta::Settings {
"[TEMP_HOME]",
);
settings.add_filter(r"\\", "/");
- // Filter out PowerShell lines on Windows - these appear only on Windows
- // and would cause snapshot mismatches with Unix snapshots
- settings.add_filter(r"(?m)^.*[Pp]owershell.*\n", "");
+ // Filter out PowerShell lines (see main filter in setup_snapshot_settings_impl for details)
+ settings.add_filter(r"(?m)^.*[Pp]owershell(?:\x1b\[[0-9;]*m)*:.*\n", "");
+ settings.add_filter(r"(?m)^.*No .*powershell.* shell extension.*\n", "");
+ settings.add_filter(r"(?m)^.*shell init powershell.*\n", "");
+ settings.add_filter(r"(?m)^.*for powershell .*\n", "");
// Normalize Windows executable extension in help output
settings.add_filter(r"wt\.exe", "wt");
diff --git a/tests/integration_tests/config_show.rs b/tests/integration_tests/config_show.rs
index a726a5c4e..542fbdc78 100644
--- a/tests/integration_tests/config_show.rs
+++ b/tests/integration_tests/config_show.rs
@@ -1463,6 +1463,64 @@ fn test_config_show_statusline_configured(mut repo: TestRepo, temp_home: TempDir
});
}
+/// When $SHELL is not set but PSModulePath is, config show should display
+/// "Detected shell: powershell" in the diagnostics and show the verification hint.
+#[rstest]
+fn test_config_show_powershell_detected_via_psmodulepath(mut repo: TestRepo, temp_home: TempDir) {
+ repo.setup_mock_ci_tools_unauthenticated();
+
+ // Create global config
+ let global_config_dir = temp_home.path().join(".config").join("worktrunk");
+ fs::create_dir_all(&global_config_dir).unwrap();
+ fs::write(global_config_dir.join("config.toml"), "").unwrap();
+
+ // Create .bashrc with wt integration
+ fs::write(
+ temp_home.path().join(".bashrc"),
+ r#"if command -v wt >/dev/null 2>&1; then eval "$(command wt config shell init bash)"; fi
+"#,
+ )
+ .unwrap();
+
+ // Create PowerShell profile with wt integration (covers Get-Command hint branch)
+ // Must use the canonical config line (what `wt config shell install` writes)
+ let ps_profile_dir = temp_home.path().join(".config").join("powershell");
+ fs::create_dir_all(&ps_profile_dir).unwrap();
+ fs::write(
+ ps_profile_dir.join("Microsoft.PowerShell_profile.ps1"),
+ "if (Get-Command wt -ErrorAction SilentlyContinue) { Invoke-Expression (& wt config shell init powershell | Out-String) }\n",
+ )
+ .unwrap();
+
+ let mut settings = setup_snapshot_settings_with_home(&repo, &temp_home);
+ // PowerShell config state is platform-dependent: the profile path differs between
+ // Windows (Documents\PowerShell\) and Unix (~/.config/powershell/). The broad
+ // PowerShell filter strips status lines, but the Get-Command hint and "To configure"
+ // hint also vary by platform (present only when profile is found). Filter them too.
+ settings.add_filter(r"(?m)^.*Get-Command.*\n", "");
+ settings.add_filter(r"(?m)^.*To configure, run.*\n", "");
+ // Collapse triple newlines that may result from stripping adjacent lines
+ settings.add_filter(r"\n\n\n", "\n\n");
+ settings.bind(|| {
+ let mut cmd = wt_command();
+ repo.configure_wt_cmd(&mut cmd);
+ repo.configure_mock_commands(&mut cmd);
+ cmd.arg("config").arg("show").current_dir(repo.root_path());
+ set_temp_home_env(&mut cmd, temp_home.path());
+ // Enable PowerShell scanning so the profile above is detected
+ cmd.env("WORKTRUNK_TEST_POWERSHELL_ENV", "1");
+ // Ensure SHELL is NOT set (already removed by configure_cli_command)
+ cmd.env_remove("SHELL");
+ // Set PSModulePath to trigger PowerShell detection fallback
+ cmd.env(
+ "PSModulePath",
+ "C:\\Users\\user\\Documents\\PowerShell\\Modules",
+ );
+
+ assert_cmd_snapshot!(cmd);
+ });
+}
+
/// Test that deprecated [commit-generation] section shows warning and creates migration file
#[rstest]
fn test_deprecated_commit_generation_section_shows_warning(repo: TestRepo, temp_home: TempDir) {
diff --git a/tests/integration_tests/init.rs b/tests/integration_tests/init.rs
index 10d092347..b9403a266 100644
--- a/tests/integration_tests/init.rs
+++ b/tests/integration_tests/init.rs
@@ -65,6 +65,8 @@ fn test_init_invalid_shell(repo: TestRepo) {
----- stdout -----
----- stderr -----
+ error: invalid value 'invalid-shell' for '<bash|fish|zsh|powershell>'
+ [possible values: bash, fish, zsh, powershell]
For more information, try '--help'.
");
diff --git a/tests/snapshots/integration__integration_tests__config_show__config_show_fish_with_completions.snap b/tests/snapshots/integration__integration_tests__config_show__config_show_fish_with_completions.snap
index 39d7e9830..4bc4adf4a 100644
--- a/tests/snapshots/integration__integration_tests__config_show__config_show_fish_with_completions.snap
+++ b/tests/snapshots/integration__integration_tests__config_show__config_show_fish_with_completions.snap
@@ -13,6 +13,7 @@ info:
GIT_COMMITTER_DATE: "2025-01-01T00:00:00Z"
GIT_CONFIG_GLOBAL: "[TEST_GIT_CONFIG]"
GIT_CONFIG_SYSTEM: /dev/null
+ GIT_EDITOR: ""
GIT_TERMINAL_PROMPT: "0"
HOME: "[TEST_HOME]"
LANG: C
@@ -50,6 +51,7 @@ exit_code: 0
○ fish: Already configured shell extension @ ~/.config/fish/functions/wt.fish:7
  command wt config shell init fish | source
○ fish: Already configured completions @ ~/.config/fish/completions/wt.fish
+↳ To verify wrapper loaded: type wt
○ bash: Skipped; ~/.bashrc not found
○ zsh: Skipped; ~/.zshrc not found
diff --git a/tests/snapshots/integration__integration_tests__config_show__config_show_fish_without_completions.snap b/tests/snapshots/integration__integration_tests__config_show__config_show_fish_without_completions.snap
index b61418d3d..55ee1858f 100644
--- a/tests/snapshots/integration__integration_tests__config_show__config_show_fish_without_completions.snap
+++ b/tests/snapshots/integration__integration_tests__config_show__config_show_fish_without_completions.snap
@@ -13,6 +13,7 @@ info:
GIT_COMMITTER_DATE: "2025-01-01T00:00:00Z"
GIT_CONFIG_GLOBAL: "[TEST_GIT_CONFIG]"
GIT_CONFIG_SYSTEM: /dev/null
+ GIT_EDITOR: ""
GIT_TERMINAL_PROMPT: "0"
HOME: "[TEST_HOME]"
LANG: C
@@ -50,6 +51,7 @@ exit_code: 0
○ fish: Already configured shell extension @ ~/.config/fish/functions/wt.fish:7
  command wt config shell init fish | source
↳ fish: Not configured completions
+↳ To verify wrapper loaded: type wt
○ bash: Skipped; ~/.bashrc not found
○ zsh: Skipped; ~/.zshrc not found
diff --git a/tests/snapshots/integration__integration_tests__config_show__config_show_partial_shell_config_shows_hint.snap b/tests/snapshots/integration__integration_tests__config_show__config_show_partial_shell_config_shows_hint.snap
index a97f97e2a..60e94620c 100644
--- a/tests/snapshots/integration__integration_tests__config_show__config_show_partial_shell_config_shows_hint.snap
+++ b/tests/snapshots/integration__integration_tests__config_show__config_show_partial_shell_config_shows_hint.snap
@@ -13,6 +13,7 @@ info:
GIT_COMMITTER_DATE: "2025-01-01T00:00:00Z"
GIT_CONFIG_GLOBAL: "[TEST_GIT_CONFIG]"
GIT_CONFIG_SYSTEM: /dev/null
+ GIT_EDITOR: ""
GIT_TERMINAL_PROMPT: "0"
HOME: "[TEST_HOME]"
LANG: C
@@ -51,6 +52,7 @@ exit_code: 0
↳ bash: Not configured shell extension & completions
○ zsh: Already configured shell extension & completions @ ~/.zshrc:2
  if command -v wt >/dev/null 2>&1; then eval "$(command wt config shell init zsh)"; fi
+↳ To verify wrapper loaded: type wt
○ fish: Skipped; ~/.config/fish/functions not found
↳ To configure, run wt config shell install
diff --git a/tests/snapshots/integration__integration_tests__config_show__config_show_powershell_detected_via_psmodulepath.snap b/tests/snapshots/integration__integration_tests__config_show__config_show_powershell_detected_via_psmodulepath.snap
new file mode 100644
index 000000000..0d78282bb
--- /dev/null
+++ b/tests/snapshots/integration__integration_tests__config_show__config_show_powershell_detected_via_psmodulepath.snap
@@ -0,0 +1,64 @@
+---
+source: tests/integration_tests/config_show.rs
+info:
+ program: wt
+ args:
+ - config
+ - show
+ env:
+ APPDATA: "[TEST_CONFIG_HOME]"
+ CLICOLOR_FORCE: "1"
+ COLUMNS: "500"
+ GIT_AUTHOR_DATE: "2025-01-01T00:00:00Z"
+ GIT_COMMITTER_DATE: "2025-01-01T00:00:00Z"
+ GIT_CONFIG_GLOBAL: "[TEST_GIT_CONFIG]"
+ GIT_CONFIG_SYSTEM: /dev/null
+ GIT_EDITOR: ""
+ GIT_TERMINAL_PROMPT: "0"
+ HOME: "[TEST_HOME]"
+ LANG: C
+ LC_ALL: C
+ MOCK_CONFIG_DIR: "[MOCK_CONFIG_DIR]"
+ PATH: "[PATH]"
+ PSModulePath: "C:\\Users\\user\\Documents\\PowerShell\\Modules"
+ RUST_LOG: warn
+ SHELL: ""
+ TERM: alacritty
+ USERPROFILE: "[TEST_HOME]"
+ WORKTRUNK_CONFIG_PATH: "[TEST_CONFIG]"
+ WORKTRUNK_TEST_CLAUDE_INSTALLED: "0"
+ WORKTRUNK_TEST_POWERSHELL_ENV: "1"
+ WORKTRUNK_TEST_SKIP_URL_HEALTH_CHECK: "1"
+ WT_TEST_DELAYED_STREAM_MS: "-1"
+ WT_TEST_EPOCH: "1735776000"
+ XDG_CONFIG_HOME: "[TEST_CONFIG_HOME]"
+---
+success: true
+exit_code: 0
+----- stdout -----
+
+----- stderr -----
+USER CONFIG ~/.config/worktrunk/config.toml
+↳ Empty file (using defaults)
+
+PROJECT CONFIG _REPO_/.config/wt.toml
+↳ Not found
+
+SHELL INTEGRATION
+▲ Shell integration not active
+  Invoked as: [PROJECT_ROOT]/target/debug/wt
+  Detected shell: powershell (via PSModulePath)
+
+○ bash: Already configured shell extension & completions @ ~/.bashrc:1
+  if command -v wt >/dev/null 2>&1; then eval "$(command wt config shell init bash)"; fi
+↳ To verify wrapper loaded: type wt
+○ zsh: Skipped; ~/.zshrc not found
+○ fish: Skipped; ~/.config/fish/functions not found
+
+CLAUDE CODE
+○ claude CLI not installed
+
+OTHER
+○ wt: [VERSION]
+○ git: [VERSION]
+○ Hyperlinks: inactive
diff --git a/tests/snapshots/integration__integration_tests__config_show__config_show_zsh_compinit_correct_order.snap b/tests/snapshots/integration__integration_tests__config_show__config_show_zsh_compinit_correct_order.snap
index c146499ca..0a510a438 100644
--- a/tests/snapshots/integration__integration_tests__config_show__config_show_zsh_compinit_correct_order.snap
+++ b/tests/snapshots/integration__integration_tests__config_show__config_show_zsh_compinit_correct_order.snap
@@ -13,6 +13,7 @@ info:
GIT_COMMITTER_DATE: "2025-01-01T00:00:00Z"
GIT_CONFIG_GLOBAL: "[TEST_GIT_CONFIG]"
GIT_CONFIG_SYSTEM: /dev/null
+ GIT_EDITOR: ""
GIT_TERMINAL_PROMPT: "0"
HOME: "[TEST_HOME]"
LANG: C
@@ -50,6 +51,7 @@ exit_code: 0
○ zsh: Already configured shell extension & completions @ ~/.zshrc:5
  if command -v wt >/dev/null 2>&1; then eval "$(command wt config shell init zsh)"; fi
+↳ To verify wrapper loaded: type wt
○ bash: Skipped; ~/.bashrc not found
○ fish: Skipped; ~/.config/fish/functions not found
diff --git a/tests/snapshots/integration__integration_tests__config_show__config_show_zsh_compinit_warning.snap b/tests/snapshots/integration__integration_tests__config_show__config_show_zsh_compinit_warning.snap
index c2e71d924..745efafd7 100644
--- a/tests/snapshots/integration__integration_tests__config_show__config_show_zsh_compinit_warning.snap
+++ b/tests/snapshots/integration__integration_tests__config_show__config_show_zsh_compinit_warning.snap
@@ -13,6 +13,7 @@ info:
GIT_COMMITTER_DATE: "2025-01-01T00:00:00Z"
GIT_CONFIG_GLOBAL: "[TEST_GIT_CONFIG]"
GIT_CONFIG_SYSTEM: /dev/null
+ GIT_EDITOR: ""
GIT_TERMINAL_PROMPT: "0"
HOME: "[TEST_HOME]"
LANG: C
@@ -52,6 +53,7 @@ exit_code: 0
  if command -v wt >/dev/null 2>&1; then eval "$(command wt config shell init zsh)"; fi
▲ Completions won't work; add to ~/.zshrc before the wt line:
  autoload -Uz compinit && compinit
+↳ To verify wrapper loaded: type wt
○ bash: Skipped; ~/.bashrc not found
○ fish: Skipped; ~/.config/fish/functions not found