Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions crates/goose/src/session/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ fn get_home_dir() -> PathBuf {
.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")
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

excellent. I wonder if we should avoid panicking here though - either return a default "", or bubble up?

fn get_current_working_dir() -> Result<PathBuf, io::Error> {
std::env::current_dir().or_else(|_| Ok(get_home_dir()))
}

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah - in other cases if we can't find a cwd then default to home (I think that may only show up in rare testing scenarios though). Things will work if you run from home (as it is usually smart enough to then know what directory to really work in from context later on) - so seems reasonable to default instead of panic?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here we default to home dir and then panic. Isn't this the same behaviour as you outlined?
Note that get_home_dir itself also panic if it can't find the home dir. So I think the only question is whether want want to show a specific error here that cwd couldn't be determined, or home directory couldn't be determined.


/// Metadata for a session, stored as the first line in the session file
#[derive(Debug, Clone, Serialize, ToSchema)]
pub struct SessionMetadata {
Expand Down Expand Up @@ -86,7 +92,7 @@ impl<'de> Deserialize<'de> for SessionMetadata {
let working_dir = helper
.working_dir
.filter(|path| path.exists())
.unwrap_or_else(get_home_dir);
.unwrap_or_else(get_current_working_dir);

Ok(SessionMetadata {
description: helper.description,
Expand Down Expand Up @@ -131,7 +137,7 @@ impl SessionMetadata {

impl Default for SessionMetadata {
fn default() -> Self {
Self::new(get_home_dir())
Self::new(get_current_working_dir())
}
}

Expand Down Expand Up @@ -1701,6 +1707,7 @@ mod tests {

// Create metadata with non-existent directory
let invalid_dir = PathBuf::from("/path/that/does/not/exist");

let metadata = SessionMetadata::new(invalid_dir.clone());

// Should fall back to home directory
Expand All @@ -1723,7 +1730,7 @@ mod tests {
// Read back - should fall back to home dir
let read_metadata = read_metadata(&file_path)?;
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(())
}
Expand Down