Skip to content

Commit 9dd5fc1

Browse files
committed
Rewatch: show warnings in completion output
1 parent 8436751 commit 9dd5fc1

3 files changed

Lines changed: 108 additions & 45 deletions

File tree

rewatch/src/build.rs

Lines changed: 78 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,40 @@ pub struct CompilerArgs {
5656
pub parser_args: Vec<String>,
5757
}
5858

59+
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
60+
pub struct IncrementalBuildResult {
61+
pub has_warnings: bool,
62+
}
63+
64+
fn has_output(output: &str) -> bool {
65+
helpers::contains_ascii_characters(output)
66+
}
67+
68+
fn has_config_warnings(build_state: &BuildCommandState) -> bool {
69+
build_state.packages.iter().any(|(_, package)| {
70+
package.is_local_dep
71+
&& (!package.config.get_unsupported_fields().is_empty()
72+
|| !package.config.get_unknown_fields().is_empty())
73+
})
74+
}
75+
76+
pub fn format_finished_compilation_message(
77+
compilation_kind: Option<&str>,
78+
has_warnings: bool,
79+
duration: Duration,
80+
) -> String {
81+
let compilation_kind = compilation_kind
82+
.map(|kind| format!("{kind} "))
83+
.unwrap_or_default();
84+
let status = if has_warnings { WARNING } else { CHECKMARK };
85+
let warning_suffix = if has_warnings { " with warnings" } else { "" };
86+
87+
format!(
88+
"{LINE_CLEAR}{status}Finished {compilation_kind}compilation{warning_suffix} in {:.2}s",
89+
duration.as_secs_f64()
90+
)
91+
}
92+
5993
pub fn get_compiler_args(rescript_file_path: &Path) -> Result<String> {
6094
let filename = &helpers::get_abs_path(rescript_file_path);
6195
let current_package = helpers::get_abs_path(
@@ -246,7 +280,7 @@ pub fn incremental_build(
246280
only_incremental: bool,
247281
create_sourcedirs: bool,
248282
plain_output: bool,
249-
) -> Result<(), IncrementalBuildError> {
283+
) -> Result<IncrementalBuildResult, IncrementalBuildError> {
250284
let build_folder = build_state.root_folder.to_string_lossy().to_string();
251285

252286
let _lock = get_lock_or_exit(LockKind::Build, &build_folder);
@@ -264,7 +298,7 @@ pub fn incremental_build(
264298
ProgressStyle::with_template(&format!(
265299
"{} {}Parsing... {{spinner}} {{pos}}/{{len}} {{msg}}",
266300
format_step(current_step, total_steps),
267-
CODE
301+
PARSE
268302
))
269303
.unwrap(),
270304
);
@@ -314,13 +348,14 @@ pub fn incremental_build(
314348
"{}{} {}Parsed {} source files in {:.2}s",
315349
LINE_CLEAR,
316350
format_step(current_step, total_steps),
317-
CODE,
351+
PARSE,
318352
num_dirty_modules,
319353
default_timing.unwrap_or(timing_parse_total).as_secs_f64()
320354
);
321355
}
322356
}
323-
if helpers::contains_ascii_characters(&parse_warnings) {
357+
let has_parse_warnings = has_output(&parse_warnings);
358+
if has_parse_warnings {
324359
eprintln!("{}", &parse_warnings);
325360
}
326361

@@ -389,13 +424,13 @@ pub fn incremental_build(
389424
);
390425
}
391426
}
392-
if helpers::contains_ascii_characters(&compile_warnings) {
427+
if has_output(&compile_warnings) {
393428
eprintln!("{}", &compile_warnings);
394429
}
395430
if initial_build {
396431
log_config_warnings(build_state);
397432
}
398-
if helpers::contains_ascii_characters(&compile_errors) {
433+
if has_output(&compile_errors) {
399434
eprintln!("{}", &compile_errors);
400435
}
401436

@@ -406,6 +441,10 @@ pub fn incremental_build(
406441
plain_output,
407442
})
408443
} else {
444+
let has_compile_warnings = has_output(&compile_warnings);
445+
let has_config_warning_output = initial_build && has_config_warnings(build_state);
446+
let has_warnings = has_parse_warnings || has_compile_warnings || has_config_warning_output;
447+
409448
if show_progress {
410449
if plain_output {
411450
println!("Compiled {num_compiled_modules} modules")
@@ -421,7 +460,7 @@ pub fn incremental_build(
421460
}
422461
}
423462

424-
if helpers::contains_ascii_characters(&compile_warnings) {
463+
if has_compile_warnings {
425464
eprintln!("{}", &compile_warnings);
426465
}
427466
if initial_build {
@@ -432,7 +471,7 @@ pub fn incremental_build(
432471
write_compiler_info(build_state);
433472

434473
let _lock = drop_lock(LockKind::Build, &build_folder);
435-
Ok(())
474+
Ok(IncrementalBuildResult { has_warnings })
436475
}
437476
}
438477

@@ -519,14 +558,16 @@ pub fn build(
519558
create_sourcedirs,
520559
plain_output,
521560
) {
522-
Ok(_) => {
561+
Ok(result) => {
523562
if !plain_output && show_progress {
524563
let timing_total_elapsed = timing_total.elapsed();
525564
println!(
526-
"\n{}{}Finished Compilation in {:.2}s",
527-
LINE_CLEAR,
528-
SPARKLES,
529-
default_timing.unwrap_or(timing_total_elapsed).as_secs_f64()
565+
"\n{}",
566+
format_finished_compilation_message(
567+
None,
568+
result.has_warnings,
569+
default_timing.unwrap_or(timing_total_elapsed),
570+
)
530571
);
531572
}
532573
clean::cleanup_after_build(&build_state);
@@ -540,3 +581,27 @@ pub fn build(
540581
}
541582
}
542583
}
584+
585+
#[cfg(test)]
586+
mod tests {
587+
use super::*;
588+
589+
#[test]
590+
fn formats_successful_completion_message() {
591+
assert_eq!(
592+
format_finished_compilation_message(None, false, Duration::from_millis(1500)),
593+
format!("{LINE_CLEAR}{}Finished compilation in 1.50s", CHECKMARK)
594+
);
595+
}
596+
597+
#[test]
598+
fn formats_warning_completion_message() {
599+
assert_eq!(
600+
format_finished_compilation_message(Some("incremental"), true, Duration::from_millis(1500)),
601+
format!(
602+
"{LINE_CLEAR}{}Finished incremental compilation with warnings in 1.50s",
603+
WARNING
604+
)
605+
);
606+
}
607+
}

rewatch/src/helpers.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,13 @@ pub mod deserialize;
1818

1919
pub mod emojis {
2020
use console::Emoji;
21-
pub static COMMAND: Emoji<'_, '_> = Emoji("🏃 ", "");
22-
pub static SWEEP: Emoji<'_, '_> = Emoji("🧹 ", "");
23-
pub static CODE: Emoji<'_, '_> = Emoji("🧱 ", "");
24-
pub static SWORDS: Emoji<'_, '_> = Emoji("🤺 ", "");
25-
pub static CHECKMARK: Emoji<'_, '_> = Emoji("✅ ", "");
26-
pub static CROSS: Emoji<'_, '_> = Emoji(" ", "");
27-
pub static SPARKLES: Emoji<'_, '_> = Emoji(" ", "");
21+
pub static COMMAND: Emoji<'_, '_> = Emoji("🏃 ", "[run] ");
22+
pub static SWEEP: Emoji<'_, '_> = Emoji("🧹 ", "[clean] ");
23+
pub static PARSE: Emoji<'_, '_> = Emoji("🧱 ", "[parse] ");
24+
pub static SWORDS: Emoji<'_, '_> = Emoji("🤺 ", "[build] ");
25+
pub static CHECKMARK: Emoji<'_, '_> = Emoji("✅ ", "[ok] ");
26+
pub static WARNING: Emoji<'_, '_> = Emoji("⚠️ ", "[warn] ");
27+
pub static CROSS: Emoji<'_, '_> = Emoji(" ", "[error] ");
2828
pub static LINE_CLEAR: &str = "\x1b[2K\r";
2929
}
3030

rewatch/src/watcher.rs

Lines changed: 23 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ use crate::cmd;
55
use crate::config;
66
use crate::helpers;
77
use crate::helpers::StrippedVerbatimPath;
8-
use crate::helpers::emojis::*;
98
use crate::lock::LockKind;
109
use crate::queue::FifoQueue;
1110
use crate::queue::*;
@@ -379,17 +378,15 @@ async fn async_watch(
379378
match needs_compile_type {
380379
CompileType::Incremental => {
381380
let timing_total = Instant::now();
382-
if build::incremental_build(
381+
if let Ok(result) = build::incremental_build(
383382
&mut build_state,
384383
None,
385384
initial_build,
386385
show_progress,
387386
!initial_build,
388387
create_sourcedirs,
389388
plain_output,
390-
)
391-
.is_ok()
392-
{
389+
) {
393390
if let Some(a) = after_build.clone() {
394391
cmd::run(a)
395392
}
@@ -400,11 +397,12 @@ async fn async_watch(
400397
println!("Finished {compilation_type} compilation")
401398
} else {
402399
println!(
403-
"\n{}{}Finished {} compilation in {:.2}s\n",
404-
LINE_CLEAR,
405-
SPARKLES,
406-
compilation_type,
407-
timing_total_elapsed.as_secs_f64()
400+
"\n{}\n",
401+
build::format_finished_compilation_message(
402+
Some(compilation_type),
403+
result.has_warnings,
404+
timing_total_elapsed,
405+
)
408406
);
409407
}
410408
}
@@ -436,7 +434,7 @@ async fn async_watch(
436434
current_watch_paths = compute_watch_paths(&build_state, path);
437435
register_watches(watcher, &current_watch_paths);
438436

439-
let _ = build::incremental_build(
437+
let result = build::incremental_build(
440438
&mut build_state,
441439
None,
442440
initial_build,
@@ -445,25 +443,25 @@ async fn async_watch(
445443
create_sourcedirs,
446444
plain_output,
447445
);
448-
if let Some(a) = after_build.clone() {
449-
cmd::run(a)
450-
}
451-
452-
build::write_build_ninja(&build_state);
446+
if let Ok(result) = result {
447+
if let Some(a) = after_build.clone() {
448+
cmd::run(a)
449+
}
453450

454-
let timing_total_elapsed = timing_total.elapsed();
455-
if show_progress {
456-
if plain_output {
457-
println!("Finished compilation")
458-
} else {
451+
let timing_total_elapsed = timing_total.elapsed();
452+
if !plain_output && show_progress {
459453
println!(
460-
"\n{}{}Finished compilation in {:.2}s\n",
461-
LINE_CLEAR,
462-
SPARKLES,
463-
timing_total_elapsed.as_secs_f64()
454+
"\n{}\n",
455+
build::format_finished_compilation_message(
456+
None,
457+
result.has_warnings,
458+
timing_total_elapsed,
459+
)
464460
);
465461
}
466462
}
463+
464+
build::write_build_ninja(&build_state);
467465
needs_compile_type = CompileType::None;
468466
initial_build = false;
469467
}

0 commit comments

Comments
 (0)