Avoid panics in LSP store path handling (#42117)
Release Notes: - Fixed incorrect journal paths handling
This commit is contained in:
@@ -173,9 +173,15 @@ pub fn new_journal_entry(workspace: &Workspace, window: &mut Window, cx: &mut Ap
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn journal_dir(path: &str) -> Option<PathBuf> {
|
fn journal_dir(path: &str) -> Option<PathBuf> {
|
||||||
shellexpand::full(path) //TODO handle this better
|
let expanded = shellexpand::full(path).ok()?;
|
||||||
.ok()
|
let base_path = Path::new(expanded.as_ref());
|
||||||
.map(|dir| Path::new(&dir.to_string()).to_path_buf().join("journal"))
|
let absolute_path = if base_path.is_absolute() {
|
||||||
|
base_path.to_path_buf()
|
||||||
|
} else {
|
||||||
|
log::warn!("Invalid journal path {path:?} (not absolute), falling back to home directory",);
|
||||||
|
std::env::home_dir()?
|
||||||
|
};
|
||||||
|
Some(absolute_path.join("journal"))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn heading_entry(now: NaiveTime, hour_format: &HourFormat) -> String {
|
fn heading_entry(now: NaiveTime, hour_format: &HourFormat) -> String {
|
||||||
@@ -224,4 +230,65 @@ mod tests {
|
|||||||
assert_eq!(actual_heading_entry, expected_heading_entry);
|
assert_eq!(actual_heading_entry, expected_heading_entry);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
mod journal_dir_tests {
|
||||||
|
use super::super::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
#[cfg(target_family = "unix")]
|
||||||
|
fn test_absolute_unix_path() {
|
||||||
|
let result = journal_dir("/home/user");
|
||||||
|
assert!(result.is_some());
|
||||||
|
let path = result.unwrap();
|
||||||
|
assert!(path.is_absolute());
|
||||||
|
assert_eq!(path, PathBuf::from("/home/user/journal"));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_tilde_expansion() {
|
||||||
|
let result = journal_dir("~/documents");
|
||||||
|
assert!(result.is_some());
|
||||||
|
let path = result.unwrap();
|
||||||
|
|
||||||
|
assert!(path.is_absolute(), "Tilde should expand to absolute path");
|
||||||
|
|
||||||
|
if let Some(home) = std::env::home_dir() {
|
||||||
|
assert_eq!(path, home.join("documents").join("journal"));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_relative_path_falls_back_to_home() {
|
||||||
|
for relative_path in ["relative/path", "NONEXT/some/path", "../some/path"] {
|
||||||
|
let result = journal_dir(relative_path);
|
||||||
|
assert!(result.is_some(), "Failed for path: {}", relative_path);
|
||||||
|
let path = result.unwrap();
|
||||||
|
|
||||||
|
assert!(
|
||||||
|
path.is_absolute(),
|
||||||
|
"Path should be absolute for input '{}', got: {:?}",
|
||||||
|
relative_path,
|
||||||
|
path
|
||||||
|
);
|
||||||
|
|
||||||
|
if let Some(home) = std::env::home_dir() {
|
||||||
|
assert_eq!(
|
||||||
|
path,
|
||||||
|
home.join("journal"),
|
||||||
|
"Should fall back to home directory for input '{}'",
|
||||||
|
relative_path
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
#[cfg(target_os = "windows")]
|
||||||
|
fn test_absolute_path_windows_style() {
|
||||||
|
let result = journal_dir("C:\\Users\\user\\Documents");
|
||||||
|
assert!(result.is_some());
|
||||||
|
let path = result.unwrap();
|
||||||
|
assert_eq!(path, PathBuf::from("C:\\Users\\user\\Documents\\journal"));
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7654,14 +7654,13 @@ impl LspStore {
|
|||||||
let uri = lsp::Uri::from_file_path(&abs_path)
|
let uri = lsp::Uri::from_file_path(&abs_path)
|
||||||
.ok()
|
.ok()
|
||||||
.with_context(|| format!("Failed to convert path to URI: {}", abs_path.display()))
|
.with_context(|| format!("Failed to convert path to URI: {}", abs_path.display()))
|
||||||
.unwrap();
|
.log_err()?;
|
||||||
let next_snapshot = buffer.text_snapshot();
|
let next_snapshot = buffer.text_snapshot();
|
||||||
for language_server in language_servers {
|
for language_server in language_servers {
|
||||||
let language_server = language_server.clone();
|
let language_server = language_server.clone();
|
||||||
|
|
||||||
let buffer_snapshots = self
|
let buffer_snapshots = self
|
||||||
.as_local_mut()
|
.as_local_mut()?
|
||||||
.unwrap()
|
|
||||||
.buffer_snapshots
|
.buffer_snapshots
|
||||||
.get_mut(&buffer.remote_id())
|
.get_mut(&buffer.remote_id())
|
||||||
.and_then(|m| m.get_mut(&language_server.server_id()))?;
|
.and_then(|m| m.get_mut(&language_server.server_id()))?;
|
||||||
|
|||||||
@@ -2519,11 +2519,12 @@ Unspecified values have a `false` value, hints won't be toggled if all the modif
|
|||||||
"path": "~",
|
"path": "~",
|
||||||
"hour_format": "hour12"
|
"hour_format": "hour12"
|
||||||
}
|
}
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
### Path
|
### Path
|
||||||
|
|
||||||
- Description: The path of the directory where journal entries are stored.
|
- Description: The path of the directory where journal entries are stored. If an invalid path is specified, the journal will fall back to using `~` (the home directory).
|
||||||
- Setting: `path`
|
- Setting: `path`
|
||||||
- Default: `~`
|
- Default: `~`
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user