From 4c5537e97c454fd4b1b502ddad3c217773d2e7ed Mon Sep 17 00:00:00 2001 From: Ed Page Date: Wed, 30 Oct 2024 14:41:47 -0500 Subject: [PATCH] fix(util): Respect all `..`s in `normalize_path` --- crates/cargo-util/src/paths.rs | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/crates/cargo-util/src/paths.rs b/crates/cargo-util/src/paths.rs index 429ee229fc46..fc10c5912d95 100644 --- a/crates/cargo-util/src/paths.rs +++ b/crates/cargo-util/src/paths.rs @@ -98,7 +98,14 @@ pub fn normalize_path(path: &Path) -> PathBuf { } Component::CurDir => {} Component::ParentDir => { - ret.pop(); + if ret.ends_with(Component::ParentDir) { + ret.push(Component::ParentDir); + } else { + let popped = ret.pop(); + if !popped && !ret.has_root() { + ret.push(Component::ParentDir); + } + } } Component::Normal(c) => { ret.push(c); @@ -879,13 +886,13 @@ mod tests { ("foo/bar/./././///", "foo/bar"), ("foo/bar/..", "foo"), ("foo/bar/../..", ""), - ("foo/bar/../../..", ""), - ("../../foo/bar", "foo/bar"), - ("../../foo/bar/", "foo/bar"), - ("../../foo/bar/./././///", "foo/bar"), - ("../../foo/bar/..", "foo"), - ("../../foo/bar/../..", ""), - ("../../foo/bar/../../..", ""), + ("foo/bar/../../..", ".."), + ("../../foo/bar", "../../foo/bar"), + ("../../foo/bar/", "../../foo/bar"), + ("../../foo/bar/./././///", "../../foo/bar"), + ("../../foo/bar/..", "../../foo"), + ("../../foo/bar/../..", "../.."), + ("../../foo/bar/../../..", "../../.."), ]; for (input, expected) in cases { let actual = normalize_path(std::path::Path::new(input));