Avoid stripping root for user path display (#6865)

## 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
```
This commit is contained in:
Charlie Marsh 2024-08-30 09:50:22 -04:00 committed by GitHub
parent 95416ad52e
commit 50c5fe96ec
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -65,6 +65,11 @@ impl<T: AsRef<Path>> 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<T: AsRef<Path>> Simplified for T {
fn user_display_from(&self, base: impl AsRef<Path>) -> 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(|_| {