Skip to content

Commit

Permalink
Avoid panic!() when current directory does not exist (#9876)
Browse files Browse the repository at this point in the history
## Summary

If the shell is currently in a directory that no longer exists, uv will
panic from any command. Panicking is a confusing behavior to those
unfamiliar with Rust and can sometimes make it hard to determine the
true issue.

Closes #9875 

## Test Plan

The reproduction steps in the issue report were followed and uv no
longer panics. `uv version` can still successfully print the version if
the directory does exist.

---------

Co-authored-by: Charlie Marsh <[email protected]>
  • Loading branch information
bepri and charliermarsh authored Dec 13, 2024
1 parent 7cdc1b2 commit 40a2a6a
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions crates/uv-fs/src/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,13 @@ use either::Either;
use path_slash::PathExt;

/// The current working directory.
pub static CWD: LazyLock<PathBuf> =
LazyLock::new(|| std::env::current_dir().expect("The current directory must exist"));
#[allow(clippy::exit, clippy::print_stderr)]
pub static CWD: LazyLock<PathBuf> = LazyLock::new(|| {
std::env::current_dir().unwrap_or_else(|_e| {
eprintln!("Current directory does not exist");
std::process::exit(1);
})
});

pub trait Simplified {
/// Simplify a [`Path`].
Expand Down

0 comments on commit 40a2a6a

Please sign in to comment.