Avoid panic!() when current directory does not exist (#9876)

## 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 <charlie.r.marsh@gmail.com>
This commit is contained in:
Imani Pelton 2024-12-13 16:39:46 -05:00 committed by GitHub
parent 7cdc1b2ec2
commit 40a2a6a959
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -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`].