From 25ad534d6404e2aa39536df818371dcbbc928b52 Mon Sep 17 00:00:00 2001 From: Michael Davis Date: Mon, 12 Jun 2023 10:32:36 -0500 Subject: [PATCH] Check for 'git' before fetching/building grammars (#7320) Previously the error message for this potential failure-case was confusing: "no such file or directory". `hx -g fetch`, `hx -g build` and the helix-term builder should bail early if the git binary is not available. --- Cargo.lock | 1 + helix-loader/Cargo.toml | 1 + helix-loader/src/grammar.rs | 11 +++++++++++ 3 files changed, 13 insertions(+) diff --git a/Cargo.lock b/Cargo.lock index 182eed9e7..d86ba9553 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1167,6 +1167,7 @@ dependencies = [ "threadpool", "toml", "tree-sitter", + "which", ] [[package]] diff --git a/helix-loader/Cargo.toml b/helix-loader/Cargo.toml index b0d8f1462..6e0217a29 100644 --- a/helix-loader/Cargo.toml +++ b/helix-loader/Cargo.toml @@ -21,6 +21,7 @@ etcetera = "0.8" tree-sitter = "0.20" once_cell = "1.18" log = "0.4" +which = "4.4" # TODO: these two should be on !wasm32 only diff --git a/helix-loader/src/grammar.rs b/helix-loader/src/grammar.rs index 16955187e..66111aebb 100644 --- a/helix-loader/src/grammar.rs +++ b/helix-loader/src/grammar.rs @@ -85,7 +85,16 @@ pub fn get_language(name: &str) -> Result { Ok(language) } +fn ensure_git_is_available() -> Result<()> { + match which::which("git") { + Ok(_cmd) => Ok(()), + Err(err) => Err(anyhow::anyhow!("'git' could not be found ({err})")), + } +} + pub fn fetch_grammars() -> Result<()> { + ensure_git_is_available()?; + // We do not need to fetch local grammars. let mut grammars = get_grammar_configs()?; grammars.retain(|grammar| !matches!(grammar.source, GrammarSource::Local { .. })); @@ -145,6 +154,8 @@ pub fn fetch_grammars() -> Result<()> { } pub fn build_grammars(target: Option) -> Result<()> { + ensure_git_is_available()?; + let grammars = get_grammar_configs()?; println!("Building {} grammars", grammars.len()); let results = run_parallel(grammars, move |grammar| {