Files
zed/crates/util
Ivan K. e21ff1b368 Fix opening path:line in a running Zed on Windows (#66501)
## Summary

On Windows, `Zed.exe path:line` and `Zed.exe path:line:column` now open
the file at that position when Zed is already running. Before this
change, the argument was dropped and nothing opened. Closes #66498.

**Before:** with Zed 1.26.2 (stable) running, `Zed.exe
"E:\zed-demo\demo.py:2"` does nothing. `Zed.log` shows:

```
ERROR [zed::zed::windows_only_instance] error parsing path argument: The system cannot find the file specified. (os error 2)
```

<img width="760" alt="Zed 1.26.2: Zed.exe demo.py:2 followed by the
ERROR line from Zed.log"
src="https://github.com/user-attachments/assets/bde230fd-d231-4415-993c-44dba9e840ba"
/>

**After:** with a build of this branch running, `zed.exe
"E:\zed-demo\greet.py:2"` opens `greet.py` in the existing window, with
the cursor at 2:1.

<img width="760" alt="This branch: greet.py open in the running window,
cursor at 2:1"
src="https://github.com/user-attachments/assets/b040a366-84d9-42e8-b444-e060239ab2f0"
/>

A relative path also works. Running `zed.exe "demo.py:5:7"` from
`E:\zed-demo` opens `E:\zed-demo\demo.py` at 5:7.

<img width="760" alt="This branch: demo.py opened from a relative path,
cursor at 5:7"
src="https://github.com/user-attachments/assets/82173f6a-a40d-489f-8f1d-c142e50e35ed"
/>

### Cause

When Zed is already running, `send_args_to_instance` in
`windows_only_instance.rs` passes each argument to
`std::fs::canonicalize` as is. A path with a `:line` suffix does not
exist on disk, so the call fails. The argument is not a URL either, so
it is logged and dropped.

The `zed` CLI does not have this problem. `parse_path_with_position` in
`crates/cli/src/main.rs` splits off the position first and canonicalizes
only the path.

### Change

`parse_path_with_position` moves from `crates/cli/src/main.rs` to
`util::paths`, and both the CLI and `windows_only_instance.rs` now call
it. The function body is unchanged. Its tests stay in the cli crate,
unchanged, because some of them change the current directory and are
safer in cli's own test binary. The running instance already parses the
position suffix in `derive_paths_with_position`.

In `windows_only_instance.rs`, the path and URL loop moves out of
`send_args_to_instance` into `split_paths_and_urls`. The URL check now
runs before the path is parsed. That order is needed because the shared
function also resolves paths that don't exist yet. As a result, a path
to a file that doesn't exist is now forwarded and opens as a new file,
as it does with the `zed` CLI, instead of being dropped.

Parsing in the calling process keeps relative paths relative to the
caller's working directory, not the running Zed's.

## Testing

- New test `test_split_paths_and_urls` (Windows only) passes an absolute
`Cargo.toml:2:5` and a `zed://file/...` URL. The path keeps `:2:5`, and
the URL is passed through unchanged.
- On Windows 11: `cargo test -p zed --bin zed windows_only_instance` 1
passed, and `cargo test -p cli` 2 passed. The two symlink tests in cli
are skipped on Windows, as on `main`.
- `cargo shear --locked --deny-warnings --check-test-targets` (1.13.4),
`cargo clippy -p util -p cli -p zed --bin zed --tests` and `cargo fmt
--all -- --check`: no issues.
- Manual check with a debug build of this branch on Windows 11. Debug
builds use the dev channel, which skips the single-instance check, so I
ran the build with `ZED_RELEASE_CHANNEL=nightly` to go through
`send_args_to_instance`:
  1. Started the build on a folder and left it running.
2. Ran `zed.exe "E:\zed-demo\greet.py:2"`. The second process exited,
one instance stayed running, and `greet.py` opened at 2:1.
3. From `E:\zed-demo`, ran `zed.exe "demo.py:5:7"`. `demo.py` opened at
5:7.
4. From `E:\zed-demo`, ran `zed.exe "new_file.py:3"`. An empty
`new_file.py` buffer opened in the same window. The status bar showed
`Failed to open E:\zed-demo\new_file.py`, which I take to be the
position not fitting an empty buffer: another new file opened without a
position gave no warning.
- Not tested on macOS or Linux. On those platforms the only change is
that the CLI imports the function from `util`.

## 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 `Zed.exe path:line` being ignored on Windows when Zed is already
running.
2026-11-04 16:29:30 +00:00
..