diff options
author | Yomain | 2023-07-11 17:51:04 +0000 |
---|---|---|
committer | GitHub | 2023-07-11 17:51:04 +0000 |
commit | 8afc0282f28e73cf78d1bd7b11d78fd853ae2036 (patch) | |
tree | 6742e141f051d352ebe7017f15805613670864b9 /helix-term | |
parent | 1adb19464f002926e1042027b41acef4c81585f6 (diff) |
Fix crash when cwd is deleted (#7185)
Diffstat (limited to 'helix-term')
-rw-r--r-- | helix-term/src/application.rs | 2 | ||||
-rw-r--r-- | helix-term/src/commands.rs | 23 | ||||
-rw-r--r-- | helix-term/src/commands/dap.rs | 2 | ||||
-rw-r--r-- | helix-term/src/commands/lsp.rs | 2 | ||||
-rw-r--r-- | helix-term/src/commands/typed.rs | 18 | ||||
-rw-r--r-- | helix-term/src/ui/mod.rs | 2 |
6 files changed, 33 insertions, 16 deletions
diff --git a/helix-term/src/application.rs b/helix-term/src/application.rs index 4c86a19f..546a57a9 100644 --- a/helix-term/src/application.rs +++ b/helix-term/src/application.rs @@ -163,7 +163,7 @@ impl Application { } else if !args.files.is_empty() { let first = &args.files[0].0; // we know it's not empty if first.is_dir() { - std::env::set_current_dir(first).context("set current dir")?; + helix_loader::set_current_working_dir(first.clone())?; editor.new_file(Action::VerticalSplit); let picker = ui::file_picker(".".into(), &config.load().editor); compositor.push(Box::new(overlaid(picker))); diff --git a/helix-term/src/commands.rs b/helix-term/src/commands.rs index ae715887..47b1a175 100644 --- a/helix-term/src/commands.rs +++ b/helix-term/src/commands.rs @@ -2080,8 +2080,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 = helix_loader::current_working_dir(); + if !search_root.exists() { + editor.set_error("Current working directory does not exist"); + return; + } + let dedup_symlinks = file_picker_config.deduplicate_links; let absolute_root = search_root .canonicalize() @@ -2173,7 +2177,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; } @@ -2518,6 +2524,10 @@ fn append_mode(cx: &mut Context) { fn file_picker(cx: &mut Context) { let root = find_workspace().0; + if !root.exists() { + cx.editor.set_error("Workspace directory does not exist"); + return; + } let picker = ui::file_picker(root, &cx.editor.config()); cx.push_layer(Box::new(overlaid(picker))); } @@ -2539,7 +2549,12 @@ fn file_picker_in_current_buffer_directory(cx: &mut Context) { cx.push_layer(Box::new(overlaid(picker))); } fn file_picker_in_current_directory(cx: &mut Context) { - let cwd = std::env::current_dir().unwrap_or_else(|_| PathBuf::from("./")); + let cwd = helix_loader::current_working_dir(); + if !cwd.exists() { + cx.editor + .set_error("Current working directory does not exist"); + return; + } let picker = ui::file_picker(cwd, &cx.editor.config()); cx.push_layer(Box::new(overlaid(picker))); } diff --git a/helix-term/src/commands/dap.rs b/helix-term/src/commands/dap.rs index 70a5ec21..e26dc08d 100644 --- a/helix-term/src/commands/dap.rs +++ b/helix-term/src/commands/dap.rs @@ -217,7 +217,7 @@ pub fn dap_start_impl( } } - args.insert("cwd", to_value(std::env::current_dir().unwrap())?); + args.insert("cwd", to_value(helix_loader::current_working_dir())?); let args = to_value(args).unwrap(); diff --git a/helix-term/src/commands/lsp.rs b/helix-term/src/commands/lsp.rs index 55153648..145bddd0 100644 --- a/helix-term/src/commands/lsp.rs +++ b/helix-term/src/commands/lsp.rs @@ -1033,7 +1033,7 @@ fn goto_impl( locations: Vec<lsp::Location>, offset_encoding: OffsetEncoding, ) { - let cwdir = std::env::current_dir().unwrap_or_default(); + let cwdir = helix_loader::current_working_dir(); match locations.as_slice() { [location] => { diff --git a/helix-term/src/commands/typed.rs b/helix-term/src/commands/typed.rs index 94cc33f0..dfc71dfd 100644 --- a/helix-term/src/commands/typed.rs +++ b/helix-term/src/commands/typed.rs @@ -1093,14 +1093,11 @@ fn change_current_directory( .as_ref(), ); - if let Err(e) = std::env::set_current_dir(dir) { - bail!("Couldn't change the current working directory: {}", e); - } + helix_loader::set_current_working_dir(dir)?; - let cwd = std::env::current_dir().context("Couldn't get the new working directory")?; cx.editor.set_status(format!( "Current working directory is now {}", - cwd.display() + helix_loader::current_working_dir().display() )); Ok(()) } @@ -1114,9 +1111,14 @@ fn show_current_directory( return Ok(()); } - let cwd = std::env::current_dir().context("Couldn't get the new working directory")?; - cx.editor - .set_status(format!("Current working directory is {}", cwd.display())); + let cwd = helix_loader::current_working_dir(); + let message = format!("Current working directory is {}", cwd.display()); + + if cwd.exists() { + cx.editor.set_status(message); + } else { + cx.editor.set_error(format!("{} (deleted)", message)); + } Ok(()) } diff --git a/helix-term/src/ui/mod.rs b/helix-term/src/ui/mod.rs index 155f2435..3359155d 100644 --- a/helix-term/src/ui/mod.rs +++ b/helix-term/src/ui/mod.rs @@ -472,7 +472,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"), + _ => helix_loader::current_working_dir(), } }; |