Skip to content

Commit 475aae1

Browse files
committed
Rewatch: show warnings in completion output
1 parent c8d05be commit 475aae1

3 files changed

Lines changed: 110 additions & 43 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(
@@ -245,7 +279,7 @@ pub fn incremental_build(
245279
only_incremental: bool,
246280
create_sourcedirs: bool,
247281
plain_output: bool,
248-
) -> Result<(), IncrementalBuildError> {
282+
) -> Result<IncrementalBuildResult, IncrementalBuildError> {
249283
let build_folder = build_state.root_folder.to_string_lossy().to_string();
250284

251285
let _lock = get_lock_or_exit(LockKind::Build, &build_folder);
@@ -263,7 +297,7 @@ pub fn incremental_build(
263297
ProgressStyle::with_template(&format!(
264298
"{} {}Parsing... {{spinner}} {{pos}}/{{len}} {{msg}}",
265299
format_step(current_step, total_steps),
266-
CODE
300+
PARSE
267301
))
268302
.unwrap(),
269303
);
@@ -313,13 +347,14 @@ pub fn incremental_build(
313347
"{}{} {}Parsed {} source files in {:.2}s",
314348
LINE_CLEAR,
315349
format_step(current_step, total_steps),
316-
CODE,
350+
PARSE,
317351
num_dirty_modules,
318352
default_timing.unwrap_or(timing_parse_total).as_secs_f64()
319353
);
320354
}
321355
}
322-
if helpers::contains_ascii_characters(&parse_warnings) {
356+
let has_parse_warnings = has_output(&parse_warnings);
357+
if has_parse_warnings {
323358
eprintln!("{}", &parse_warnings);
324359
}
325360

@@ -388,13 +423,13 @@ pub fn incremental_build(
388423
);
389424
}
390425
}
391-
if helpers::contains_ascii_characters(&compile_warnings) {
426+
if has_output(&compile_warnings) {
392427
eprintln!("{}", &compile_warnings);
393428
}
394429
if initial_build {
395430
log_config_warnings(build_state);
396431
}
397-
if helpers::contains_ascii_characters(&compile_errors) {
432+
if has_output(&compile_errors) {
398433
eprintln!("{}", &compile_errors);
399434
}
400435

@@ -405,6 +440,10 @@ pub fn incremental_build(
405440
plain_output,
406441
})
407442
} else {
443+
let has_compile_warnings = has_output(&compile_warnings);
444+
let has_config_warning_output = initial_build && has_config_warnings(build_state);
445+
let has_warnings = has_parse_warnings || has_compile_warnings || has_config_warning_output;
446+
408447
if show_progress {
409448
if plain_output {
410449
println!("Compiled {num_compiled_modules} modules")
@@ -420,7 +459,7 @@ pub fn incremental_build(
420459
}
421460
}
422461

423-
if helpers::contains_ascii_characters(&compile_warnings) {
462+
if has_compile_warnings {
424463
eprintln!("{}", &compile_warnings);
425464
}
426465
if initial_build {
@@ -431,7 +470,7 @@ pub fn incremental_build(
431470
write_compiler_info(build_state);
432471

433472
let _lock = drop_lock(LockKind::Build, &build_folder);
434-
Ok(())
473+
Ok(IncrementalBuildResult { has_warnings })
435474
}
436475
}
437476

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

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: 25 additions & 23 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,21 +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)
446+
if let Ok(result) = result {
447+
if let Some(a) = after_build.clone() {
448+
cmd::run(a)
449+
}
450+
451+
let timing_total_elapsed = timing_total.elapsed();
452+
if !plain_output && show_progress {
453+
println!(
454+
"\n{}\n",
455+
build::format_finished_compilation_message(
456+
None,
457+
result.has_warnings,
458+
timing_total_elapsed,
459+
)
460+
);
461+
}
450462
}
451463

452464
build::write_build_ninja(&build_state);
453-
454-
let timing_total_elapsed = timing_total.elapsed();
455-
if !plain_output && show_progress {
456-
println!(
457-
"\n{}{}Finished compilation in {:.2}s\n",
458-
LINE_CLEAR,
459-
SPARKLES,
460-
timing_total_elapsed.as_secs_f64()
461-
);
462-
}
463465
needs_compile_type = CompileType::None;
464466
initial_build = false;
465467
}

0 commit comments

Comments
 (0)