diff --git a/CHANGELOG.md b/CHANGELOG.md index 32e3259da0..322688fd29 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -24,6 +24,8 @@ #### :nail_care: Polish +- Rewatch: plain output when not running in tty. https://github.com/rescript-lang/rescript/pull/7970 + #### :house: Internal # 12.0.0-rc.2 diff --git a/rewatch/src/build.rs b/rewatch/src/build.rs index a06a84b168..e12c121ea3 100644 --- a/rewatch/src/build.rs +++ b/rewatch/src/build.rs @@ -131,13 +131,13 @@ pub fn initialize_build( filter: &Option, show_progress: bool, path: &Path, - snapshot_output: bool, + plain_output: bool, warn_error: Option, ) -> Result { let project_context = ProjectContext::new(path)?; let compiler = get_compiler_info(&project_context)?; - if !snapshot_output && show_progress { + if !plain_output && show_progress { print!("{} {}Building package tree...", style("[1/7]").bold().dim(), TREE); let _ = stdout().flush(); } @@ -149,7 +149,7 @@ pub fn initialize_build( let compiler_check = verify_compiler_info(&packages, &compiler); if show_progress { - if snapshot_output { + if plain_output { if let CompilerCheckResult::CleanedPackagesDueToCompiler = compiler_check { // Snapshot-friendly output (no progress prefixes or emojis) println!("Cleaned previous build due to compiler update"); @@ -181,7 +181,7 @@ pub fn initialize_build( let timing_source_files = Instant::now(); - if !snapshot_output && show_progress { + if !plain_output && show_progress { print!( "{} {}Finding source files...", style("[2/7]").bold().dim(), @@ -194,7 +194,7 @@ pub fn initialize_build( packages::parse_packages(&mut build_state); let timing_source_files_elapsed = timing_source_files.elapsed(); - if !snapshot_output && show_progress { + if !plain_output && show_progress { println!( "{}{} {}Found source files in {:.2}s", LINE_CLEAR, @@ -216,7 +216,7 @@ pub fn initialize_build( let compile_assets_state = read_compile_state::read(&mut build_state)?; let timing_compile_state_elapsed = timing_compile_state.elapsed(); - if !snapshot_output && show_progress { + if !plain_output && show_progress { println!( "{}{} {}Read compile state {:.2}s", LINE_CLEAR, @@ -238,7 +238,7 @@ pub fn initialize_build( let timing_cleanup_elapsed = timing_cleanup.elapsed(); if show_progress { - if snapshot_output { + if plain_output { println!("Cleaned {diff_cleanup}/{total_cleanup}") } else { println!( @@ -268,7 +268,7 @@ pub enum IncrementalBuildErrorKind { #[derive(Debug, Clone)] pub struct IncrementalBuildError { - pub snapshot_output: bool, + pub plain_output: bool, pub kind: IncrementalBuildErrorKind, } @@ -276,21 +276,21 @@ impl fmt::Display for IncrementalBuildError { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { match &self.kind { IncrementalBuildErrorKind::SourceFileParseError => { - if self.snapshot_output { + if self.plain_output { write!(f, "{LINE_CLEAR} Could not parse Source Files",) } else { write!(f, "{LINE_CLEAR} {CROSS}Could not parse Source Files",) } } IncrementalBuildErrorKind::CompileError(Some(e)) => { - if self.snapshot_output { + if self.plain_output { write!(f, "{LINE_CLEAR} Failed to Compile. Error: {e}",) } else { write!(f, "{LINE_CLEAR} {CROSS}Failed to Compile. Error: {e}",) } } IncrementalBuildErrorKind::CompileError(None) => { - if self.snapshot_output { + if self.plain_output { write!(f, "{LINE_CLEAR} Failed to Compile. See Errors Above",) } else { write!(f, "{LINE_CLEAR} {CROSS}Failed to Compile. See Errors Above",) @@ -307,11 +307,11 @@ pub fn incremental_build( show_progress: bool, only_incremental: bool, create_sourcedirs: bool, - snapshot_output: bool, + plain_output: bool, ) -> Result<(), IncrementalBuildError> { logs::initialize(&build_state.packages); let num_dirty_modules = build_state.modules.values().filter(|m| is_dirty(m)).count() as u64; - let pb = if !snapshot_output && show_progress { + let pb = if !plain_output && show_progress { ProgressBar::new(num_dirty_modules) } else { ProgressBar::hidden() @@ -334,7 +334,7 @@ pub fn incremental_build( match result_asts { Ok(_ast) => { if show_progress { - if snapshot_output { + if plain_output { println!("Parsed {num_dirty_modules} source files") } else { println!( @@ -352,7 +352,7 @@ pub fn incremental_build( Err(err) => { logs::finalize(&build_state.packages); - if !snapshot_output && show_progress { + if !plain_output && show_progress { println!( "{}{} {}Error parsing source files in {:.2}s", LINE_CLEAR, @@ -366,7 +366,7 @@ pub fn incremental_build( println!("{}", &err); return Err(IncrementalBuildError { kind: IncrementalBuildErrorKind::SourceFileParseError, - snapshot_output, + plain_output, }); } } @@ -376,7 +376,7 @@ pub fn incremental_build( let timing_deps_elapsed = timing_deps.elapsed(); current_step += 1; - if !snapshot_output && show_progress { + if !plain_output && show_progress { println!( "{}{} {}Collected deps in {:.2}s", LINE_CLEAR, @@ -400,7 +400,7 @@ pub fn incremental_build( }; let start_compiling = Instant::now(); - let pb = if !snapshot_output && show_progress { + let pb = if !plain_output && show_progress { ProgressBar::new(build_state.modules.len().try_into().unwrap()) } else { ProgressBar::hidden() @@ -422,7 +422,7 @@ pub fn incremental_build( ) .map_err(|e| IncrementalBuildError { kind: IncrementalBuildErrorKind::CompileError(Some(e.to_string())), - snapshot_output, + plain_output, })?; let compile_duration = start_compiling.elapsed(); @@ -434,7 +434,7 @@ pub fn incremental_build( pb.finish(); if !compile_errors.is_empty() { if show_progress { - if snapshot_output { + if plain_output { println!("Compiled {num_compiled_modules} modules") } else { println!( @@ -458,11 +458,11 @@ pub fn incremental_build( } Err(IncrementalBuildError { kind: IncrementalBuildErrorKind::CompileError(None), - snapshot_output, + plain_output, }) } else { if show_progress { - if snapshot_output { + if plain_output { println!("Compiled {num_compiled_modules} modules") } else { println!( @@ -539,7 +539,7 @@ pub fn build( show_progress: bool, no_timing: bool, create_sourcedirs: bool, - snapshot_output: bool, + plain_output: bool, warn_error: Option, ) -> Result { let default_timing: Option = if no_timing { @@ -553,7 +553,7 @@ pub fn build( filter, show_progress, path, - snapshot_output, + plain_output, warn_error, ) .map_err(|e| anyhow!("Could not initialize build. Error: {e}"))?; @@ -565,10 +565,10 @@ pub fn build( show_progress, false, create_sourcedirs, - snapshot_output, + plain_output, ) { Ok(_) => { - if !snapshot_output && show_progress { + if !plain_output && show_progress { let timing_total_elapsed = timing_total.elapsed(); println!( "\n{}{}Finished Compilation in {:.2}s", diff --git a/rewatch/src/build/clean.rs b/rewatch/src/build/clean.rs index 7a360f7f64..8d63db7ef6 100644 --- a/rewatch/src/build/clean.rs +++ b/rewatch/src/build/clean.rs @@ -331,13 +331,13 @@ pub fn cleanup_after_build(build_state: &BuildCommandState) { }); } -pub fn clean(path: &Path, show_progress: bool, snapshot_output: bool) -> Result<()> { +pub fn clean(path: &Path, show_progress: bool, plain_output: bool) -> Result<()> { let project_context = ProjectContext::new(path)?; let compiler_info = build::get_compiler_info(&project_context)?; let packages = packages::make(&None, &project_context, show_progress)?; let timing_clean_compiler_assets = Instant::now(); - if !snapshot_output && show_progress { + if !plain_output && show_progress { print!( "{} {}Cleaning compiler assets...", style("[1/2]").bold().dim(), @@ -347,12 +347,12 @@ pub fn clean(path: &Path, show_progress: bool, snapshot_output: bool) -> Result< }; for (_, package) in &packages { - clean_package(show_progress, snapshot_output, package) + clean_package(show_progress, plain_output, package) } let timing_clean_compiler_assets_elapsed = timing_clean_compiler_assets.elapsed(); - if !snapshot_output && show_progress { + if !plain_output && show_progress { println!( "{}{} {}Cleaned compiler assets in {:.2}s", LINE_CLEAR, @@ -367,7 +367,7 @@ pub fn clean(path: &Path, show_progress: bool, snapshot_output: bool) -> Result< let mut build_state = BuildState::new(project_context, packages, compiler_info); packages::parse_packages(&mut build_state); let root_config = build_state.get_root_config(); - let suffix_for_print = if snapshot_output || !show_progress { + let suffix_for_print = if plain_output || !show_progress { String::new() } else { match root_config.package_specs { @@ -390,7 +390,7 @@ pub fn clean(path: &Path, show_progress: bool, snapshot_output: bool) -> Result< } }; - if !snapshot_output && show_progress { + if !plain_output && show_progress { println!( "{} {}Cleaning {} files...", style("[2/2]").bold().dim(), @@ -403,7 +403,7 @@ pub fn clean(path: &Path, show_progress: bool, snapshot_output: bool) -> Result< clean_source_files(&build_state, root_config); let timing_clean_mjs_elapsed = timing_clean_mjs.elapsed(); - if !snapshot_output && show_progress { + if !plain_output && show_progress { println!( "{}{} {}Cleaned {} files in {:.2}s", LINE_CLEAR, @@ -418,9 +418,9 @@ pub fn clean(path: &Path, show_progress: bool, snapshot_output: bool) -> Result< Ok(()) } -pub fn clean_package(show_progress: bool, snapshot_output: bool, package: &Package) { +pub fn clean_package(show_progress: bool, plain_output: bool, package: &Package) { if show_progress { - if snapshot_output { + if plain_output { println!("Cleaning {}", package.name) } else { print!( diff --git a/rewatch/src/cli.rs b/rewatch/src/cli.rs index 88e9ed8ba4..0e95dad98d 100644 --- a/rewatch/src/cli.rs +++ b/rewatch/src/cli.rs @@ -219,13 +219,6 @@ pub struct DevArg { pub dev: bool, } -#[derive(Args, Debug, Clone, Copy)] -pub struct SnapshotOutputArg { - /// simple output for snapshot testing - #[arg(short, long, default_value = "false", num_args = 0..=1)] - pub snapshot_output: bool, -} - #[derive(Args, Debug, Clone)] pub struct BuildArgs { #[command(flatten)] @@ -247,9 +240,6 @@ pub struct BuildArgs { #[arg(short, long, default_value_t = false, num_args = 0..=1)] pub no_timing: bool, - #[command(flatten)] - pub snapshot_output: SnapshotOutputArg, - /// Watch mode (deprecated, use `rescript watch` instead) #[arg(short, default_value_t = false, num_args = 0..=1)] pub watch: bool, @@ -418,9 +408,6 @@ pub struct WatchArgs { #[command(flatten)] pub dev: DevArg, - #[command(flatten)] - pub snapshot_output: SnapshotOutputArg, - /// Warning numbers and whether to turn them into errors /// /// This flag overrides any warning configuration in rescript.json. @@ -438,7 +425,6 @@ impl From for WatchArgs { after_build: build_args.after_build, create_sourcedirs: build_args.create_sourcedirs, dev: build_args.dev, - snapshot_output: build_args.snapshot_output, warn_error: build_args.warn_error, } } @@ -455,9 +441,6 @@ pub enum Command { #[command(flatten)] folder: FolderArg, - #[command(flatten)] - snapshot_output: SnapshotOutputArg, - #[command(flatten)] dev: DevArg, }, @@ -531,11 +514,3 @@ impl Deref for DevArg { &self.dev } } - -impl Deref for SnapshotOutputArg { - type Target = bool; - - fn deref(&self) -> &Self::Target { - &self.snapshot_output - } -} diff --git a/rewatch/src/main.rs b/rewatch/src/main.rs index c54844da3e..01eb750599 100644 --- a/rewatch/src/main.rs +++ b/rewatch/src/main.rs @@ -1,4 +1,5 @@ use anyhow::Result; +use console::Term; use log::LevelFilter; use std::{io::Write, path::Path}; @@ -24,6 +25,9 @@ fn main() -> Result<()> { } } + let is_tty: bool = Term::stdout().is_term() && Term::stderr().is_term(); + let plain_output = !is_tty; + // The 'normal run' mode will show the 'pretty' formatted progress. But if we turn off the log // level, we should never show that. let show_progress = log_level_filter == LevelFilter::Info; @@ -48,7 +52,7 @@ fn main() -> Result<()> { show_progress, build_args.no_timing, *build_args.create_sourcedirs, - *build_args.snapshot_output, + plain_output, build_args.warn_error.clone(), ) { Err(e) => { @@ -78,17 +82,13 @@ fn main() -> Result<()> { &watch_args.folder, (*watch_args.after_build).clone(), *watch_args.create_sourcedirs, - *watch_args.snapshot_output, + plain_output, watch_args.warn_error.clone(), ); Ok(()) } - cli::Command::Clean { - folder, - snapshot_output, - dev, - } => { + cli::Command::Clean { folder, dev } => { let _lock = get_lock(&folder); if dev.dev { @@ -97,7 +97,7 @@ fn main() -> Result<()> { ); } - build::clean::clean(Path::new(&folder as &str), show_progress, *snapshot_output) + build::clean::clean(Path::new(&folder as &str), show_progress, plain_output) } cli::Command::Format { stdin, diff --git a/rewatch/src/watcher.rs b/rewatch/src/watcher.rs index 642d552fef..2ce6d53989 100644 --- a/rewatch/src/watcher.rs +++ b/rewatch/src/watcher.rs @@ -62,7 +62,7 @@ struct AsyncWatchArgs<'a> { filter: &'a Option, after_build: Option, create_sourcedirs: bool, - snapshot_output: bool, + plain_output: bool, warn_error: Option, } @@ -74,12 +74,12 @@ async fn async_watch( filter, after_build, create_sourcedirs, - snapshot_output, + plain_output, warn_error, }: AsyncWatchArgs<'_>, ) -> notify::Result<()> { let mut build_state: build::build_types::BuildCommandState = - build::initialize_build(None, filter, show_progress, path, snapshot_output, warn_error) + build::initialize_build(None, filter, show_progress, path, plain_output, warn_error) .expect("Can't initialize build"); let mut needs_compile_type = CompileType::Incremental; // create a mutex to capture if ctrl-c was pressed @@ -241,7 +241,7 @@ async fn async_watch( show_progress, !initial_build, create_sourcedirs, - snapshot_output, + plain_output, ) .is_ok() { @@ -251,7 +251,7 @@ async fn async_watch( let timing_total_elapsed = timing_total.elapsed(); if show_progress { let compilation_type = if initial_build { "initial" } else { "incremental" }; - if snapshot_output { + if plain_output { println!("Finished {compilation_type} compilation") } else { println!( @@ -274,7 +274,7 @@ async fn async_watch( filter, show_progress, path, - snapshot_output, + plain_output, build_state.get_warn_error_override(), ) .expect("Can't initialize build"); @@ -285,7 +285,7 @@ async fn async_watch( show_progress, false, create_sourcedirs, - snapshot_output, + plain_output, ); if let Some(a) = after_build.clone() { cmd::run(a) @@ -294,7 +294,7 @@ async fn async_watch( build::write_build_ninja(&build_state); let timing_total_elapsed = timing_total.elapsed(); - if !snapshot_output && show_progress { + if !plain_output && show_progress { println!( "\n{}{}Finished compilation in {:.2}s\n", LINE_CLEAR, @@ -321,7 +321,7 @@ pub fn start( folder: &str, after_build: Option, create_sourcedirs: bool, - snapshot_output: bool, + plain_output: bool, warn_error: Option, ) { futures::executor::block_on(async { @@ -347,7 +347,7 @@ pub fn start( filter, after_build, create_sourcedirs, - snapshot_output, + plain_output, warn_error: warn_error.clone(), }) .await diff --git a/rewatch/tests/clean.sh b/rewatch/tests/clean.sh index 84d57011e3..2f71b8d7ef 100755 --- a/rewatch/tests/clean.sh +++ b/rewatch/tests/clean.sh @@ -135,7 +135,7 @@ fi # Rebuild with snapshot output snapshot_file=../tests/snapshots/clean-rebuild.txt -rewatch build --snapshot-output &> $snapshot_file +rewatch build &> $snapshot_file build_status=$? normalize_paths $snapshot_file if [ $build_status -eq 0 ]; diff --git a/rewatch/tests/compile.sh b/rewatch/tests/compile.sh index 5f981610bc..73cbbb32cd 100755 --- a/rewatch/tests/compile.sh +++ b/rewatch/tests/compile.sh @@ -52,51 +52,51 @@ popd > /dev/null node ./packages/main/src/Main.mjs > ./packages/main/src/output.txt mv ./packages/main/src/Main.res ./packages/main/src/Main2.res -rewatch build --snapshot-output &> ../tests/snapshots/rename-file.txt +rewatch build &> ../tests/snapshots/rename-file.txt normalize_paths ../tests/snapshots/rename-file.txt mv ./packages/main/src/Main2.res ./packages/main/src/Main.res # Rename a file with a dependent - this should trigger an error mv ./packages/main/src/InternalDep.res ./packages/main/src/InternalDep2.res -rewatch build --snapshot-output &> ../tests/snapshots/rename-file-internal-dep.txt +rewatch build &> ../tests/snapshots/rename-file-internal-dep.txt normalize_paths ../tests/snapshots/rename-file-internal-dep.txt mv ./packages/main/src/InternalDep2.res ./packages/main/src/InternalDep.res # Rename a file with a dependent in a namespaced package - this should trigger an error (regression) mv ./packages/new-namespace/src/Other_module.res ./packages/new-namespace/src/Other_module2.res -rewatch build --snapshot-output &> ../tests/snapshots/rename-file-internal-dep-namespace.txt +rewatch build &> ../tests/snapshots/rename-file-internal-dep-namespace.txt normalize_paths ../tests/snapshots/rename-file-internal-dep-namespace.txt mv ./packages/new-namespace/src/Other_module2.res ./packages/new-namespace/src/Other_module.res rewatch build &> /dev/null mv ./packages/main/src/ModuleWithInterface.resi ./packages/main/src/ModuleWithInterface2.resi -rewatch build --snapshot-output &> ../tests/snapshots/rename-interface-file.txt +rewatch build &> ../tests/snapshots/rename-interface-file.txt normalize_paths ../tests/snapshots/rename-interface-file.txt mv ./packages/main/src/ModuleWithInterface2.resi ./packages/main/src/ModuleWithInterface.resi rewatch build &> /dev/null mv ./packages/main/src/ModuleWithInterface.res ./packages/main/src/ModuleWithInterface2.res -rewatch build --snapshot-output &> ../tests/snapshots/rename-file-with-interface.txt +rewatch build &> ../tests/snapshots/rename-file-with-interface.txt normalize_paths ../tests/snapshots/rename-file-with-interface.txt mv ./packages/main/src/ModuleWithInterface2.res ./packages/main/src/ModuleWithInterface.res rewatch build &> /dev/null # when deleting a file that other files depend on, the compile should fail rm packages/dep02/src/Dep02.res -rewatch build --snapshot-output &> ../tests/snapshots/remove-file.txt +rewatch build &> ../tests/snapshots/remove-file.txt normalize_paths ../tests/snapshots/remove-file.txt git checkout -- packages/dep02/src/Dep02.res rewatch build &> /dev/null # it should show an error when we have a dependency cycle echo 'Dep01.log()' >> packages/new-namespace/src/NS_alias.res -rewatch build --snapshot-output &> ../tests/snapshots/dependency-cycle.txt +rewatch build &> ../tests/snapshots/dependency-cycle.txt normalize_paths ../tests/snapshots/dependency-cycle.txt git checkout -- packages/new-namespace/src/NS_alias.res # this should not compile because "@rescript/webapi" is part of dev-dependencies # and FileToTest.res is not listed as "type":"dev" echo 'open WebAPI' >> packages/with-dev-deps/src/FileToTest.res -rewatch build --snapshot-output &> ../tests/snapshots/bs-dev-dependency-used-by-non-dev-source.txt +rewatch build &> ../tests/snapshots/bs-dev-dependency-used-by-non-dev-source.txt normalize_paths ../tests/snapshots/bs-dev-dependency-used-by-non-dev-source.txt git checkout -- packages/with-dev-deps/src/FileToTest.res