Fix working directory when session has no messages (#3513)

Signed-off-by: Soroosh <soroosh.sarabadani@gmail.com>
This commit is contained in:
Soroosh Sarabadani
2025-07-26 21:44:27 +01:00
committed by GitHub
parent d532042f20
commit 247773fa8a
+10 -3
View File
@@ -32,6 +32,12 @@ fn get_home_dir() -> PathBuf {
.to_path_buf() .to_path_buf()
} }
fn get_current_working_dir() -> PathBuf {
std::env::current_dir()
.or_else(|_| Ok::<PathBuf, io::Error>(get_home_dir()))
.expect("could not determine the current working directory")
}
/// Metadata for a session, stored as the first line in the session file /// Metadata for a session, stored as the first line in the session file
#[derive(Debug, Clone, Serialize, ToSchema)] #[derive(Debug, Clone, Serialize, ToSchema)]
pub struct SessionMetadata { pub struct SessionMetadata {
@@ -87,7 +93,7 @@ impl<'de> Deserialize<'de> for SessionMetadata {
let working_dir = helper let working_dir = helper
.working_dir .working_dir
.filter(|path| path.exists()) .filter(|path| path.exists())
.unwrap_or_else(get_home_dir); .unwrap_or_else(get_current_working_dir);
Ok(SessionMetadata { Ok(SessionMetadata {
description: helper.description, description: helper.description,
@@ -132,7 +138,7 @@ impl SessionMetadata {
impl Default for SessionMetadata { impl Default for SessionMetadata {
fn default() -> Self { fn default() -> Self {
Self::new(get_home_dir()) Self::new(get_current_working_dir())
} }
} }
@@ -1702,6 +1708,7 @@ mod tests {
// Create metadata with non-existent directory // Create metadata with non-existent directory
let invalid_dir = PathBuf::from("/path/that/does/not/exist"); let invalid_dir = PathBuf::from("/path/that/does/not/exist");
let metadata = SessionMetadata::new(invalid_dir.clone()); let metadata = SessionMetadata::new(invalid_dir.clone());
// Should fall back to home directory // Should fall back to home directory
@@ -1724,7 +1731,7 @@ mod tests {
// Read back - should fall back to home dir // Read back - should fall back to home dir
let read_metadata = read_metadata(&file_path)?; let read_metadata = read_metadata(&file_path)?;
assert_ne!(read_metadata.working_dir, invalid_dir); assert_ne!(read_metadata.working_dir, invalid_dir);
assert_eq!(read_metadata.working_dir, get_home_dir()); assert_eq!(read_metadata.working_dir, get_current_working_dir());
Ok(()) Ok(())
} }