Worktrunk is a CLI for Git worktree management, designed for parallel AI agent workflows
fix(shell): harden nushell wrapper and improve diagnostics (#1059)
Maximilian Roos5a10dcc7888a
diff --git a/src/output/shell_integration.rs b/src/output/shell_integration.rsindex 34b41cdbb..cbb054502 100644--- a/src/output/shell_integration.rs+++ b/src/output/shell_integration.rs@@ -91,7 +91,7 @@ fn shell_integration_unsupported_shell(shell_path: &str) -> String {// e.g., "/bin/tcsh" -> "tcsh", "C:\...\tcsh.exe" -> "tcsh"let shell_name = extract_filename_from_path(shell_path).unwrap_or(shell_path);format!(- "Shell integration not yet supported for {shell_name} (supports bash, zsh, fish, PowerShell)"+ "Shell integration not yet supported for {shell_name} (supports bash, zsh, fish, nu, PowerShell)")}diff --git a/src/shell/detection.rs b/src/shell/detection.rsindex e51b82274..1375de3d0 100644--- a/src/shell/detection.rs+++ b/src/shell/detection.rs@@ -459,6 +459,9 @@ pub fn scan_for_detection_details(cmd: &str) -> Result<Vec<FileDetectionResult>,home.join(".config/fish/conf.d").join(format!("{cmd}.fish")),];+ // Add Nushell vendor autoload paths (check all candidate locations)+ config_files.extend(super::config_paths(super::Shell::Nushell, cmd).unwrap_or_default());+// Add PowerShell profilesconfig_files.extend(powershell_profile_paths(&home));@@ -935,10 +938,17 @@ mod tests {// ------------------------------------------------------------------------#[test]- fn test_nushell_pattern() {- // Nushell's config_line uses `save --force` which contains "wt config shell init"+ fn test_nushell_save_pattern() {+ // Nushell's config_line uses `save --force` (detected via "save" keyword)+ let line = "if (which wt | is-not-empty) { wt config shell init nu | save --force ($nu.default-config-dir | path join vendor/autoload/wt.nu) }";+ assert_detects(line, "wt", "nushell save pattern (actual config line)");+ }++ #[test]+ fn test_nushell_source_pattern() {+ // Alternative nushell pattern using source (for manual setups)let line = "wt config shell init nu | source";- assert_detects(line, "wt", "nushell pattern");+ assert_detects(line, "wt", "nushell source pattern");}// ------------------------------------------------------------------------diff --git a/src/shell/snapshots/worktrunk__shell__tests__init_nu.snap b/src/shell/snapshots/worktrunk__shell__tests__init_nu.snapindex 35838031f..e31c684ee 100644--- a/src/shell/snapshots/worktrunk__shell__tests__init_nu.snap+++ b/src/shell/snapshots/worktrunk__shell__tests__init_nu.snap@@ -27,11 +27,13 @@ def --env --wrapped wt [...args: string] {let directive_file = (mktemp --tmpdir)- # Run command with directive file - output streams in real-time- with-env { WORKTRUNK_DIRECTIVE_FILE: $directive_file } {- ^$worktrunk_bin ...$args+ # Run command with directive file - capture exit code immediately+ let exit_code = do {+ with-env { WORKTRUNK_DIRECTIVE_FILE: $directive_file } {+ ^$worktrunk_bin ...$args+ }+ $env.LAST_EXIT_CODE}- let exit_code = $env.LAST_EXIT_CODE# Process directive file line by lineif ($directive_file | path exists) and (open $directive_file --raw | str trim | is-not-empty) {@@ -47,7 +49,10 @@ def --env --wrapped wt [...args: string] {let target_dir = $directive | str substring 4..-2cd $target_dir} else if ($directive | is-not-empty) {- # Execute other commands via sh for proper shell expansion+ # Execute via sh for POSIX shell expansion (globs, pipes, $VAR).+ # --execute commands are user-provided shell expressions that expect+ # this. Env changes (export) won't persist in the nushell session,+ # but no worktrunk code emits export directives.^sh -c $directive}}diff --git a/templates/nushell.nu b/templates/nushell.nuindex 4c1f64f35..d1714b4fd 100644--- a/templates/nushell.nu+++ b/templates/nushell.nu@@ -23,11 +23,13 @@ def --env --wrapped {{ cmd }} [...args: string] {let directive_file = (mktemp --tmpdir)- # Run command with directive file - output streams in real-time- with-env { WORKTRUNK_DIRECTIVE_FILE: $directive_file } {- ^$worktrunk_bin ...$args+ # Run command with directive file - capture exit code immediately+ let exit_code = do {+ with-env { WORKTRUNK_DIRECTIVE_FILE: $directive_file } {+ ^$worktrunk_bin ...$args+ }+ $env.LAST_EXIT_CODE}- let exit_code = $env.LAST_EXIT_CODE# Process directive file line by lineif ($directive_file | path exists) and (open $directive_file --raw | str trim | is-not-empty) {@@ -43,7 +45,10 @@ def --env --wrapped {{ cmd }} [...args: string] {let target_dir = $directive | str substring 4..-2cd $target_dir} else if ($directive | is-not-empty) {- # Execute other commands via sh for proper shell expansion+ # Execute via sh for POSIX shell expansion (globs, pipes, $VAR).+ # --execute commands are user-provided shell expressions that expect+ # this. Env changes (export) won't persist in the nushell session,+ # but no worktrunk code emits export directives.^sh -c $directive}}diff --git a/tests/integration_tests/configure_shell.rs b/tests/integration_tests/configure_shell.rsindex 1ffa1a62d..13abd67a5 100644--- a/tests/integration_tests/configure_shell.rs+++ b/tests/integration_tests/configure_shell.rs@@ -1952,3 +1952,42 @@ fn test_nushell_auto_detection_creates_vendor_autoload(repo: TestRepo, temp_homecontent);}++/// Test that `wt config show` detects nushell integration after install.+///+/// Verifies that `scan_for_detection_details` includes nushell vendor autoload+/// paths in its scan, so nushell appears in diagnostic output.+#[rstest]+fn test_config_show_detects_nushell_integration(mut repo: TestRepo, temp_home: TempDir) {+ repo.setup_mock_ci_tools_unauthenticated();++ // Install nushell integration+ let mut install_cmd = wt_command();+ repo.configure_wt_cmd(&mut install_cmd);+ set_temp_home_env(&mut install_cmd, temp_home.path());+ install_cmd.env("SHELL", "/bin/nu");+ install_cmd+ .args(["config", "shell", "install", "nu", "--yes"])+ .current_dir(repo.root_path());+ let install_output = install_cmd.output().expect("Failed to execute install");+ assert!(+ install_output.status.success(),+ "Install should succeed:\nstderr: {}",+ String::from_utf8_lossy(&install_output.stderr)+ );++ // Run `wt config show` and verify nushell integration is detected+ let mut cmd = wt_command();+ repo.configure_wt_cmd(&mut cmd);+ repo.configure_mock_commands(&mut cmd);+ set_temp_home_env(&mut cmd, temp_home.path());+ cmd.env("SHELL", "/bin/nu");+ cmd.args(["config", "show"]).current_dir(repo.root_path());++ let output = cmd.output().expect("Failed to execute config show");+ let stderr = String::from_utf8_lossy(&output.stderr);+ assert!(+ stderr.contains("wt.nu") || stderr.contains("nushell"),+ "config show should detect nushell integration:\n{stderr}"+ );+}diff --git a/tests/integration_tests/shell_integration_prompt.rs b/tests/integration_tests/shell_integration_prompt.rsindex 49d74bb40..55a9caa15 100644--- a/tests/integration_tests/shell_integration_prompt.rs+++ b/tests/integration_tests/shell_integration_prompt.rs@@ -167,7 +167,7 @@ fn test_switch_unsupported_shell_shows_hint(repo: TestRepo) {"Should show unsupported shell message: {stderr}");assert!(- stderr.contains("bash, zsh, fish, PowerShell"),+ stderr.contains("bash, zsh, fish, nu, PowerShell"),"Should list supported shells: {stderr}");}