Skip to content

Commit

Permalink
chore: do not exit when first compilation failed (#1764)
Browse files Browse the repository at this point in the history
Co-authored-by: ADNY <[email protected]>
  • Loading branch information
wre232114 and ErKeLost authored Sep 11, 2024
1 parent a783c3e commit 9d61308
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 7 deletions.
25 changes: 23 additions & 2 deletions crates/compiler/src/build/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};

Expand Down Expand Up @@ -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)?;

Expand Down Expand Up @@ -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());
Expand All @@ -159,7 +176,9 @@ impl Compiler {
.iter()
.map(|e| e.to_string())
.collect::<Vec<_>>());
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
Expand All @@ -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<CompilationError>) {
Expand Down
14 changes: 10 additions & 4 deletions crates/compiler/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ use farmfe_core::{
context::CompilationContext,
error::Result,
farm_profile_function,
module::ModuleId,
parking_lot::Mutex,
plugin::Plugin,
rayon::{ThreadPool, ThreadPoolBuilder},
};
Expand All @@ -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<CompilationContext>,
pub thread_pool: Arc<ThreadPool>,
pub last_fail_module_ids: Mutex<Vec<ModuleId>>,
}

impl Compiler {
Expand Down Expand Up @@ -93,6 +97,7 @@ impl Compiler {
.build()
.unwrap(),
),
last_fail_module_ids: Mutex::new(vec![]),
})
}

Expand Down Expand Up @@ -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")]
Expand Down Expand Up @@ -166,7 +172,7 @@ impl Compiler {

self.context.record_manager.set_end_time();

Ok(())
res
}

pub fn context(&self) -> &Arc<CompilationContext> {
Expand Down
18 changes: 18 additions & 0 deletions crates/compiler/src/update/handle_update_modules.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use farmfe_utils::relative;

pub fn handle_update_modules(
paths: Vec<(String, UpdateType)>,
last_fail_module_ids: &[ModuleId],
context: &Arc<CompilationContext>,
update_result: &mut UpdateResult,
) -> farmfe_core::error::Result<Vec<(String, UpdateType)>> {
Expand All @@ -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)
Expand Down Expand Up @@ -181,6 +184,7 @@ pub fn handle_update_modules(
end_time,
})
}

Ok(result)
}

Expand Down Expand Up @@ -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<CompilationContext>,
) -> Vec<(String, UpdateType)> {
paths.extend(
last_fail_module_ids
.iter()
.map(|id| (id.resolved_path(&context.config.root), UpdateType::Updated)),
);

paths
}
13 changes: 12 additions & 1 deletion crates/compiler/src/update/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand All @@ -175,6 +184,8 @@ impl Compiler {
.map(|e| e.to_string())
.collect::<Vec<_>>());
return Err(CompilationError::GenericError(errors_json.to_string()));
} else {
self.set_last_fail_module_ids(&[]);
}

self.context.record_manager.set_build_end_time();
Expand Down
24 changes: 24 additions & 0 deletions crates/compiler/src/utils.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
use farmfe_core::module::ModuleId;

pub fn get_module_ids_from_compilation_errors(
errors: &[farmfe_core::error::CompilationError],
) -> Vec<ModuleId> {
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()
}

0 comments on commit 9d61308

Please sign in to comment.