From 347f171a3fd00a3b672382d78348b37bd969d17c Mon Sep 17 00:00:00 2001 From: buvidk1234 <161066602+buvidk1234@users.noreply.github.com> Date: Sat, 24 Oct 2026 18:06:50 +0000 Subject: [PATCH] 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 Co-authored-by: Kirill Bulatov --- .../src/conversation_view/thread_view.rs | 12 +--- crates/util/src/paths.rs | 64 ++++++++++++++++++- 2 files changed, 65 insertions(+), 11 deletions(-) diff --git a/crates/agent_ui/src/conversation_view/thread_view.rs b/crates/agent_ui/src/conversation_view/thread_view.rs index f6cf1030f86..95426d2e125 100644 --- a/crates/agent_ui/src/conversation_view/thread_view.rs +++ b/crates/agent_ui/src/conversation_view/thread_view.rs @@ -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"), ], ), ( diff --git a/crates/util/src/paths.rs b/crates/util/src/paths.rs index 03c595ce88e..510294da6f6 100644 --- a/crates/util/src/paths.rs +++ b/crates/util/src/paths.rs @@ -647,6 +647,14 @@ impl PathWithPosition { // in the future seems unlikely. static SUFFIX_RE: LazyLock = 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::().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 [