From 40a2a6a959bb85b074333f6732be744cb1bdaed9 Mon Sep 17 00:00:00 2001 From: Imani Pelton Date: Fri, 13 Dec 2024 16:39:46 -0500 Subject: [PATCH] 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 --- crates/uv-fs/src/path.rs | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/crates/uv-fs/src/path.rs b/crates/uv-fs/src/path.rs index 7ce2ef42a..0e162c76c 100644 --- a/crates/uv-fs/src/path.rs +++ b/crates/uv-fs/src/path.rs @@ -6,8 +6,13 @@ use either::Either; use path_slash::PathExt; /// The current working directory. -pub static CWD: LazyLock = - LazyLock::new(|| std::env::current_dir().expect("The current directory must exist")); +#[allow(clippy::exit, clippy::print_stderr)] +pub static CWD: LazyLock = 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`].