Skip to content

Commit

Permalink
feat: align module error
Browse files Browse the repository at this point in the history
  • Loading branch information
h-a-n-a committed Dec 5, 2023
1 parent 1aa1c32 commit 89ae615
Show file tree
Hide file tree
Showing 42 changed files with 132 additions and 111 deletions.
4 changes: 2 additions & 2 deletions crates/node_binding/binding.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export class JsCompilation {
getMissingDependencies(): Array<string>
getBuildDependencies(): Array<string>
pushDiagnostic(severity: "error" | "warning", title: string, message: string): void
pushNativeDiagnostics(diagnostics: ExternalObject<'Diagnostic[]'>): void
pushNativeDiagnostics(diagnostics: ExternalObject<'RspackDiagnostic[]'>): void
getStats(): JsStats
getAssetPath(filename: string, data: PathData): string
getAssetPathWithInfo(filename: string, data: PathData): PathWithInfo
Expand Down Expand Up @@ -342,7 +342,7 @@ export interface JsLoaderContext {
* Internal loader diagnostic
* @internal
*/
diagnosticsExternal: ExternalObject<'Diagnostic[]'>
diagnosticsExternal: ExternalObject<'RspackDiagnostic[]'>
}

export interface JsModule {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::{

use napi_derive::napi;
use rspack_core::{rspack_sources::SourceMap, Content, ResourceData};
use rspack_error::Diagnostic;
use rspack_error::RspackDiagnostic;
use rspack_loader_runner::AdditionalData;
use rustc_hash::FxHashSet as HashSet;
use tracing::{span_enabled, Level};
Expand Down Expand Up @@ -261,8 +261,8 @@ pub struct JsLoaderContext {
pub context_external: External<rspack_core::LoaderRunnerContext>,
/// Internal loader diagnostic
/// @internal
#[napi(ts_type = "ExternalObject<'Diagnostic[]'>")]
pub diagnostics_external: External<Vec<Diagnostic>>,
#[napi(ts_type = "ExternalObject<'RspackDiagnostic[]'>")]
pub diagnostics_external: External<Vec<RspackDiagnostic>>,
}

impl TryFrom<&rspack_core::LoaderContext<'_, rspack_core::LoaderRunnerContext>>
Expand Down
10 changes: 5 additions & 5 deletions crates/rspack_binding_values/src/compilation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use rspack_core::rspack_sources::BoxSource;
use rspack_core::AssetInfo;
use rspack_core::ModuleIdentifier;
use rspack_core::{rspack_sources::SourceExt, NormalModuleSource};
use rspack_error::Diagnostic;
use rspack_error::RspackDiagnostic;
use rspack_identifier::Identifier;
use rspack_napi_shared::NapiResultExt;

Expand Down Expand Up @@ -315,14 +315,14 @@ impl JsCompilation {
#[napi(ts_args_type = r#"severity: "error" | "warning", title: string, message: string"#)]
pub fn push_diagnostic(&mut self, severity: String, title: String, message: String) {
let diagnostic = match severity.as_str() {
"warning" => rspack_error::Diagnostic::warn(title, message, 0, 0),
_ => rspack_error::Diagnostic::error(title, message, 0, 0),
"warning" => rspack_error::RspackDiagnostic::warn(title, message, 0, 0),
_ => rspack_error::RspackDiagnostic::error(title, message, 0, 0),
};
self.inner.push_diagnostic(diagnostic);
}

#[napi(ts_args_type = r#"diagnostics: ExternalObject<'Diagnostic[]'>"#)]
pub fn push_native_diagnostics(&mut self, mut diagnostics: External<Vec<Diagnostic>>) {
#[napi(ts_args_type = r#"diagnostics: ExternalObject<'RspackDiagnostic[]'>"#)]
pub fn push_native_diagnostics(&mut self, mut diagnostics: External<Vec<RspackDiagnostic>>) {
while let Some(diagnostic) = diagnostics.pop() {
self.inner.push_diagnostic(diagnostic);
}
Expand Down
12 changes: 6 additions & 6 deletions crates/rspack_core/src/compiler/compilation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use itertools::Itertools;
use rayon::prelude::{
IntoParallelIterator, IntoParallelRefIterator, ParallelBridge, ParallelIterator,
};
use rspack_error::{internal_error, Diagnostic, Result, Severity, TWithDiagnosticArray};
use rspack_error::{internal_error, Result, RspackDiagnostic, Severity, TWithDiagnosticArray};
use rspack_futures::FuturesResults;
use rspack_hash::{RspackHash, RspackHashDigest};
use rspack_identifier::{Identifiable, IdentifierMap, IdentifierSet};
Expand Down Expand Up @@ -78,7 +78,7 @@ pub struct Compilation {
pub async_entrypoints: Vec<ChunkGroupUkey>,
assets: CompilationAssets,
pub emitted_assets: DashSet<String, BuildHasherDefault<FxHasher>>,
diagnostics: IndexSet<Diagnostic, BuildHasherDefault<FxHasher>>,
diagnostics: IndexSet<RspackDiagnostic, BuildHasherDefault<FxHasher>>,
logging: CompilationLogging,
pub plugin_driver: SharedPluginDriver,
pub resolver_factory: Arc<ResolverFactory>,
Expand Down Expand Up @@ -314,22 +314,22 @@ impl Compilation {
&self.entrypoints
}

pub fn push_diagnostic(&mut self, diagnostic: Diagnostic) {
pub fn push_diagnostic(&mut self, diagnostic: RspackDiagnostic) {
self.diagnostics.insert(diagnostic);
}

pub fn push_batch_diagnostic(&mut self, diagnostics: Vec<Diagnostic>) {
pub fn push_batch_diagnostic(&mut self, diagnostics: Vec<RspackDiagnostic>) {
self.diagnostics.extend(diagnostics);
}

pub fn get_errors(&self) -> impl Iterator<Item = &Diagnostic> {
pub fn get_errors(&self) -> impl Iterator<Item = &RspackDiagnostic> {
self
.diagnostics
.iter()
.filter(|d| matches!(d.severity, Severity::Error))
}

pub fn get_warnings(&self) -> impl Iterator<Item = &Diagnostic> {
pub fn get_warnings(&self) -> impl Iterator<Item = &RspackDiagnostic> {
self
.diagnostics
.iter()
Expand Down
6 changes: 3 additions & 3 deletions crates/rspack_core/src/compiler/queue.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::sync::Arc;

use rspack_error::{Diagnostic, Result};
use rspack_error::{Result, RspackDiagnostic};

use crate::{
cache::Cache, BoxDependency, BuildContext, BuildResult, Compilation, CompilerContext,
Expand Down Expand Up @@ -56,7 +56,7 @@ pub struct FactorizeTaskResult {
pub factory_result: ModuleFactoryResult,
pub module_graph_module: Box<ModuleGraphModule>,
pub dependencies: Vec<DependencyId>,
pub diagnostics: Vec<Diagnostic>,
pub diagnostics: Vec<RspackDiagnostic>,
pub is_entry: bool,
pub current_profile: Option<Box<ModuleProfile>>,
pub exports_info_related: ExportsInfoRelated,
Expand Down Expand Up @@ -280,7 +280,7 @@ pub struct BuildTask {
pub struct BuildTaskResult {
pub module: Box<dyn Module>,
pub build_result: Box<BuildResult>,
pub diagnostics: Vec<Diagnostic>,
pub diagnostics: Vec<RspackDiagnostic>,
pub current_profile: Option<Box<ModuleProfile>>,
pub from_cache: bool,
}
Expand Down
2 changes: 1 addition & 1 deletion crates/rspack_core/src/context_module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use std::{
use itertools::Itertools;
use once_cell::sync::Lazy;
use regex::{Captures, Regex};
use rspack_error::{IntoTWithDiagnosticArray, Result, TWithDiagnosticArray};
use rspack_error::{IntoTWithRspackDiagnosticArray, Result, TWithDiagnosticArray};
use rspack_hash::RspackHash;
use rspack_identifier::{Identifiable, Identifier};
use rspack_regex::RspackRegex;
Expand Down
2 changes: 1 addition & 1 deletion crates/rspack_core/src/context_module_factory.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::sync::Arc;

use rspack_error::{IntoTWithDiagnosticArray, Result, TWithDiagnosticArray};
use rspack_error::{IntoTWithRspackDiagnosticArray, Result, TWithDiagnosticArray};
use tracing::instrument;

use crate::{
Expand Down
6 changes: 3 additions & 3 deletions crates/rspack_core/src/dependencies_block.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use rspack_error::{Diagnostic, DIAGNOSTIC_POS_DUMMY};
use rspack_error::{RspackDiagnostic, DIAGNOSTIC_POS_DUMMY};
use serde::Serialize;
use ustr::Ustr;

Expand Down Expand Up @@ -163,14 +163,14 @@ pub struct AsyncDependenciesToInitialChunkError<'a> {
pub loc: Option<&'a DependencyLocation>,
}

impl<'a> From<AsyncDependenciesToInitialChunkError<'a>> for Diagnostic {
impl<'a> From<AsyncDependenciesToInitialChunkError<'a>> for RspackDiagnostic {
fn from(value: AsyncDependenciesToInitialChunkError<'a>) -> Self {
let title = "AsyncDependencyToInitialChunkError".to_string();
let message = format!("It's not allowed to load an initial chunk on demand. The chunk name \"{}\" is already used by an entrypoint.", value.chunk_name);
let (start, end) = value
.loc
.map(|loc| (loc.start as usize, loc.end as usize))
.unwrap_or((DIAGNOSTIC_POS_DUMMY, DIAGNOSTIC_POS_DUMMY));
Diagnostic::error(title, message, start, end)
RspackDiagnostic::error(title, message, start, end)
}
}
9 changes: 9 additions & 0 deletions crates/rspack_core/src/diagnostics.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
use rspack_error::{
miette::{self, Diagnostic},
thiserror::{self, Error},
};

#[derive(Debug, Error, Diagnostic)]
#[error("Module build failed")]
#[diagnostic(code(ModuleBuildError))]
pub struct ModuleBuildError(pub String);
2 changes: 1 addition & 1 deletion crates/rspack_core/src/external_module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::borrow::Cow;
use std::hash::Hash;
use std::iter;

use rspack_error::{internal_error, IntoTWithDiagnosticArray, Result, TWithDiagnosticArray};
use rspack_error::{internal_error, IntoTWithRspackDiagnosticArray, Result, TWithDiagnosticArray};
use rspack_hash::RspackHash;
use rspack_identifier::{Identifiable, Identifier};
use rustc_hash::FxHashMap as HashMap;
Expand Down
1 change: 1 addition & 0 deletions crates/rspack_core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use std::sync::atomic::AtomicBool;
use std::{fmt, sync::Arc};
mod dependencies_block;
mod diagnostics;
pub mod mf;
pub use dependencies_block::{
AsyncDependenciesBlock, AsyncDependenciesBlockIdentifier, DependenciesBlock, DependencyLocation,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::{borrow::Cow, hash::Hash};

use async_trait::async_trait;
use rspack_error::{IntoTWithDiagnosticArray, Result, TWithDiagnosticArray};
use rspack_error::{IntoTWithRspackDiagnosticArray, Result, TWithDiagnosticArray};
use rspack_hash::RspackHash;
use rspack_identifier::{Identifiable, Identifier};
use rspack_sources::{RawSource, Source, SourceExt};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// TODO: move to rspack_plugin_mf

use async_trait::async_trait;
use rspack_error::{internal_error, IntoTWithDiagnosticArray, Result, TWithDiagnosticArray};
use rspack_error::{internal_error, IntoTWithRspackDiagnosticArray, Result, TWithDiagnosticArray};

use super::{
container_entry_dependency::ContainerEntryDependency,
Expand Down
2 changes: 1 addition & 1 deletion crates/rspack_core/src/mf/container/remote_module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::borrow::Cow;
use std::hash::Hash;

use async_trait::async_trait;
use rspack_error::{IntoTWithDiagnosticArray, Result, TWithDiagnosticArray};
use rspack_error::{IntoTWithRspackDiagnosticArray, Result, TWithDiagnosticArray};
use rspack_hash::RspackHash;
use rspack_identifier::{Identifiable, Identifier};
use rspack_sources::{RawSource, Source, SourceExt};
Expand Down
2 changes: 1 addition & 1 deletion crates/rspack_core/src/mf/sharing/consume_shared_module.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::{borrow::Cow, hash::Hash};

use async_trait::async_trait;
use rspack_error::{IntoTWithDiagnosticArray, Result, TWithDiagnosticArray};
use rspack_error::{IntoTWithRspackDiagnosticArray, Result, TWithDiagnosticArray};
use rspack_hash::RspackHash;
use rspack_identifier::{Identifiable, Identifier};
use rspack_sources::{RawSource, Source, SourceExt};
Expand Down
2 changes: 1 addition & 1 deletion crates/rspack_core/src/mf/sharing/provide_shared_module.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::{borrow::Cow, hash::Hash};

use async_trait::async_trait;
use rspack_error::{IntoTWithDiagnosticArray, Result, TWithDiagnosticArray};
use rspack_error::{IntoTWithRspackDiagnosticArray, Result, TWithDiagnosticArray};
use rspack_hash::RspackHash;
use rspack_identifier::{Identifiable, Identifier};
use rspack_sources::Source;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// TODO: move to rspack_plugin_mf

use async_trait::async_trait;
use rspack_error::{internal_error, IntoTWithDiagnosticArray, Result, TWithDiagnosticArray};
use rspack_error::{internal_error, IntoTWithRspackDiagnosticArray, Result, TWithDiagnosticArray};

use super::{
provide_shared_dependency::ProvideSharedDependency, provide_shared_module::ProvideSharedModule,
Expand Down
2 changes: 1 addition & 1 deletion crates/rspack_core/src/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::path::PathBuf;
use std::{any::Any, borrow::Cow, fmt::Debug};

use async_trait::async_trait;
use rspack_error::{IntoTWithDiagnosticArray, Result, TWithDiagnosticArray};
use rspack_error::{IntoTWithRspackDiagnosticArray, Result, TWithDiagnosticArray};
use rspack_hash::{RspackHash, RspackHashDigest};
use rspack_identifier::{Identifiable, Identifier};
use rspack_sources::Source;
Expand Down
5 changes: 3 additions & 2 deletions crates/rspack_core/src/normal_module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ use bitflags::bitflags;
use dashmap::DashMap;
use derivative::Derivative;
use rspack_error::{
internal_error, Diagnostic, IntoTWithDiagnosticArray, Result, Severity, TWithDiagnosticArray,
internal_error, IntoTWithRspackDiagnosticArray, Result, RspackDiagnostic, Severity,
TWithDiagnosticArray,
};
use rspack_hash::RspackHash;
use rspack_identifier::Identifiable;
Expand Down Expand Up @@ -133,7 +134,7 @@ pub enum NormalModuleSource {
}

impl NormalModuleSource {
pub fn new_built(source: BoxSource, diagnostics: &[Diagnostic]) -> Self {
pub fn new_built(source: BoxSource, diagnostics: &[RspackDiagnostic]) -> Self {
if diagnostics.iter().any(|d| d.severity == Severity::Error) {
NormalModuleSource::BuiltFailed(
diagnostics
Expand Down
4 changes: 2 additions & 2 deletions crates/rspack_core/src/normal_module_factory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::{path::Path, sync::Arc};
use once_cell::sync::Lazy;
use regex::Regex;
use rspack_error::{
internal_error, Diagnostic, IntoTWithDiagnosticArray, Result, TWithDiagnosticArray,
internal_error, IntoTWithRspackDiagnosticArray, Result, RspackDiagnostic, TWithDiagnosticArray,
};
use rspack_loader_runner::{get_scheme, Loader, Scheme};
use sugar_path::{AsPath, SugarPath};
Expand Down Expand Up @@ -361,7 +361,7 @@ impl NormalModuleFactory {
let module_identifier = ModuleIdentifier::from(format!("missing|{ident}"));
let mut file_dependencies = Default::default();
let mut missing_dependencies = Default::default();
let diagnostics: Vec<Diagnostic> = internal_error.into();
let diagnostics: Vec<RspackDiagnostic> = internal_error.into();
let mut diagnostic = diagnostics[0].clone();
let mut from_cache_result = from_cache;
if !data
Expand Down
6 changes: 3 additions & 3 deletions crates/rspack_core/src/plugin/plugin_driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::{
sync::{Arc, Mutex},
};

use rspack_error::{Diagnostic, Result};
use rspack_error::{Result, RspackDiagnostic};
use rspack_loader_runner::ResourceData;
use rustc_hash::FxHashMap as HashMap;
use tracing::instrument;
Expand Down Expand Up @@ -34,7 +34,7 @@ pub struct PluginDriver {
// pub registered_parser: HashMap<ModuleType, BoxedParser>,
pub registered_parser_and_generator_builder: HashMap<ModuleType, BoxedParserAndGeneratorBuilder>,
/// Collecting error generated by plugin phase, e.g., `Syntax Error`
pub diagnostics: Arc<Mutex<Vec<Diagnostic>>>,
pub diagnostics: Arc<Mutex<Vec<RspackDiagnostic>>>,
}

impl std::fmt::Debug for PluginDriver {
Expand Down Expand Up @@ -90,7 +90,7 @@ impl PluginDriver {
)
}

pub fn take_diagnostic(&self) -> Vec<Diagnostic> {
pub fn take_diagnostic(&self) -> Vec<RspackDiagnostic> {
let mut diagnostic = self.diagnostics.lock().expect("TODO:");
std::mem::take(&mut diagnostic)
}
Expand Down
2 changes: 1 addition & 1 deletion crates/rspack_core/src/raw_module.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::borrow::Cow;
use std::hash::Hash;

use rspack_error::{IntoTWithDiagnosticArray, Result, TWithDiagnosticArray};
use rspack_error::{IntoTWithRspackDiagnosticArray, Result, TWithDiagnosticArray};
use rspack_hash::RspackHash;
use rspack_identifier::Identifiable;
use rspack_sources::{BoxSource, RawSource, Source, SourceExt};
Expand Down
2 changes: 1 addition & 1 deletion crates/rspack_core/src/tree_shaking/optimizer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use petgraph::{
Directed,
};
use rspack_error::{
errors_to_diagnostics, Error, InternalError, IntoTWithDiagnosticArray, Result, Severity,
errors_to_diagnostics, Error, InternalError, IntoTWithRspackDiagnosticArray, Result, Severity,
TWithDiagnosticArray,
};
use rspack_identifier::{Identifier, IdentifierLinkedSet, IdentifierMap, IdentifierSet};
Expand Down
Loading

0 comments on commit 89ae615

Please sign in to comment.