Skip to content

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

fix(shell): keep the Nushell wrapper working when its stdout is redirected (#4525)

Maximilian Roos12d33b3e30d3
diff --git a/src/shell/snapshots/worktrunk__shell__tests__init_nu.snap b/src/shell/snapshots/worktrunk__shell__tests__init_nu.snap
index cf5c555f4..9e4fd70cc 100644
--- a/src/shell/snapshots/worktrunk__shell__tests__init_nu.snap
+++ b/src/shell/snapshots/worktrunk__shell__tests__init_nu.snap
@@ -104,7 +104,10 @@ export def --env --wrapped wt [...args] {
} else {
let cd_file = (mktemp --tmpdir)
let stdout_file = (mktemp --tmpdir)
- try {
+ # The return value comes from outside `try … finally`: under an output
+ # redirect (`o> file`), Nushell fails converting a `try … finally`
+ # that evaluates to nothing ("can't convert nothing to string").
+ let result = (try {
let wt_args = ($arg_text | take until {|arg| $arg == "--" })
# Before `-x`, `c`, `v`, and `y` are boolean flags clap may cluster with it.
let has_execute = ($wt_args | any {|arg| $arg == "--execute" or ($arg | str starts-with "--execute=") or ($arg =~ '^-[cvy]*x') })
@@ -130,15 +133,9 @@ export def --env --wrapped wt [...args] {
})
let output = if $has_execute { "" } else { open $stdout_file --raw }
-
- # Return captured stdout or reproduce the external failure without
- # a source trace. Nu itself also works on Windows, where sh is absent.
- if $exit_code != 0 {
- if ($output | is-not-empty) { print -n $output }
- ^$nu.current-exe --no-config-file --no-std-lib -c $"exit ($exit_code)"
- } else if ($output | is-not-empty) {
- $output
- }
+ # A failure's stdout prints now, so a failing directive can't drop it.
+ if $exit_code != 0 and ($output | is-not-empty) { print -n $output }
+ { exit_code: $exit_code, output: $output }
} finally {
try {
# cd file holds a raw path — no shell parsing needed
@@ -155,6 +152,14 @@ export def --env --wrapped wt [...args] {
try { ^rm -f $cd_file $stdout_file }
}
}
+ })
+
+ # Return captured stdout or reproduce the external failure without
+ # a source trace. Nu itself also works on Windows, where sh is absent.
+ if $result.exit_code != 0 {
+ ^$nu.current-exe --no-config-file --no-std-lib -c $"exit ($result.exit_code)"
+ } else if ($result.output | is-not-empty) {
+ $result.output
}
}
}
diff --git a/templates/nushell.nu b/templates/nushell.nu
index c0a70e916..e4dd0b34e 100644
--- a/templates/nushell.nu
+++ b/templates/nushell.nu
@@ -100,7 +100,10 @@ export def --env --wrapped {{ cmd }} [...args] {
} else {
let cd_file = (mktemp --tmpdir)
let stdout_file = (mktemp --tmpdir)
- try {
+ # The return value comes from outside `try … finally`: under an output
+ # redirect (`o> file`), Nushell fails converting a `try … finally`
+ # that evaluates to nothing ("can't convert nothing to string").
+ let result = (try {
let wt_args = ($arg_text | take until {|arg| $arg == "--" })
# Before `-x`, `c`, `v`, and `y` are boolean flags clap may cluster with it.
let has_execute = ($wt_args | any {|arg| $arg == "--execute" or ($arg | str starts-with "--execute=") or ($arg =~ '^-[cvy]*x') })
@@ -126,15 +129,9 @@ export def --env --wrapped {{ cmd }} [...args] {
})
let output = if $has_execute { "" } else { open $stdout_file --raw }
-
- # Return captured stdout or reproduce the external failure without
- # a source trace. Nu itself also works on Windows, where sh is absent.
- if $exit_code != 0 {
- if ($output | is-not-empty) { print -n $output }
- ^$nu.current-exe --no-config-file --no-std-lib -c $"exit ($exit_code)"
- } else if ($output | is-not-empty) {
- $output
- }
+ # A failure's stdout prints now, so a failing directive can't drop it.
+ if $exit_code != 0 and ($output | is-not-empty) { print -n $output }
+ { exit_code: $exit_code, output: $output }
} finally {
try {
# cd file holds a raw path — no shell parsing needed
@@ -151,6 +148,14 @@ export def --env --wrapped {{ cmd }} [...args] {
try { ^rm -f $cd_file $stdout_file }
}
}
+ })
+
+ # Return captured stdout or reproduce the external failure without
+ # a source trace. Nu itself also works on Windows, where sh is absent.
+ if $result.exit_code != 0 {
+ ^$nu.current-exe --no-config-file --no-std-lib -c $"exit ($result.exit_code)"
+ } else if ($result.output | is-not-empty) {
+ $result.output
}
}
}
diff --git a/tests/integration_tests/shell_wrapper.rs b/tests/integration_tests/shell_wrapper.rs
index d1189006d..a96b2f41f 100644
--- a/tests/integration_tests/shell_wrapper.rs
+++ b/tests/integration_tests/shell_wrapper.rs
@@ -1303,6 +1303,31 @@ mod unix_tests {
assert_eq!(fs::read_dir(tmp.path()).unwrap().count(), 0, "{combined}");
}
+ /// Redirecting the wrapper's stdout must not abort it when the command
+ /// prints nothing there. Under `o>`, Nushell fails to convert a
+ /// `try … finally` that evaluates to nothing, and the script stopped
+ /// after the directory change was lost.
+ #[rstest]
+ fn test_nu_wrapper_redirected_stdout_applies_cd(mut repo: TestRepo) {
+ repo.add_worktree("nu-redirect");
+ let target = canonicalize(repo.worktree_path("nu-redirect")).unwrap();
+ let mut script = String::new();
+ append_wrapper_setup(&mut script, "nu", &repo);
+ script.push_str(&format!(
+ "wt switch nu-redirect o> /dev/null\nprint $\"WT_CD:($env.PWD == {})\"\n",
+ nushell_quote(&target.to_string_lossy()),
+ ));
+
+ let config_path = repo.test_config_path().to_string_lossy().to_string();
+ let approvals_path = repo.test_approvals_path().to_string_lossy().to_string();
+ let env_vars = build_test_env_vars(&config_path, &approvals_path);
+ let (combined, exit_code) =
+ exec_in_pty_interactive("nu", &script, repo.root_path(), &env_vars, &[]);
+
+ assert_eq!(exit_code, 0, "Output:\n{combined}");
+ assert!(combined.contains("WT_CD:true"), "Output:\n{combined}");
+ }
+
/// Native signal deaths must cross Nushell's negative-status boundary
/// before the wrapper propagates the status, after applying CD and cleanup.
#[rstest]