From 9d61308637c28dff86ab669df45ef4607ab91885 Mon Sep 17 00:00:00 2001 From: brightwu <1521488775@qq.com> Date: Wed, 11 Sep 2024 09:48:55 +0800 Subject: [PATCH] chore: do not exit when first compilation failed (#1764) Co-authored-by: ADNY <66500121+ErKeLost@users.noreply.github.com> --- crates/compiler/src/build/mod.rs | 25 +++++++++++++++++-- crates/compiler/src/lib.rs | 14 ++++++++--- .../src/update/handle_update_modules.rs | 18 +++++++++++++ crates/compiler/src/update/mod.rs | 13 +++++++++- crates/compiler/src/utils.rs | 24 ++++++++++++++++++ 5 files changed, 87 insertions(+), 7 deletions(-) create mode 100644 crates/compiler/src/utils.rs diff --git a/crates/compiler/src/build/mod.rs b/crates/compiler/src/build/mod.rs index e25894926..4219df907 100644 --- a/crates/compiler/src/build/mod.rs +++ b/crates/compiler/src/build/mod.rs @@ -35,6 +35,7 @@ use crate::{ analyze_deps::analyze_deps, finalize_module::finalize_module, load::load, parse::parse, resolve::resolve, transform::transform, }, + utils::get_module_ids_from_compilation_errors, Compiler, }; @@ -115,6 +116,17 @@ impl Compiler { } } + pub fn set_last_fail_module_ids(&self, errors: &[CompilationError]) { + let mut last_fail_module_ids = self.last_fail_module_ids.lock(); + last_fail_module_ids.clear(); + + for id in get_module_ids_from_compilation_errors(errors) { + if !last_fail_module_ids.contains(&id) { + last_fail_module_ids.push(id); + } + } + } + pub(crate) fn build(&self) -> Result<()> { self.context.plugin_driver.build_start(&self.context)?; @@ -145,12 +157,17 @@ impl Compiler { } self.handle_global_log(&mut errors); + let mut res = Ok(()); + if !errors.is_empty() { // set stats if stats is enabled self.context.record_manager.set_build_end_time(); self.context.record_manager.set_end_time(); self.set_module_graph_stats(); + // set last failed module ids + self.set_last_fail_module_ids(&errors); + let mut error_messages = vec![]; for error in errors { error_messages.push(error.to_string()); @@ -159,7 +176,9 @@ impl Compiler { .iter() .map(|e| e.to_string()) .collect::>()); - return Err(CompilationError::GenericError(errors_json.to_string())); + res = Err(CompilationError::GenericError(errors_json.to_string())); + } else { + self.set_last_fail_module_ids(&[]); } // set module graph cache @@ -186,8 +205,10 @@ impl Compiler { { farm_profile_scope!("call build_end hook".to_string()); - self.context.plugin_driver.build_end(&self.context) + self.context.plugin_driver.build_end(&self.context)?; } + + res } pub(crate) fn handle_global_log(&self, errors: &mut Vec) { diff --git a/crates/compiler/src/lib.rs b/crates/compiler/src/lib.rs index c49b75b7b..6d74484c0 100644 --- a/crates/compiler/src/lib.rs +++ b/crates/compiler/src/lib.rs @@ -12,6 +12,8 @@ use farmfe_core::{ context::CompilationContext, error::Result, farm_profile_function, + module::ModuleId, + parking_lot::Mutex, plugin::Plugin, rayon::{ThreadPool, ThreadPoolBuilder}, }; @@ -24,10 +26,12 @@ pub mod build; pub mod generate; pub mod trace_module_graph; pub mod update; +pub mod utils; pub struct Compiler { context: Arc, pub thread_pool: Arc, + pub last_fail_module_ids: Mutex>, } impl Compiler { @@ -93,6 +97,7 @@ impl Compiler { .build() .unwrap(), ), + last_fail_module_ids: Mutex::new(vec![]), }) } @@ -128,11 +133,12 @@ impl Compiler { } // triggering build stage - { + let res = { #[cfg(feature = "profile")] farmfe_core::puffin::profile_scope!("Build Stage"); - self.build()?; - } + self.build() + }; + self.context.record_manager.set_build_end_time(); { #[cfg(feature = "profile")] @@ -166,7 +172,7 @@ impl Compiler { self.context.record_manager.set_end_time(); - Ok(()) + res } pub fn context(&self) -> &Arc { diff --git a/crates/compiler/src/update/handle_update_modules.rs b/crates/compiler/src/update/handle_update_modules.rs index 6c91c7322..5679b9e9d 100644 --- a/crates/compiler/src/update/handle_update_modules.rs +++ b/crates/compiler/src/update/handle_update_modules.rs @@ -11,6 +11,7 @@ use farmfe_utils::relative; pub fn handle_update_modules( paths: Vec<(String, UpdateType)>, + last_fail_module_ids: &[ModuleId], context: &Arc, update_result: &mut UpdateResult, ) -> farmfe_core::error::Result> { @@ -26,6 +27,8 @@ pub fn handle_update_modules( (vec![], 0) }; let paths = resolve_watch_graph_paths(paths, context); + let paths = resolve_last_failed_module_paths(paths, last_fail_module_ids, context); + if context.config.record { let end_time = std::time::SystemTime::now() .duration_since(std::time::UNIX_EPOCH) @@ -181,6 +184,7 @@ pub fn handle_update_modules( end_time, }) } + Ok(result) } @@ -231,3 +235,17 @@ fn resolve_watch_graph_paths( }) .collect() } + +fn resolve_last_failed_module_paths( + mut paths: Vec<(String, UpdateType)>, + last_fail_module_ids: &[ModuleId], + context: &Arc, +) -> Vec<(String, UpdateType)> { + paths.extend( + last_fail_module_ids + .iter() + .map(|id| (id.resolved_path(&context.config.root), UpdateType::Updated)), + ); + + paths +} diff --git a/crates/compiler/src/update/mod.rs b/crates/compiler/src/update/mod.rs index e06024c6f..31e4a253e 100644 --- a/crates/compiler/src/update/mod.rs +++ b/crates/compiler/src/update/mod.rs @@ -111,7 +111,15 @@ impl Compiler { let mut update_result = UpdateResult::default(); self.context.clear_log_store(); - let paths = handle_update_modules(paths, &self.context, &mut update_result)?; + + let last_failed_module_ids = self.last_fail_module_ids.lock(); + let paths = handle_update_modules( + paths, + &last_failed_module_ids, + &self.context, + &mut update_result, + )?; + drop(last_failed_module_ids); for (path, update_type) in paths.clone() { match update_type { @@ -165,6 +173,7 @@ impl Compiler { self.context.record_manager.set_build_end_time(); self.context.record_manager.set_end_time(); self.set_update_module_graph_stats(&update_context); + self.set_last_fail_module_ids(&errors); let mut error_messages = vec![]; for error in errors { @@ -175,6 +184,8 @@ impl Compiler { .map(|e| e.to_string()) .collect::>()); return Err(CompilationError::GenericError(errors_json.to_string())); + } else { + self.set_last_fail_module_ids(&[]); } self.context.record_manager.set_build_end_time(); diff --git a/crates/compiler/src/utils.rs b/crates/compiler/src/utils.rs new file mode 100644 index 000000000..2f342da7b --- /dev/null +++ b/crates/compiler/src/utils.rs @@ -0,0 +1,24 @@ +use farmfe_core::module::ModuleId; + +pub fn get_module_ids_from_compilation_errors( + errors: &[farmfe_core::error::CompilationError], +) -> Vec { + errors + .iter() + .filter_map(|e| match e { + farmfe_core::error::CompilationError::ResolveError { importer, .. } => { + Some(importer.as_str().into()) + } + farmfe_core::error::CompilationError::LoadError { resolved_path, .. } => { + Some(resolved_path.as_str().into()) + } + farmfe_core::error::CompilationError::TransformError { resolved_path, .. } => { + Some(resolved_path.as_str().into()) + } + farmfe_core::error::CompilationError::ParseError { resolved_path, .. } => { + Some(resolved_path.as_str().into()) + } + _ => None, + }) + .collect() +}