From 50c5fe96ecc9d5dca897c65489f2d9c4a02ac75f Mon Sep 17 00:00:00 2001 From: Charlie Marsh Date: Fri, 30 Aug 2024 09:50:22 -0400 Subject: [PATCH] Avoid stripping root for user path display (#6865) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ## Summary Closes https://github.com/astral-sh/uv/issues/6840. ## Test Plan ``` ❯ ~/workspace/uv/target/debug/uv pip list --verbose DEBUG uv 0.4.0 DEBUG Searching for Python interpreter in system path DEBUG Found `cpython-3.12.3-macos-aarch64-none` at `/Users/crmarsh/.local/share/rtx/installs/python/3.12.3/bin/python3` (search path) DEBUG Using Python 3.12.3 environment at /Users/crmarsh/.local/share/rtx/installs/python/3.12.3/bin/python3 ``` --- crates/uv-fs/src/path.rs | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/crates/uv-fs/src/path.rs b/crates/uv-fs/src/path.rs index 695bc5e42..3a3c051d8 100644 --- a/crates/uv-fs/src/path.rs +++ b/crates/uv-fs/src/path.rs @@ -65,6 +65,11 @@ impl> Simplified for T { fn user_display(&self) -> impl std::fmt::Display { let path = dunce::simplified(self.as_ref()); + // If current working directory is root, display the path as-is. + if CWD.ancestors().nth(1).is_none() { + return path.display(); + } + // Attempt to strip the current working directory, then the canonicalized current working // directory, in case they differ. let path = path.strip_prefix(CWD.simplified()).unwrap_or_else(|_| { @@ -78,6 +83,11 @@ impl> Simplified for T { fn user_display_from(&self, base: impl AsRef) -> impl std::fmt::Display { let path = dunce::simplified(self.as_ref()); + // If current working directory is root, display the path as-is. + if CWD.ancestors().nth(1).is_none() { + return path.display(); + } + // Attempt to strip the base, then the current working directory, then the canonicalized // current working directory, in case they differ. let path = path.strip_prefix(base.as_ref()).unwrap_or_else(|_| {