@@ -1556,7 +1556,8 @@ pub(crate) fn default_working_directory(workspace: &Workspace, cx: &App) -> Opti
.read(cx)
.active_project_directory(cx)
.as_deref()
- .map(Path::to_path_buf),
+ .map(Path::to_path_buf)
+ .or_else(|| first_project_directory(workspace, cx)),
WorkingDirectory::FirstProjectDirectory => first_project_directory(workspace, cx),
WorkingDirectory::AlwaysHome => None,
WorkingDirectory::Always { directory } => {
@@ -1570,10 +1571,13 @@ pub(crate) fn default_working_directory(workspace: &Workspace, cx: &App) -> Opti
///Gets the first project's home directory, or the home directory
fn first_project_directory(workspace: &Workspace, cx: &App) -> Option<PathBuf> {
let worktree = workspace.worktrees(cx).next()?.read(cx);
- if !worktree.root_entry()?.is_dir() {
- return None;
+ let worktree_path = worktree.abs_path();
+ if worktree.root_entry()?.is_dir() {
+ Some(worktree_path.to_path_buf())
+ } else {
+ // If worktree is a file, return its parent directory
+ worktree_path.parent().map(|p| p.to_path_buf())
}
- Some(worktree.abs_path().to_path_buf())
}
#[cfg(test)]
@@ -1606,7 +1610,7 @@ mod tests {
});
}
- // No active entry, but a worktree, worktree is a file -> home_dir()
+ // No active entry, but a worktree, worktree is a file -> parent directory
#[gpui::test]
async fn no_active_entry_worktree_is_file(cx: &mut TestAppContext) {
let (project, workspace) = init_test(cx).await;
@@ -1621,9 +1625,9 @@ mod tests {
assert!(workspace.worktrees(cx).next().is_some());
let res = default_working_directory(workspace, cx);
- assert_eq!(res, None);
+ assert_eq!(res, Some(Path::new("/").to_path_buf()));
let res = first_project_directory(workspace, cx);
- assert_eq!(res, None);
+ assert_eq!(res, Some(Path::new("/").to_path_buf()));
});
}