Skip to content

Commit b06edc3

Browse files
authored
feat: hard fail on missing tools (#189)
1 parent 1375bff commit b06edc3

File tree

4 files changed

+33
-11
lines changed

4 files changed

+33
-11
lines changed

README.md

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -47,15 +47,15 @@ USAGE:
4747
treefmt [FLAGS] [OPTIONS] [paths]...
4848
4949
FLAGS:
50-
--clear-cache Reset the evaluation cache. Use in case the cache is not precise enough
51-
--fail-on-change Exit with error if any changes were made. Useful for CI
52-
-h, --help Prints help information
53-
--init Create a new treefmt.toml
54-
--no-cache Ignore the evaluation cache entirely. Useful for CI
55-
-q, --quiet No output printed to stderr
56-
--stdin Format the content passed in stdin
57-
-V, --version Prints version information
58-
-v, --verbose Log verbosity is based off the number of v used
50+
--allow-missing-formatter Do not exit with error if a configured formatter is missing
51+
--clear-cache Clear the evaluation cache. Use in case the cache is not precise enough
52+
--fail-on-change Exit with error if any changes were made. Useful for CI
53+
-h, --help Prints help information
54+
--init Create a new treefmt.toml
55+
-q, --quiet No output printed to stderr
56+
--stdin Format the content passed in stdin
57+
-V, --version Prints version information
58+
-v, --verbose Log verbosity is based off the number of v used
5959
6060
OPTIONS:
6161
--config-file <config-file> Run with the specified config file, which is not required to be in the tree to be

src/command/format.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ pub fn format_cmd(
1212
no_cache: bool,
1313
clear_cache: bool,
1414
fail_on_change: bool,
15+
allow_missing_formatter: bool,
1516
selected_formatters: &Option<Vec<String>>,
1617
) -> anyhow::Result<()> {
1718
let proj_dirs = match ProjectDirs::from("com", "NumTide", "treefmt") {
@@ -57,6 +58,7 @@ pub fn format_cmd(
5758
no_cache,
5859
clear_cache,
5960
fail_on_change,
61+
allow_missing_formatter,
6062
selected_formatters,
6163
)?;
6264

src/command/mod.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@ pub struct Cli {
4141
)]
4242
pub fail_on_change: bool,
4343

44+
/// Do not exit with error if a configured formatter is missing
45+
#[structopt(long = "allow-missing-formatter")]
46+
pub allow_missing_formatter: bool,
47+
4448
/// Log verbosity is based off the number of v used
4549
#[structopt(long = "verbose", short = "v", parse(from_occurrences))]
4650
pub verbosity: u8,
@@ -133,6 +137,7 @@ pub fn run_cli(cli: &Cli) -> anyhow::Result<()> {
133137
cli.no_cache,
134138
cli.clear_cache,
135139
cli.fail_on_change,
140+
cli.allow_missing_formatter,
136141
&cli.formatters,
137142
)?
138143
}

src/engine.rs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ pub fn run_treefmt(
2929
no_cache: bool,
3030
clear_cache: bool,
3131
fail_on_change: bool,
32+
allow_missing_formatter: bool,
3233
selected_formatters: &Option<Vec<String>>,
3334
) -> anyhow::Result<()> {
3435
assert!(tree_root.is_absolute());
@@ -82,10 +83,13 @@ pub fn run_treefmt(
8283

8384
timed_debug("load config");
8485

85-
// Load all the formatter instances from the config. Ignore the ones that failed.
86+
// Load all the formatter instances from the config.
87+
let mut expected_count = 0;
88+
8689
let formatters = project_config.formatter.into_iter().fold(
8790
BTreeMap::new(),
8891
|mut sum, (name, mut fmt_config)| {
92+
expected_count += 1;
8993
fmt_config.excludes.extend_from_slice(&global_excludes);
9094
match Formatter::from_config(tree_root, &name, &fmt_config) {
9195
Ok(fmt_matcher) => match selected_formatters {
@@ -98,14 +102,25 @@ pub fn run_treefmt(
98102
sum.insert(fmt_matcher.name.clone(), fmt_matcher);
99103
}
100104
},
101-
Err(err) => error!("Ignoring formatter #{} due to error: {}", name, err),
105+
Err(err) => {
106+
if allow_missing_formatter {
107+
error!("Ignoring formatter #{} due to error: {}", name, err)
108+
} else {
109+
error!("Failed to load formatter #{} due to error: {}", name, err)
110+
}
111+
}
102112
};
103113
sum
104114
},
105115
);
106116

107117
timed_debug("load formatters");
108118

119+
// Check the number of configured formatters matches the number of formatters loaded
120+
if !(allow_missing_formatter || formatters.len() == expected_count) {
121+
return Err(anyhow!("One or more formatters are missing"));
122+
}
123+
109124
// Load the eval cache
110125
let mut cache = if no_cache || clear_cache {
111126
// Start with an empty cache

0 commit comments

Comments
 (0)