ZIm/crates/project/src
张小白 cff9ae0bbc
Better absolute path handling (#19727)
Closes #19866

This PR supersedes #19228, as #19228 encountered too many merge
conflicts.

After some exploration, I found that for paths with the `\\?\` prefix,
we can safely remove it and consistently use the clean paths in all
cases. Previously, in #19228, I thought we would still need the `\\?\`
prefix for IO operations to handle long paths better. However, this
turns out to be unnecessary because Rust automatically manages this for
us when calling IO-related APIs. For details, refer to Rust's internal
function
[`get_long_path`](017ae1b21f/library/std/src/sys/path/windows.rs (L225-L233)).

Therefore, we can always store and use paths without the `\\?\` prefix.

This PR introduces a `SanitizedPath` structure, which represents a path
stripped of the `\\?\` prefix. To prevent untrimmed paths from being
mistakenly passed into `Worktree`, the type of `Worktree`’s `abs_path`
member variable has been changed to `SanitizedPath`.

Additionally, this PR reverts the changes of #15856 and #18726. After
testing, it appears that the issues those PRs addressed can be resolved
by this PR.

### Existing Issue
To keep the scope of modifications manageable, `Worktree::abs_path` has
retained its current signature as `fn abs_path(&self) -> Arc<Path>`,
rather than returning a `SanitizedPath`. Updating the method to return
`SanitizedPath`—which may better resolve path inconsistencies—would
likely introduce extensive changes similar to those in #19228.

Currently, the limitation is as follows:

```rust
let abs_path: &Arc<Path> = snapshot.abs_path();
let some_non_trimmed_path = Path::new("\\\\?\\C:\\Users\\user\\Desktop\\project"); 
// The caller performs some actions here:
some_non_trimmed_path.strip_prefix(abs_path);  // This fails
some_non_trimmed_path.starts_with(abs_path);   // This fails too
```

The final two lines will fail because `snapshot.abs_path()` returns a
clean path without the `\\?\` prefix. I have identified two relevant
instances that may face this issue:
-
[lsp_store.rs#L3578](0173479d18/crates/project/src/lsp_store.rs (L3578))
-
[worktree.rs#L4338](0173479d18/crates/worktree/src/worktree.rs (L4338))

Switching `Worktree::abs_path` to return `SanitizedPath` would resolve
these issues but would also lead to many code changes.

Any suggestions or feedback on this approach are very welcome.

cc @SomeoneToIgnore 

Release Notes:

- N/A
2024-11-27 20:22:58 +02:00
..
lsp_command use ssh lsp store (#17655) 2024-09-10 15:51:01 -04:00
buffer_store.rs Simplify BufferStore's local vs remote state (#21180) 2024-11-25 12:53:23 -08:00
color_extractor.rs Show color swatches for LSP completions (#18665) 2024-10-03 14:38:17 -04:00
connection_manager.rs chore: Fix several style lints (#17488) 2024-09-06 11:58:39 +02:00
debounced_delay.rs chore: Fix several style lints (#17488) 2024-09-06 11:58:39 +02:00
direnv.rs Environment loading fixes (#19144) 2024-10-16 12:14:40 +02:00
environment.rs environment: Log stderr too if command fails to run (#20659) 2024-11-14 14:44:32 +01:00
image_store.rs Add File.disk_state enum to clarify filesystem states (#20776) 2024-11-18 10:30:08 -08:00
lsp_command.rs Properly use lsp::CompletionList defaults (#21202) 2024-11-26 12:23:38 +02:00
lsp_ext_command.rs lsp: Retrieve links to documentation for the given symbol (#19233) 2024-11-16 20:23:49 +02:00
lsp_store.rs Better absolute path handling (#19727) 2024-11-27 20:22:58 +02:00
prettier_store.rs language servers: Fix wrong language server name (#20428) 2024-11-11 10:18:38 +01:00
project.rs Filter LSP code actions based on the requested kinds (#20847) 2024-11-22 13:01:00 +01:00
project_settings.rs language servers: Fix wrong language server name (#20428) 2024-11-11 10:18:38 +01:00
project_tests.rs Filter LSP code actions based on the requested kinds (#20847) 2024-11-22 13:01:00 +01:00
search.rs Enable look-around in Project Search using fancy-regex crate (#20308) 2024-11-07 09:18:09 -07:00
search_history.rs project search: Persist search history across session (#9932) 2024-04-02 11:13:18 +02:00
task_inventory.rs tasks: Add ability to query active toolchains for languages (#20667) 2024-11-14 14:37:37 +01:00
task_store.rs tasks: Add ability to query active toolchains for languages (#20667) 2024-11-14 14:37:37 +01:00
terminals.rs Add the option to hide both the task and command lines in the task output (#20920) 2024-11-22 13:45:42 -05:00
toolchain_store.rs toolchains: Expose raw JSON representation of a toolchain (#20721) 2024-11-21 13:27:25 +01:00
worktree_store.rs Better absolute path handling (#19727) 2024-11-27 20:22:58 +02:00
yarn.rs chore: Fix several style lints (#17488) 2024-09-06 11:58:39 +02:00