`wt remove` from inside a worktree produces repeated Nushell `$env.PWD` errors
Summary
When running wt remove from inside a worktree with the Nushell shell
integration, the terminal outputs 5×
nu::shell::error: $env.PWD points to a non-existent directory.
The staged removal path renames the worktree directory synchronously
inside the binary (before exit). By the time the Nushell wrapper reads
the directive file and executes the cd, $env.PWD already points to a
non-existent path. Nushell validates $env.PWD on each internal
operation between binary exit and cd completion, producing 5 errors.
Steps to reproduce
cd /path/to/some-worktree
wt remove
Expected behavior
Silent switch to the main worktree with no error output.
Actual behavior
nu::shell::error: $env.PWD points to a non-existent directory
nu::shell::error: $env.PWD points to a non-existent directory
nu::shell::error: $env.PWD points to a non-existent directory
nu::shell::error: $env.PWD points to a non-existent directory
nu::shell::error: $env.PWD points to a non-existent directory
The cd to the main worktree does eventually succeed, but only after
the 5 errors.
Root cause
The incorrect assumption in build_remove_command_staged
src/commands/process.rs:395-397 contains this comment:
/// No sleep is needed because:
/// 1. The shell cd happens before the rename (directive file is written first)
/// 2. The original worktree path no longer exists immediately after rename
Claim 1 has a flaw. The directive file gets written first, but the shell only reads it after the binary exits. The rename also happens inside the binary—before exit—so the sequence follows:
- Binary writes
cd '<main-worktree-path>'to the directive file std::fs::rename(worktree_path, &staged_path)executes (src/output/handlers.rs:62)—the worktree directory disappears from its original path- Binary exits
- Nushell reads the directive file (
templates/nushell.nu:111) and processes directives - Nushell executes
cd $target_dir(templates/nushell.nu:122)
Between steps 2 and 5, $env.PWD goes stale. Nushell internal
operations (prompt updates, path validation, etc.) fire during steps 3–5
and each validates $env.PWD, producing one error per validation.
Contrast with the legacy path
build_remove_command (src/commands/process.rs:419)—the fallback for
cross-filesystem renames—includes sleep 1 with a detailed comment
(lines 429-436) documenting exactly this race for other shells. The
staged path received no matching protection because the author believed
the directive-file ordering prevented it—it doesn't.
Proposed fix
Option A—Move rename to the background rm -rf command: instead of
renaming the worktree in the binary, emit a background command that does
both the rename and the rm -rf. The original worktree directory exists
under its original path until after the shell cds away.
Option B—Add sleep 1 to build_remove_command_staged: consistent
with the legacy path. Conservative but guaranteed to work. The
background command becomes:
sleep 1 && rm -rf -- <staged-path>
The sleep 1 gives the shell enough time to cd away before the
process exits.
Option A represents the principled fix. Option B makes the minimal safe change that mirrors the existing workaround.
Workaround
wt remove
cd ~
Running cd ~ after the errors clears $env.PWD and stops the spam.
Thanks for reporting this! The analysis of the race between the rename and the shell's cd directive was spot-on.
I've opened #1508 with a fix that takes a hybrid approach: after the rename, an empty placeholder directory is created at the original worktree path so $env.PWD stays valid until the shell wrapper processes the cd. The background command includes sleep 1 (consistent with the legacy path) and cleans up the placeholder before removing the staged directory.
A maintainer will review it shortly.