Skip to content

Commit

Permalink
feat(rust): return errors from NormalModuleTask#resolve_dependencies (
Browse files Browse the repository at this point in the history
#270)

<!-- Thank you for contributing! -->

### Description

<!-- Please insert your description here and provide especially info about the "what" this PR is solving -->

### Test Plan

<!-- e.g. is there anything you'd like reviewers to focus on? -->

---
  • Loading branch information
hyf0 committed Nov 15, 2023
1 parent 81e52d2 commit fdbf290
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 18 deletions.
2 changes: 1 addition & 1 deletion crates/rolldown/src/bundler/module_loader/module_loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ impl<T: FileSystem + 'static + Default> ModuleLoader<T> {

let import_records = builder.import_records.as_mut().unwrap();

resolved_deps.into_iter().for_each(|(import_record_idx, info)| {
resolved_deps.into_iter_enumerated().for_each(|(import_record_idx, info)| {
let id = self.try_spawn_new_task(&info, false, &mut symbols);
let import_record = &mut import_records[import_record_idx];
import_record.resolved_module = id;
Expand Down
28 changes: 13 additions & 15 deletions crates/rolldown/src/bundler/module_loader/normal_module_task.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use crate::{
},
visitors::scanner::{self, ScanResult},
},
error::BatchedResult,
error::{BatchedErrors, BatchedResult},
HookLoadArgs, HookResolveIdArgsOptions, HookTransformArgs,
};
pub struct NormalModuleTask<'task, T: FileSystem + Default> {
Expand Down Expand Up @@ -221,11 +221,10 @@ impl<'task, T: FileSystem + Default + 'static> NormalModuleTask<'task, T> {
}
}

#[allow(clippy::collection_is_never_read)]
async fn resolve_dependencies(
&mut self,
dependencies: &IndexVec<ImportRecordId, ImportRecord>,
) -> BatchedResult<Vec<(ImportRecordId, ResolvedRequestInfo)>> {
) -> BatchedResult<IndexVec<ImportRecordId, ResolvedRequestInfo>> {
let jobs = dependencies.iter_enumerated().map(|(idx, item)| {
let specifier = item.module_request.clone();
let input_options = Arc::clone(&self.ctx.input_options);
Expand All @@ -251,18 +250,17 @@ impl<'task, T: FileSystem + Default + 'static> NormalModuleTask<'task, T> {

let resolved_ids = join_all(jobs).await;

let mut errors = vec![];

let ret = resolved_ids
.into_iter()
.filter_map(|handle| match handle.unwrap() {
Ok(item) => Some(item),
Err(e) => {
errors.push(e);
None
}
})
.collect();
let mut errors = BatchedErrors::default();
let mut ret = IndexVec::with_capacity(dependencies.len());
resolved_ids.into_iter().for_each(|handle| match handle.expect("Assuming no task panics") {
Ok((_idx, item)) => {
ret.push(item);
}
Err(e) => {
errors.merge(e);
}
});
debug_assert!(errors.is_empty() && ret.len() == dependencies.len());

Ok(ret)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use index_vec::IndexVec;
use oxc::{ast::Visit, span::SourceType};
use rolldown_common::{ModuleId, ModuleType, ResourceId, SymbolRef};
use rolldown_error::BuildError;
Expand Down Expand Up @@ -70,7 +71,7 @@ impl<'task, T: FileSystem + Default + 'static> RuntimeNormalModuleTask<'task, T>
.common_data
.tx
.send(Msg::RuntimeNormalModuleDone(NormalModuleTaskResult {
resolved_deps: Vec::default(),
resolved_deps: IndexVec::default(),
module_id: self.module_id,
errors: self.errors,
warnings: self.warnings,
Expand Down
3 changes: 2 additions & 1 deletion crates/rolldown/src/bundler/module_loader/task_result.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use index_vec::IndexVec;
use rolldown_common::{ImportRecordId, ModuleId};
use rolldown_error::BuildError;

Expand All @@ -8,7 +9,7 @@ use crate::bundler::utils::resolve_id::ResolvedRequestInfo;
pub struct NormalModuleTaskResult {
pub module_id: ModuleId,
pub ast_symbol: AstSymbol,
pub resolved_deps: Vec<(ImportRecordId, ResolvedRequestInfo)>,
pub resolved_deps: IndexVec<ImportRecordId, ResolvedRequestInfo>,
pub errors: Vec<BuildError>,
pub warnings: Vec<BuildError>,
pub builder: NormalModuleBuilder,
Expand Down
5 changes: 5 additions & 0 deletions crates/rolldown/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ use smallvec::SmallVec;
pub struct BatchedErrors(SmallVec<[BuildError; 1]>);

impl BatchedErrors {
// TODO(hyf): using `trait Extend` would be more proper.
pub fn merge(&mut self, mut other: Self) {
self.0.append(&mut other.0);
}

pub fn with_error(err: BuildError) -> Self {
Self(smallvec::smallvec![err])
}
Expand Down

0 comments on commit fdbf290

Please sign in to comment.