Skip to content

Commit

Permalink
fix: don't crash when cwd is not found
Browse files Browse the repository at this point in the history
Helix is crashing in a few places when it cannot
find the current working directory. Instead of
crashing, this commit ensures it will rely on a
default PathBuffer or display an appropriate
error message in the editor.

This fix the behavior on the global search,
file picker and using the open command.
  • Loading branch information
yo-main committed May 30, 2023
1 parent 0a91a24 commit 345ff0d
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 5 deletions.
2 changes: 1 addition & 1 deletion helix-loader/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ pub fn merge_toml_values(left: toml::Value, right: toml::Value, merge_depth: usi
/// If no workspace was found returns (CWD, true).
/// Otherwise (workspace, false) is returned
pub fn find_workspace() -> (PathBuf, bool) {
let current_dir = std::env::current_dir().expect("unable to determine current directory");
let current_dir = std::env::current_dir().unwrap_or_default();
for ancestor in current_dir.ancestors() {
if ancestor.join(".git").exists() || ancestor.join(".helix").exists() {
return (ancestor.to_owned(), false);
Expand Down
12 changes: 9 additions & 3 deletions helix-term/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2068,8 +2068,12 @@ fn global_search(cx: &mut Context) {
.binary_detection(BinaryDetection::quit(b'\x00'))
.build();

let search_root = std::env::current_dir()
.expect("Global search error: Failed to get current dir");
let search_root = std::env::current_dir();
if search_root.is_err() {
_editor.set_error("Global search error: Failed to get current dir");
return;
}
let search_root = search_root.unwrap();
let dedup_symlinks = file_picker_config.deduplicate_links;
let absolute_root = search_root
.canonicalize()
Expand Down Expand Up @@ -2141,7 +2145,9 @@ fn global_search(cx: &mut Context) {
let call: job::Callback = Callback::EditorCompositor(Box::new(
move |editor: &mut Editor, compositor: &mut Compositor| {
if all_matches.is_empty() {
editor.set_status("No matches found");
if !editor.is_err() {
editor.set_status("No matches found");
}
return;
}

Expand Down
2 changes: 1 addition & 1 deletion helix-term/src/ui/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -476,7 +476,7 @@ pub mod completers {
match path.parent() {
Some(path) if !path.as_os_str().is_empty() => path.to_path_buf(),
// Path::new("h")'s parent is Some("")...
_ => std::env::current_dir().expect("couldn't determine current directory"),
_ => std::env::current_dir().unwrap_or_default(),
}
};

Expand Down

0 comments on commit 345ff0d

Please sign in to comment.