Represent relative paths using a dedicated, separator-agnostic type (#38744)

Closes https://github.com/zed-industries/zed/issues/38690
Closes #37353

### Background

On Windows, paths are normally separated by `\`, unlike mac and linux
where they are separated by `/`. When editing code in a project that
uses a different path style than your local system (e.g. remoting from
Windows to Linux, using WSL, and collaboration between windows and unix
users), the correct separator for a path may differ from the "native"
separator.

Previously, to work around this, Zed converted paths' separators in
numerous places. This was applied to both absolute and relative paths,
leading to incorrect conversions in some cases.

### Solution

Many code paths in Zed use paths that are *relative* to either a
worktree root or a git repository. This PR introduces a dedicated type
for these paths called `RelPath`, which stores the path in the same way
regardless of host platform, and offers `Path`-like manipulation APIs.
RelPath supports *displaying* the path using either separator, so that
we can display paths in a style that is determined at runtime based on
the current project.

The representation of absolute paths is left untouched, for now.
Absolute paths are different from relative paths because (except in
contexts where we know that the path refers to the local filesystem)
they should generally be treated as opaque strings. Currently we use a
mix of types for these paths (std::path::Path, String, SanitizedPath).

Release Notes:

- N/A

---------

Co-authored-by: Cole Miller <cole@zed.dev>
Co-authored-by: Piotr Osiewicz <24362066+osiewicz@users.noreply.github.com>
Co-authored-by: Peter Tripp <petertripp@gmail.com>
Co-authored-by: Smit Barmase <heysmitbarmase@gmail.com>
Co-authored-by: Lukas Wirth <me@lukaswirth.dev>
This commit is contained in:
Max Brunsfeld
2025-09-24 18:57:33 -04:00
committed by GitHub
co-authored by Cole Miller Piotr Osiewicz Peter Tripp Smit Barmase Lukas Wirth
parent 3c626f3758
commit 03f9cf4414
216 changed files with 5873 additions and 5257 deletions
+19 -18
View File
@@ -5,6 +5,7 @@ use std::path::{Path, PathBuf};
use std::sync::OnceLock;
pub use util::paths::home_dir;
use util::rel_path::RelPath;
/// A default editorconfig file name to use when resolving project settings.
pub const EDITORCONFIG_NAME: &str = ".editorconfig";
@@ -29,13 +30,13 @@ static CURRENT_DATA_DIR: OnceLock<PathBuf> = OnceLock::new();
static CONFIG_DIR: OnceLock<PathBuf> = OnceLock::new();
/// Returns the relative path to the zed_server directory on the ssh host.
pub fn remote_server_dir_relative() -> &'static Path {
Path::new(".zed_server")
pub fn remote_server_dir_relative() -> &'static RelPath {
RelPath::new(".zed_server").unwrap()
}
/// Returns the relative path to the zed_wsl_server directory on the wsl host.
pub fn remote_wsl_server_dir_relative() -> &'static Path {
Path::new(".zed_wsl_server")
pub fn remote_wsl_server_dir_relative() -> &'static RelPath {
RelPath::new(".zed_wsl_server").unwrap()
}
/// Sets a custom directory for all user data, overriding the default data directory.
@@ -398,28 +399,28 @@ pub fn remote_servers_dir() -> &'static PathBuf {
}
/// Returns the relative path to a `.zed` folder within a project.
pub fn local_settings_folder_relative_path() -> &'static Path {
Path::new(".zed")
pub fn local_settings_folder_name() -> &'static str {
".zed"
}
/// Returns the relative path to a `.vscode` folder within a project.
pub fn local_vscode_folder_relative_path() -> &'static Path {
Path::new(".vscode")
pub fn local_vscode_folder_name() -> &'static str {
".vscode"
}
/// Returns the relative path to a `settings.json` file within a project.
pub fn local_settings_file_relative_path() -> &'static Path {
Path::new(".zed/settings.json")
pub fn local_settings_file_relative_path() -> &'static RelPath {
RelPath::new(".zed/settings.json").unwrap()
}
/// Returns the relative path to a `tasks.json` file within a project.
pub fn local_tasks_file_relative_path() -> &'static Path {
Path::new(".zed/tasks.json")
pub fn local_tasks_file_relative_path() -> &'static RelPath {
RelPath::new(".zed/tasks.json").unwrap()
}
/// Returns the relative path to a `.vscode/tasks.json` file within a project.
pub fn local_vscode_tasks_file_relative_path() -> &'static Path {
Path::new(".vscode/tasks.json")
pub fn local_vscode_tasks_file_relative_path() -> &'static RelPath {
RelPath::new(".vscode/tasks.json").unwrap()
}
pub fn debug_task_file_name() -> &'static str {
@@ -432,13 +433,13 @@ pub fn task_file_name() -> &'static str {
/// Returns the relative path to a `debug.json` file within a project.
/// .zed/debug.json
pub fn local_debug_file_relative_path() -> &'static Path {
Path::new(".zed/debug.json")
pub fn local_debug_file_relative_path() -> &'static RelPath {
RelPath::new(".zed/debug.json").unwrap()
}
/// Returns the relative path to a `.vscode/launch.json` file within a project.
pub fn local_vscode_launch_file_relative_path() -> &'static Path {
Path::new(".vscode/launch.json")
pub fn local_vscode_launch_file_relative_path() -> &'static RelPath {
RelPath::new(".vscode/launch.json").unwrap()
}
pub fn user_ssh_config_file() -> PathBuf {