util: Preserve path separators when parsing file positions (#66089)

## Summary

Fix Windows file links into WSL projects, including Agent links and `zed
path:line`. Preserve the original path prefix in
`PathWithPosition::parse_str` instead of rebuilding it with the host
platform's separator.

Closes #65323. Fixes a regression introduced in #64748.

Windows regression test output before the fix:

```text
 left: "/home/user/project/src\\main.rs"
right: "/home/user/project/src/main.rs"
```

The test passes after the fix. It compares raw `OsStr` values because
Windows `Path` equality hides separator differences.

## Testing

- Manually tested the patched Windows client connected to WSL: clicking
the previously failing Agent link opens the correct file as expected.
- Verified that the development CLI opens `llm_engine.py:49:7` at line
49, column 7 in the Windows GUI.
- Utility tests: **122 passed** on Linux; path-position tests: **7
passed** with each Windows GNU/MSVC target.
- Windows client, CLI, and matching WSL server builds passed, along with
Clippy, formatting, and `git diff --check`.

To verify, connect a patched Windows client to a WSL project. Have the
Agent produce a Markdown link to an existing file using its absolute WSL
path with `:line` or `:line:column`. Click it and verify the file and
cursor position. Also check the same path through the matching
development CLI.

## Self-review

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

Release Notes:

- Fixed file links with line numbers failing to open WSL files when
using Zed on Windows.

---------

Co-authored-by: Kirill Bulatov <kirill@zed.dev>
Co-authored-by: Kirill Bulatov <mail4score@gmail.com>
This commit is contained in:
buvidk1234
2026-10-24 18:06:50 +00:00
committed by GitHub
parent af4948a164
commit 347f171a3f
2 changed files with 65 additions and 11 deletions
@@ -15179,18 +15179,14 @@ mod tests {
vec![
Path::new(path!("/project"))
.join("crates/editor/src/inlays/inlay_hints.rs:394"),
Path::new(path!("/project"))
.join("crates/editor/src/inlays")
.join("inlay_hints.rs"),
Path::new(path!("/project")).join("crates/editor/src/inlays/inlay_hints.rs"),
],
),
(
"crates/editor/src/element.rs:608".to_string(),
vec![
Path::new(path!("/project")).join("crates/editor/src/element.rs:608"),
Path::new(path!("/project"))
.join("crates/editor/src")
.join("element.rs"),
Path::new(path!("/project")).join("crates/editor/src/element.rs"),
],
),
(missing_file_uri.clone(), Vec::new()),
@@ -15198,9 +15194,7 @@ mod tests {
"../outside/missing.md:2".to_string(),
vec![
Path::new(path!("/project")).join("../outside/missing.md:2"),
Path::new(path!("/project"))
.join("../outside")
.join("missing.md"),
Path::new(path!("/project")).join("../outside/missing.md"),
],
),
(
+62 -2
View File
@@ -647,6 +647,14 @@ impl PathWithPosition {
// in the future seems unlikely.
static SUFFIX_RE: LazyLock<Regex> =
LazyLock::new(|| Regex::new(ROW_COL_CAPTURE_REGEX).unwrap());
// `Path::with_file_name` inserts the host's separator, which can
// corrupt Unix paths when the editor runs on Windows.
let replace_file_name = |input: &str, file_name: &str, replacement: &str| {
input.rsplit_once(file_name).map_or_else(
|| PathBuf::from(input),
|(prefix, _)| PathBuf::from(format!("{prefix}{replacement}")),
)
};
match SUFFIX_RE
.captures(maybe_file_name_with_row_col)
.and_then(|captures| {
@@ -659,7 +667,11 @@ impl PathWithPosition {
let column = maybe_column.parse::<u32>().ok();
Self {
path: path.with_file_name(&maybe_file_name_with_row_col[..file_name_end]),
path: replace_file_name(
trimmed,
maybe_file_name_with_row_col,
&maybe_file_name_with_row_col[..file_name_end],
),
row,
column,
}
@@ -703,7 +715,7 @@ impl PathWithPosition {
Self {
path: if row.is_some() {
path.with_file_name(path_string)
replace_file_name(s, file_name, &path_string)
} else {
path.to_path_buf()
},
@@ -2482,6 +2494,54 @@ mod tests {
}
}
#[test]
fn path_with_position_preserves_path_separators() {
for (input, expected_path, expected_column) in [
(
"/home/user/project/src/main.rs:49",
"/home/user/project/src/main.rs",
None,
),
(
"/home/user/project/src/main.rs:49:7",
"/home/user/project/src/main.rs",
Some(7),
),
(
"/home/user/project/src/main.rs:49:in",
"/home/user/project/src/main.rs",
None,
),
("src/main.rs(49,7)", "src/main.rs", Some(7)),
("src/main.rs:49/", "src/main.rs", None),
("src/main.rs:49/.", "src/main.rs", None),
("parent//file.rs:49", "parent//file.rs", None),
("parent/./file.rs:49", "parent/./file.rs", None),
("parent/😀.rs:49", "parent/😀.rs", None),
("C:/project/src/main.rs:49", "C:/project/src/main.rs", None),
(
r"C:\project\src\main.rs:49",
r"C:\project\src\main.rs",
None,
),
(r"src\mixed/main.rs:49", r"src\mixed/main.rs", None),
(
r"\\server\share/main.rs:49",
r"\\server\share/main.rs",
None,
),
] {
let parsed = PathWithPosition::parse_str(input);
assert_eq!(
parsed.path.as_os_str(),
OsStr::new(expected_path),
"{input}"
);
assert_eq!(parsed.row, Some(49), "{input}");
assert_eq!(parsed.column, expected_column, "{input}");
}
}
#[test]
fn path_with_position_preserves_repeated_filename() {
for (input, expected_path) in [