Skip to content

Commit

Permalink
fix: better error when cwd is gone (#370)
Browse files Browse the repository at this point in the history
If external command invocation fails, then check if the current working directory no longer exists; in that case, report a more specific error message.
  • Loading branch information
reubeno authored Jan 31, 2025
1 parent 87a70ea commit ee71122
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
10 changes: 9 additions & 1 deletion brush-core/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -442,7 +442,15 @@ pub(crate) fn execute_external_command(
sys::terminal::move_self_to_foreground()?;
}

if context.shell.options.sh_mode {
if !context.shell.working_dir.exists() {
// We may have failed because the working directory doesn't exist.
writeln!(
stderr,
"{}: working directory does not exist: {}",
context.shell.shell_name.as_ref().unwrap_or(&String::new()),
context.shell.working_dir.display()
)?;
} else if context.shell.options.sh_mode {
writeln!(
stderr,
"{}: {}: {}: not found",
Expand Down
12 changes: 12 additions & 0 deletions brush-shell/tests/cases/simple_commands.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,15 @@ cases:
stdin: |
./non-existent-command 2>/dev/null
echo "Result: $?"
- name: "Simple command with non-existent cwd"
# N.B. We intentionally fail here because there's no safe way to pick an
# alternate working directory that we are okay with. To our knowledge,
# there's no easy way to hold onto the current working directory past
# its deletion.
known_failure: true
stdin: |
mkdir test-dir
cd test-dir
rmdir $(pwd)
ls

0 comments on commit ee71122

Please sign in to comment.