Skip to content

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

fix(shell): bypass rm aliases in the nushell wrapper's cleanup (#3732)

Worktrunk Bot78010166bd3e
diff --git a/src/shell/snapshots/worktrunk__shell__tests__init_nu.snap b/src/shell/snapshots/worktrunk__shell__tests__init_nu.snap
index 1e59d694f..05382aea0 100644
--- a/src/shell/snapshots/worktrunk__shell__tests__init_nu.snap
+++ b/src/shell/snapshots/worktrunk__shell__tests__init_nu.snap
@@ -127,9 +127,28 @@ export def --env --wrapped wt [...args] {
^sh -c $script
}
- rm -f $cd_file $exec_file
let output = (open $stdout_file --raw)
- rm -f $stdout_file
+
+ # Clean up all three temp files at once, after the last read of any of them.
+ #
+ # A bare `rm` here is shadowable by the user's own `alias rm = ...`:
+ # nushell resolves aliases at parse time, and `config.nu` runs before the
+ # vendor autoload dir this file lives in, so the alias is already in scope
+ # when this `def` is parsed. A `trash`-style alias then leaks the temp
+ # files, and an alias that exits non-zero raises a ShellError that aborts
+ # the wrapper before it can return `$output` — swallowing the command's
+ # stdout entirely.
+ #
+ # `^rm` bypasses aliases, but it's external-only, so Windows (where the
+ # nushell builtin is the only `rm`) keeps the builtin. `try` covers both
+ # branches so cleanup can never abort the wrapper. `hide rm` is not an
+ # option: it's a parse-time keyword that leaks out of this function into
+ # the user's session, silently unbinding their alias.
+ if $nu.os-info.family == "windows" {
+ try { rm -f $cd_file $exec_file $stdout_file }
+ } else {
+ try { ^rm -f $cd_file $exec_file $stdout_file }
+ }
# Return stdout or propagate failure as the function's last expression.
# Using a failing external command (not `error make`) so nushell treats it
diff --git a/templates/nushell.nu b/templates/nushell.nu
index 369bc3402..ea534206f 100644
--- a/templates/nushell.nu
+++ b/templates/nushell.nu
@@ -123,9 +123,28 @@ export def --env --wrapped {{ cmd }} [...args] {
^sh -c $script
}
- rm -f $cd_file $exec_file
let output = (open $stdout_file --raw)
- rm -f $stdout_file
+
+ # Clean up all three temp files at once, after the last read of any of them.
+ #
+ # A bare `rm` here is shadowable by the user's own `alias rm = ...`:
+ # nushell resolves aliases at parse time, and `config.nu` runs before the
+ # vendor autoload dir this file lives in, so the alias is already in scope
+ # when this `def` is parsed. A `trash`-style alias then leaks the temp
+ # files, and an alias that exits non-zero raises a ShellError that aborts
+ # the wrapper before it can return `$output` — swallowing the command's
+ # stdout entirely.
+ #
+ # `^rm` bypasses aliases, but it's external-only, so Windows (where the
+ # nushell builtin is the only `rm`) keeps the builtin. `try` covers both
+ # branches so cleanup can never abort the wrapper. `hide rm` is not an
+ # option: it's a parse-time keyword that leaks out of this function into
+ # the user's session, silently unbinding their alias.
+ if $nu.os-info.family == "windows" {
+ try { rm -f $cd_file $exec_file $stdout_file }
+ } else {
+ try { ^rm -f $cd_file $exec_file $stdout_file }
+ }
# Return stdout or propagate failure as the function's last expression.
# Using a failing external command (not `error make`) so nushell treats it
diff --git a/tests/integration_tests/shell_wrapper.rs b/tests/integration_tests/shell_wrapper.rs
index 161a3cc11..f3141bc92 100644
--- a/tests/integration_tests/shell_wrapper.rs
+++ b/tests/integration_tests/shell_wrapper.rs
@@ -840,6 +840,62 @@ mod unix_tests {
});
}
+ /// A user's `alias rm = ...` must not intercept the nushell wrapper's
+ /// temp-file cleanup.
+ ///
+ /// Nushell resolves aliases at parse time, and `config.nu` runs before the
+ /// vendor autoload dir the wrapper is installed into — so an alias declared
+ /// there is already in scope when the wrapper's `def` is parsed. Before the
+ /// fix, an alias that exits non-zero raised a ShellError mid-cleanup that
+ /// aborted the wrapper before it returned the command's stdout, and left
+ /// every temp file behind. `^false` stands in for the realistic aliases
+ /// (`trash`, a wrapper that prompts, one that isn't installed on this box).
+ ///
+ /// The POSIX wrappers use `command rm` for the same reason; nushell has no
+ /// `command` builtin, so the template branches on `$nu.os-info.family`.
+ #[rstest]
+ fn test_nu_wrapper_cleanup_survives_rm_alias(repo: TestRepo) {
+ // A dedicated TMPDIR so the wrapper's `mktemp` files are the only
+ // occupants, and a leak is directly observable.
+ let tmp = tempfile::tempdir().unwrap();
+ let tmp_path = tmp.path().to_string_lossy().to_string();
+
+ let mut script = String::new();
+ // Must precede the wrapper's `def`: alias resolution is parse-time.
+ script.push_str("alias rm = ^false\n");
+ append_wrapper_setup(&mut script, "nu", &repo);
+ // `config show` writes to stdout, which the wrapper returns as the
+ // function's value — the part a mid-cleanup abort swallows. `switch`
+ // wouldn't show it: its output is on stderr, which streams to the
+ // terminal before cleanup runs either way.
+ script.push_str("let out = (wt config show)\n");
+ script.push_str("print $\"WRAPPER_STDOUT_EMPTY:($out | is-empty)\"\n");
+
+ 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 mut env_vars = build_test_env_vars(&config_path, &approvals_path);
+ env_vars.push(("TMPDIR", &tmp_path));
+
+ let (combined, exit_code) =
+ exec_in_pty_interactive("nu", &script, repo.root_path(), &env_vars, &[]);
+
+ assert!(
+ combined.contains("WRAPPER_STDOUT_EMPTY:false"),
+ "wrapper returned no stdout — the marker is missing entirely when cleanup \
+ aborted the wrapper, and reads `true` when it returned an empty value.\nOutput:\n{combined}"
+ );
+ assert_eq!(exit_code, 0, "Output:\n{combined}");
+
+ let leftover: Vec<_> = fs::read_dir(tmp.path())
+ .unwrap()
+ .map(|e| e.unwrap().file_name())
+ .collect();
+ assert!(
+ leftover.is_empty(),
+ "wrapper leaked temp files past cleanup: {leftover:?}"
+ );
+ }
+
#[rstest]
#[case("bash")]
#[case("zsh")]