Commit Graph

5 Commits

Author SHA1 Message Date
Xin Zhao 9521840152 path: Reimplement PathStyle::join based on the standard library's implementation (#65966)
## Summary

Final follow-up of #64149, this PR reimplements `PathStyle::join()` on
top of `std::path::PathBuf::push`, via a new `PathStyle`-aware
`PathBufExt::styled_push` helper. The old `join_path()` and
`join_path_preserving_components()` are removed, and their call sites
are migrated as follows:

- `join` -> `join`
- `join_path` -> `join` + `normalize_lexically`
- `join_path_preserving_components` -> `join`, plus
`normalize_separators` only where the joined path is displayed to the
user

I'm confident that the new `join()` mirrors std's behavior. The main
thing to be aware of is that the old `join` rejected a rooted right-hand
side and returned `None`, while the new one accepts it and handles it:
the left-hand side is replaced entirely, except for a root-only operand
such as `\b` on Windows, which keeps the left side's prefix (`C:\a` +
`\b` = `C:\b`).

Because the old `join` rejected rooted right-hand sides, all migrated
call sites were audited for `has_root`: the right operand either cannot
be rooted by construction, or the call site already guards rooted inputs
with `has_root`, or an explicit guard was added here.

One edge case was noticed but left unchanged: a drive-relative operand
such as `C:foo.rs` is now replaced instead of concatenated (mirroring
std). This case was equally unhandled before, so it is left as-is here;
adding another check beyond the existing `has_root` guard would not be
self-explanatory either. #65911 (git worktree names containing path
components) is likewise pre-existing and tracked separately.

## Testing

New tests are added for the new `join` method, adapted from the std lib.
The tests of the affected caller crates have been run to prevent
regressions, but this is not a guarantee.

## 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-28 15:21:52 +00:00
Xin Zhao f2a60bb8b6 title_bar: Fix project name shown for repo.git bare clones on Windows remotes (#66199)
## Summary

For a repo named `test_repo`, if it is bare cloned without renaming the
common dir, the common dir is `test_repo.git`, and Zed treats the
directory itself as the identity path:

https://github.com/zed-industries/zed/blob/ec86047d67acd6fddd81392f44ed6ebcc6e7f531/crates/project/src/git_store.rs#L10674-L10689

The title bar then uses the identity path to display the project name,
with the name determined by the std lib’s method:


https://github.com/zed-industries/zed/blob/ec86047d67acd6fddd81392f44ed6ebcc6e7f531/crates/title_bar/src/title_bar.rs#L284-L288
When connecting from a Unix client to a Windows remote host, the
identity path could be, for example, `C:\User\test\test_repo.git`.
Applying std’s path methods to it yields an extension of `git` and a
file stem of `C:\User\test\test_repo`, so the project name is displayed
as `C:\User\test\test_repo` instead of `test_repo`.

This PR introduces two new `PathStyle` methods to handle retrieving
`file_stem()` and `extension()` cross-platform. They mirror the std
lib’s behavior using the already introduced Path components.
## Testing

New unit tests for `PathStyle::file_stem()` and `PathStyle::extension()`
are introduced to guard its behavior. The title bar shown is locally
tested:

|Before|After|
|:--:|:--:|
| <img width="390" height="112" alt="Before"
src="https://github.com/user-attachments/assets/82c08423-a25e-4cd0-ae74-2614818d47c9"
/> |<img width="267" height="147" alt="After"
src="https://github.com/user-attachments/assets/b739c801-10ef-49da-90fb-c36233313a66"
/>|
## 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:

- Fixed project names in the title bar for `repo.git` bare clones on
remote Windows hosts.
2026-10-27 10:55:18 +00:00
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
Xin Zhao 51a3a0e971 path: Distinguish is_absolute and has_root in PathStyle (#65468)
## Summary

Follow-up of #64149,

Zed currently has three handcrafted, identical `is_absolute()`
functions, living in `crates/path/src/path.rs` and
`crates/util/src/paths.rs`. They read as follows:

https://github.com/zed-industries/zed/blob/93d745668805029476e6d7fbb19652116fed51de/crates/path/src/path.rs#L409-L420
In fact, these handcrafted methods behave like `Path::has_root()` rather
than `Path::is_absolute()`. The difference between the two is that
`Path::has_root()` only checks whether a path has a root, whereas
`Path::is_absolute()` checks whether the path is truly absolute. On
Unix, which has a single root, `has_root()` and `is_absolute()`
coincide. Windows, however, is multi-rooted (`C:\` and `D:\` are
different roots), so a rooted path such as `\temp\path` is not absolute:
depending on the working directory, it may resolve to `C:\temp\path` or
`D:\temp\path`, which are different paths. This misleading naming can
lead to misuse of the API.

This PR does three things:
1. Deduplicates the methods, keeping only `is_absolute()` in the
`PathStyle` body.
2. Reimplements `PathStyle::has_root()` and `PathStyle::is_absolute()`
based on the standard library’s implementations; the former replaces the
old `is_absolute()`.
3. Updates some callers of the old `is_absolute()` (now `has_root()`,
per step 2) to the new `is_absolute()`, based on each call site’s
semantics and the actual behavior of the methods.

Steps 1 and 2 are pure refactors; the only functional change is in step
3. I believe this fixes some edge cases — or at least surfaces things
that need fixing — though I am not entirely certain that every
replacement is correct.

## Testing

No new tests are introduced. The refactored parts pass the existing
tests in the affected crates.

## 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).
- [ ] Tests cover the new or changed behavior.

Release Notes:

- N/A
2026-10-12 12:08:43 +00:00
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