-
Notifications
You must be signed in to change notification settings - Fork 5.8k
feat(fmt): add --fail-fast flag to deno fmt --check #31438
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 2 commits
4c90f14
30b1a31
cfd075b
330aae6
94a3d51
1dab95b
42c72d1
b579bcd
45a550e
8a207af
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -227,7 +227,7 @@ async fn format_files( | |
| paths_with_options_batches: Vec<PathsWithOptions>, | ||
| ) -> Result<(), AnyError> { | ||
| let formatter: Box<dyn Formatter> = if fmt_flags.check { | ||
| Box::new(CheckFormatter::default()) | ||
| Box::new(CheckFormatter::new(fmt_flags.fail_fast)) | ||
| } else { | ||
| Box::new(RealFormatter::default()) | ||
| }; | ||
|
|
@@ -939,10 +939,22 @@ trait Formatter { | |
| fn finish(&self) -> Result<(), AnyError>; | ||
| } | ||
|
|
||
| #[derive(Default)] | ||
| struct CheckFormatter { | ||
| not_formatted_files_count: Arc<AtomicUsize>, | ||
| checked_files_count: Arc<AtomicUsize>, | ||
| fail_fast: bool, | ||
| found_error: Arc<std::sync::atomic::AtomicBool>, | ||
|
||
| } | ||
|
|
||
| impl CheckFormatter { | ||
| fn new(fail_fast: bool) -> Self { | ||
| Self { | ||
| not_formatted_files_count: Arc::new(AtomicUsize::new(0)), | ||
| checked_files_count: Arc::new(AtomicUsize::new(0)), | ||
| fail_fast, | ||
| found_error: Arc::new(std::sync::atomic::AtomicBool::new(false)), | ||
| } | ||
| } | ||
| } | ||
|
|
||
| #[async_trait] | ||
|
|
@@ -961,7 +973,14 @@ impl Formatter for CheckFormatter { | |
| run_parallelized(paths, { | ||
| let not_formatted_files_count = self.not_formatted_files_count.clone(); | ||
| let checked_files_count = self.checked_files_count.clone(); | ||
| let fail_fast = self.fail_fast; | ||
| let found_error = self.found_error.clone(); | ||
| move |file_path| { | ||
| // Early exit if fail-fast is enabled and we've already found an error | ||
| if fail_fast && found_error.load(Ordering::Relaxed) { | ||
| return Ok(()); | ||
| } | ||
|
|
||
| checked_files_count.fetch_add(1, Ordering::Relaxed); | ||
| let file = read_file_contents(&file_path)?; | ||
|
|
||
|
|
@@ -981,6 +1000,7 @@ impl Formatter for CheckFormatter { | |
| ) { | ||
| Ok(Some(formatted_text)) => { | ||
| not_formatted_files_count.fetch_add(1, Ordering::Relaxed); | ||
| found_error.store(true, Ordering::Relaxed); | ||
| let _g = output_lock.lock(); | ||
| let diff = | ||
| deno_resolver::display::diff(&file.text, &formatted_text); | ||
|
|
@@ -1001,6 +1021,7 @@ impl Formatter for CheckFormatter { | |
| } | ||
| Err(e) => { | ||
| not_formatted_files_count.fetch_add(1, Ordering::Relaxed); | ||
| found_error.store(true, Ordering::Relaxed); | ||
| let _g = output_lock.lock(); | ||
| warn!("Error checking: {}", file_path.to_string_lossy()); | ||
| warn!( | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would be good for this to also be
fail_fastwhencli_options.is_quiet().There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @dsherret — updated this as well.
fail_fast is now enabled when either --fail-fast is passed or cli_options.is_quiet() is true.
Let me know if you'd prefer any tweaks!