cli: add --no-cache option (#157)

This is useful for CI where you don't care about eval caching. And avoid
the issue described in #156.
This commit is contained in:
Jonas Chevalier 2022-05-02 19:26:56 +02:00 committed by GitHub
parent 51423833dc
commit dfecb1d6cb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 29 additions and 11 deletions

View File

@ -47,10 +47,11 @@ USAGE:
treefmt [FLAGS] [OPTIONS] [paths]...
FLAGS:
--clear-cache Clear the evaluation cache. Use in case the cache is not precise enough
--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

View File

@ -10,6 +10,7 @@ pub fn format_cmd(
work_dir: &Path,
config_file: &Path,
paths: &[PathBuf],
no_cache: bool,
clear_cache: bool,
fail_on_change: bool,
) -> anyhow::Result<()> {
@ -55,6 +56,7 @@ pub fn format_cmd(
&cache_dir,
config_file,
&paths,
no_cache,
clear_cache,
fail_on_change,
)?;

View File

@ -25,7 +25,11 @@ pub struct Cli {
#[structopt(long = "stdin", conflicts_with("init"))]
pub stdin: bool,
/// Clear the evaluation cache. Use in case the cache is not precise enough.
/// Ignore the evaluation cache entirely. Useful for CI.
#[structopt(long = "no-cache", conflicts_with("stdin"), conflicts_with("init"))]
pub no_cache: bool,
/// Reset the evaluation cache. Use in case the cache is not precise enough.
#[structopt(long = "clear-cache", conflicts_with("stdin"), conflicts_with("init"))]
pub clear_cache: bool,
@ -122,6 +126,7 @@ pub fn run_cli(cli: &Cli) -> anyhow::Result<()> {
.as_ref()
.expect("presence asserted in ::cli_from_args"),
&cli.paths,
cli.no_cache,
cli.clear_cache,
cli.fail_on_change,
)?

View File

@ -26,6 +26,7 @@ pub fn run_treefmt(
cache_dir: &Path,
treefmt_toml: &Path,
paths: &[PathBuf],
no_cache: bool,
clear_cache: bool,
fail_on_change: bool,
) -> anyhow::Result<()> {
@ -98,15 +99,18 @@ pub fn run_treefmt(
timed_debug("load formatters");
// Load the eval cache
let mut cache = if clear_cache {
let mut cache = if no_cache || clear_cache {
// Start with an empty cache
CacheManifest::default()
} else {
CacheManifest::load(cache_dir, treefmt_toml)
};
timed_debug("load cache");
// Insert the new formatter configs
cache.update_formatters(formatters.clone());
if !no_cache {
// Insert the new formatter configs
cache.update_formatters(formatters.clone());
}
// Configure the tree walker
let walker = {
@ -168,7 +172,11 @@ pub fn run_treefmt(
timed_debug("tree walk");
// Filter out all of the paths that were already in the cache
let matches = cache.filter_matches(matches);
let matches = if !no_cache {
cache.filter_matches(matches)
} else {
matches
};
timed_debug("filter_matches");
@ -219,11 +227,13 @@ pub fn run_treefmt(
.collect::<anyhow::Result<BTreeMap<FormatterName, BTreeMap<PathBuf, FileMeta>>>>()?;
timed_debug("format");
// Record the new matches in the cache
cache.add_results(new_matches.clone());
// And write to disk
cache.write(cache_dir, treefmt_toml);
timed_debug("write cache");
if !no_cache {
// Record the new matches in the cache
cache.add_results(new_matches.clone());
// And write to disk
cache.write(cache_dir, treefmt_toml);
timed_debug("write cache");
}
// Diff the old matches with the new matches
let changed_matches: BTreeMap<FormatterName, Vec<PathBuf>> =