Xin Zhao a84689073d path: Reimplement PathStyle on the standard library's path implementation (#64149)
# Objective

Rust's standard library `Path` API is platform-dependent, so you get
wrong results when processing a Windows path on Unix, or vice versa. Zed
needs this functionality because we support remote development, where
cross-platform path handling is routine. Zed's `PathStyle` takes on this
responsibility.

But handling paths can be really tricky: we need to handle paths with
non-UTF-8 characters, a problem I ran into in #63075. And thanks to
Microsoft, there are 5 different kinds of valid path prefixes! Not to
mention the `/` and `\` separators, which was another problem I ran into
in #63102. So we need to make some changes to the `PathStyle` API.

## Solution

I'm not an expert on this low-level API design, or on API design in
general. But the people who maintain the Rust standard library are.
Since `PathStyle`'s API is largely a cross-platform version of the
standard library's `Path`, the most reliable and straightforward
approach I could come up with is to bring the standard library's
implementation into the Zed repo and make the necessary modifications to
make it work cross-platform.

It turns out Rust already has a great abstraction: the
platform-dependent part of `Path` is packaged under
`library/std/src/sys/path` and selected at compile time, while the
`Path` implementation just uses the abstracted constants and methods:


https://github.com/rust-lang/rust/blob/6474e999898cad2289175e2a7d253f5d0f574fa2/library/std/src/path.rs#L96

So we can add some helper methods to `PathStyle` to represent the
compile-time-determined pieces of the standard library. Then we bring in
the rest, achieving our goal of making `PathStyle`'s functionality the
same as the standard library's, but cross-platform.

Overall, the `parent()` and `file_name()` methods I introduced in #62038
have been reimplemented, and a new `starts_with()` method has been
added. Their implementations are nearly the same as the ones in the
standard library. To make these methods work, the standard library's
`Components` struct was added in a new file,
`crates/path/src/path_component.rs`, which is the core of reliable path
handling; and `crates/path/src/windows_prefix.rs` was added to handle
Windows paths. Most of the code in these two files is the same as the
standard library's; I only made some small tweaks to make it work with
`PathStyle`.

## Testing

New tests for `starts_with` have been added.

## Self-Review Checklist:

- [x] I've reviewed my own diff for quality, security, and reliability
- [x] Unsafe blocks (if any) have justifying comments
- [x] The content adheres to Zed's UI standards
([UX/UI](https://github.com/zed-industries/zed/blob/main/CONTRIBUTING.md#uiux-checklist)
and
[icon](https://github.com/zed-industries/zed/blob/main/crates/icons/README.md)
guidelines)
- [x] Tests cover the new/changed behavior
- [x] Performance impact has been considered and is acceptable

---

Release Notes:

- N/A
2026-10-03 08:53:38 +00:00
…
…
…
…
…
…
…
…
…
…
…

Zed

Zed CI

Welcome to Zed, a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.


Installation

On macOS, Linux, and Windows you can download Zed directly or install Zed via your local package manager (macOS/Linux/Windows).

Other platforms are not yet available:

Developing Zed

Contributing

See CONTRIBUTING.md for ways you can contribute to Zed.

Also... we're hiring! Check out our jobs page for open roles.

Licensing

Zed source code is licensed primarily under GPL-3.0-or-later, with Apache-2.0 components where marked.

License information for third party dependencies must be correctly provided for CI to pass.

We use cargo-about to automatically comply with open source licenses. If CI is failing, check the following:

  • Is it showing a no license specified error for a crate you've created? If so, add publish = false under [package] in your crate's Cargo.toml.
  • Is the error failed to satisfy license requirements for a dependency? If so, first determine what license the project has and whether this system is sufficient to comply with this license's requirements. If you're unsure, ask a lawyer. Once you've verified that this system is acceptable add the license's SPDX identifier to the accepted array in script/licenses/zed-licenses.toml.
  • Is cargo-about unable to find the license for a dependency? If so, add a clarification field at the end of script/licenses/zed-licenses.toml, as specified in the cargo-about book.

Sponsorship

Zed is developed by Zed Industries, Inc., a for-profit company.

If you’d like to financially support the project, you can do so via GitHub Sponsors. Sponsorships go directly to Zed Industries and are used as general company revenue. There are no perks or entitlements associated with sponsorship.

S
Description
No description provided
Readme 1.6 GiB
Languages
Rust 95.4%
JSON-with-Comments 2.4%
Python 0.8%
Inno Setup 0.3%
Tree-sitter Query 0.3%
Other 0.4%