From b06edc3c8d84232688106ff5399d66cee6f18061 Mon Sep 17 00:00:00 2001 From: BMG Date: Thu, 1 Dec 2022 17:15:44 +0000 Subject: [PATCH] feat: hard fail on missing tools (#189) --- README.md | 18 +++++++++--------- src/command/format.rs | 2 ++ src/command/mod.rs | 5 +++++ src/engine.rs | 19 +++++++++++++++++-- 4 files changed, 33 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index 27953fb..3f515fd 100644 --- a/README.md +++ b/README.md @@ -47,15 +47,15 @@ USAGE: treefmt [FLAGS] [OPTIONS] [paths]... FLAGS: - --clear-cache Reset the evaluation cache. Use in case the cache is not precise enough - --fail-on-change Exit with error if any changes were made. Useful for CI - -h, --help Prints help information - --init Create a new treefmt.toml - --no-cache Ignore the evaluation cache entirely. Useful for CI - -q, --quiet No output printed to stderr - --stdin Format the content passed in stdin - -V, --version Prints version information - -v, --verbose Log verbosity is based off the number of v used + --allow-missing-formatter Do not exit with error if a configured formatter is missing + --clear-cache Clear the evaluation cache. Use in case the cache is not precise enough + --fail-on-change Exit with error if any changes were made. Useful for CI + -h, --help Prints help information + --init Create a new treefmt.toml + -q, --quiet No output printed to stderr + --stdin Format the content passed in stdin + -V, --version Prints version information + -v, --verbose Log verbosity is based off the number of v used OPTIONS: --config-file Run with the specified config file, which is not required to be in the tree to be diff --git a/src/command/format.rs b/src/command/format.rs index c0c3666..409d809 100644 --- a/src/command/format.rs +++ b/src/command/format.rs @@ -12,6 +12,7 @@ pub fn format_cmd( no_cache: bool, clear_cache: bool, fail_on_change: bool, + allow_missing_formatter: bool, selected_formatters: &Option>, ) -> anyhow::Result<()> { let proj_dirs = match ProjectDirs::from("com", "NumTide", "treefmt") { @@ -57,6 +58,7 @@ pub fn format_cmd( no_cache, clear_cache, fail_on_change, + allow_missing_formatter, selected_formatters, )?; diff --git a/src/command/mod.rs b/src/command/mod.rs index 7bad92d..e753570 100644 --- a/src/command/mod.rs +++ b/src/command/mod.rs @@ -41,6 +41,10 @@ pub struct Cli { )] pub fail_on_change: bool, + /// Do not exit with error if a configured formatter is missing + #[structopt(long = "allow-missing-formatter")] + pub allow_missing_formatter: bool, + /// Log verbosity is based off the number of v used #[structopt(long = "verbose", short = "v", parse(from_occurrences))] pub verbosity: u8, @@ -133,6 +137,7 @@ pub fn run_cli(cli: &Cli) -> anyhow::Result<()> { cli.no_cache, cli.clear_cache, cli.fail_on_change, + cli.allow_missing_formatter, &cli.formatters, )? } diff --git a/src/engine.rs b/src/engine.rs index 86032ee..41be8de 100644 --- a/src/engine.rs +++ b/src/engine.rs @@ -29,6 +29,7 @@ pub fn run_treefmt( no_cache: bool, clear_cache: bool, fail_on_change: bool, + allow_missing_formatter: bool, selected_formatters: &Option>, ) -> anyhow::Result<()> { assert!(tree_root.is_absolute()); @@ -82,10 +83,13 @@ pub fn run_treefmt( timed_debug("load config"); - // Load all the formatter instances from the config. Ignore the ones that failed. + // Load all the formatter instances from the config. + let mut expected_count = 0; + let formatters = project_config.formatter.into_iter().fold( BTreeMap::new(), |mut sum, (name, mut fmt_config)| { + expected_count += 1; fmt_config.excludes.extend_from_slice(&global_excludes); match Formatter::from_config(tree_root, &name, &fmt_config) { Ok(fmt_matcher) => match selected_formatters { @@ -98,7 +102,13 @@ pub fn run_treefmt( sum.insert(fmt_matcher.name.clone(), fmt_matcher); } }, - Err(err) => error!("Ignoring formatter #{} due to error: {}", name, err), + Err(err) => { + if allow_missing_formatter { + error!("Ignoring formatter #{} due to error: {}", name, err) + } else { + error!("Failed to load formatter #{} due to error: {}", name, err) + } + } }; sum }, @@ -106,6 +116,11 @@ pub fn run_treefmt( timed_debug("load formatters"); + // Check the number of configured formatters matches the number of formatters loaded + if !(allow_missing_formatter || formatters.len() == expected_count) { + return Err(anyhow!("One or more formatters are missing")); + } + // Load the eval cache let mut cache = if no_cache || clear_cache { // Start with an empty cache