Skip to content

Commit

Permalink
Improve error handling for which::which failures
Browse files Browse the repository at this point in the history
Co-authored-by: Pascal Kuthe <[email protected]>
  • Loading branch information
2 people authored and mtoohey31 committed Jun 2, 2024
1 parent 1396485 commit 856a883
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 9 deletions.
2 changes: 1 addition & 1 deletion helix-dap/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ impl Client {
id: usize,
) -> Result<(Self, UnboundedReceiver<Payload>)> {
// Resolve path to the binary
let cmd = helix_stdx::env::which(cmd).map_err(|err| anyhow::anyhow!(err))?;
let cmd = helix_stdx::env::which(cmd)?;

let process = Command::new(cmd)
.args(args)
Expand Down
2 changes: 2 additions & 0 deletions helix-dap/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ pub enum Error {
#[error("server closed the stream")]
StreamClosed,
#[error(transparent)]
ExecutableNotFound(#[from] helix_stdx::env::ExecutableNotFoundError),
#[error(transparent)]
Other(#[from] anyhow::Error),
}
pub type Result<T> = core::result::Result<T, Error>;
6 changes: 2 additions & 4 deletions helix-loader/src/grammar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,8 @@ pub fn get_language(name: &str) -> Result<Language> {
}

fn ensure_git_is_available() -> Result<()> {
match helix_stdx::env::which("git") {
Ok(_cmd) => Ok(()),
Err(err) => Err(anyhow::anyhow!("'git' could not be found ({err})")),
}
helix_stdx::env::which("git")?;
Ok(())
}

pub fn fetch_grammars() -> Result<()> {
Expand Down
2 changes: 1 addition & 1 deletion helix-lsp/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ impl Client {
doc_path: Option<&std::path::PathBuf>,
) -> Result<(Self, UnboundedReceiver<(usize, Call)>, Arc<Notify>)> {
// Resolve path to the binary
let cmd = helix_stdx::env::which(cmd).map_err(|err| anyhow::anyhow!(err))?;
let cmd = helix_stdx::env::which(cmd)?;

let process = Command::new(cmd)
.envs(server_environment)
Expand Down
2 changes: 2 additions & 0 deletions helix-lsp/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ pub enum Error {
#[error("Unhandled")]
Unhandled,
#[error(transparent)]
ExecutableNotFound(#[from] helix_stdx::env::ExecutableNotFoundError),
#[error(transparent)]
Other(#[from] anyhow::Error),
}

Expand Down
28 changes: 25 additions & 3 deletions helix-stdx/src/env.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
pub use which::which;

use std::{
ffi::OsStr,
path::{Path, PathBuf},
sync::RwLock,
};
Expand Down Expand Up @@ -36,10 +35,33 @@ pub fn env_var_is_set(env_var_name: &str) -> bool {
std::env::var_os(env_var_name).is_some()
}

pub fn binary_exists(binary_name: &str) -> bool {
pub fn binary_exists<T: AsRef<OsStr>>(binary_name: T) -> bool {
which::which(binary_name).is_ok()
}

pub fn which<T: AsRef<OsStr>>(
binary_name: T,
) -> Result<std::path::PathBuf, ExecutableNotFoundError> {
which::which(binary_name.as_ref()).map_err(|err| ExecutableNotFoundError {
command: binary_name.as_ref().to_string_lossy().into_owned(),
inner: err,
})
}

#[derive(Debug)]
pub struct ExecutableNotFoundError {
command: String,
inner: which::Error,
}

impl std::fmt::Display for ExecutableNotFoundError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "command '{}' not found: {}", self.command, self.inner)
}
}

impl std::error::Error for ExecutableNotFoundError {}

#[cfg(test)]
mod tests {
use super::{current_working_dir, set_current_working_dir};
Expand Down

0 comments on commit 856a883

Please sign in to comment.