Files
zed/crates/path
Xin Zhao a4a3a968d1 path: Replace normalize with component-based normalization in PathStyle (#65782)
## Summary

Follow-up to #64149 and an intermediate step toward the composable join
API discussed in
https://github.com/zed-industries/zed/pull/63102#issuecomment-6214033056.

This PR removes the handwritten `PathStyle::normalize()` and migrates
its call sites, then introduces two new methods:
`PathStyle::normalize_separators()` and
`PathStyle::normalize_lexically()`. `normalize_separators()` collects a
path's components into a `PathBuf` for the target `PathStyle`; the `/`
to `\` rewrite only happens on Windows, and not inside verbatim paths.
`normalize_lexically()` additionally resolves `.` and `..`.

The standard library has no direct equivalent of
`normalize_separators()`. The closest operation is
`Path::components().collect::<PathBuf>()`, but that implementation
pushes each component as a path and corrupts verbatim paths on Windows
(rust-lang/rust#164467). I rewrote the collection in a dedicated
`collect_components()` function that appends each component's raw
`OsStr` with a `PathStyle`-aware separator; `normalize_separators()` is
a thin wrapper around it. It removes redundant and trailing separators
and interior `.` components, and it does not resolve `..`. One thing to
note is that it does not normalize the prefix:
`//server/share/user/project` becomes `//server/share\user\project`.
This is necessary to prevent `//?/` from being transformed into `\\?\`:
`//?/C:` is parsed as `UNC("?", "C:")`, while `\\?\C:` is a verbatim
path with stricter separator rules. It may still be reasonable to
canonicalize other prefixes such as plain UNC or device paths, but that
would need an explicit exception for `//?/`; I kept the simpler
raw-prefix rule for now. Happy to discuss.

`normalize_lexically()` has a counterpart in std
`Path::normalize_lexically()`, but that method is still unstable and
under development. Combining the current std implementation with the
discussion in
https://github.com/rust-lang/rust/issues/134694#issuecomment-5780155118,
I implemented `PathStyle::normalize_lexically()` to return `PathBuf`
instead of `Result<PathBuf>`, preserve leading `..` components, and
discard `..` above a root instead of returning an error; this follows
the suggestion in that discussion that `a/../..` should normalize to
`..` rather than error. It mostly matches the old
`PathStyle::normalize()` behavior, with a few intentional differences: a
non-empty path that normalizes to nothing now returns `.` instead of an
empty path, Windows prefixes are preserved as written, `/` is not
treated as a separator inside verbatim paths, and `C:..\worktrees` keeps
`..` instead of collapsing to `C:worktrees`. It also normalizes
separators.

As discussed in
https://github.com/zed-industries/zed/pull/63102#issuecomment-6214033056,
although these two methods overlap, they expose different layers of
normalization that callers can opt into on demand. The next step is to
replace the three join helpers (`PathStyle::join`,
`PathStyle::join_path`, and
`PathStyle::join_path_preserving_components`) with a plain
`PathStyle::join` that mirrors `std::path::Path::join`, leaving
separator and lexical normalization as explicit steps that callers
compose.

## Testing

Added tests for both methods across Unix and Windows styles, covering
the scenarios described above.


## Self-review

- [x] I've reviewed my diff for quality, security, reliability, and
performance.
- [x] UI changes follow the
[checklist](https://zed.dev/docs/development/ui-checklist).
- [x] Tests cover the new or changed behavior.

Release Notes:

- N/A
2026-10-20 09:44:28 +00:00
..
…