Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: align part of StatsError #7091

Merged
merged 9 commits into from
Jul 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion crates/node_binding/binding.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -484,6 +484,8 @@ export interface JsRspackError {
message: string
moduleIdentifier?: string
file?: string
stack?: string
hideStack?: boolean
}

export enum JsRspackSeverity {
Expand Down Expand Up @@ -584,10 +586,16 @@ export interface JsStatsChunkGroupChildren {

export interface JsStatsError {
message: string
chunkName?: string
chunkEntry?: boolean
chunkInitial?: boolean
file?: string
moduleIdentifier?: string
moduleName?: string
moduleId?: string
file?: string
chunkId?: string
details?: string
stack?: string
}

export interface JsStatsGetAssets {
Expand Down
7 changes: 7 additions & 0 deletions crates/rspack_binding_values/src/rspack_error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,14 @@ impl From<JsRspackSeverity> for RspackSeverity {
}

#[napi(object)]
#[derive(Debug)]
pub struct JsRspackError {
pub name: String,
pub message: String,
pub module_identifier: Option<String>,
pub file: Option<String>,
pub stack: Option<String>,
pub hide_stack: Option<bool>,
}

impl JsRspackError {
Expand All @@ -48,6 +51,8 @@ impl JsRspackError {
message: diagnostic.render_report(colored)?,
module_identifier: diagnostic.module_identifier().map(|d| d.to_string()),
file: diagnostic.file().map(|f| f.to_string_lossy().to_string()),
stack: diagnostic.stack(),
hide_stack: diagnostic.hide_stack(),
})
}

Expand All @@ -58,5 +63,7 @@ impl JsRspackError {
})(self.name, self.message)
.with_file(self.file.map(Into::into))
.with_module_identifier(self.module_identifier.map(Into::into))
.with_stack(self.stack)
.with_hide_stack(self.hide_stack)
}
}
14 changes: 13 additions & 1 deletion crates/rspack_binding_values/src/stats.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,16 @@ use super::{JsCompilation, ToJsCompatSource};
#[derive(Debug)]
pub struct JsStatsError {
pub message: String,
pub chunk_name: Option<String>,
pub chunk_entry: Option<bool>,
pub chunk_initial: Option<bool>,
pub file: Option<String>,
pub module_identifier: Option<String>,
pub module_name: Option<String>,
pub module_id: Option<String>,
pub file: Option<String>,
pub chunk_id: Option<String>,
pub details: Option<String>,
pub stack: Option<String>,
}

impl From<rspack_core::StatsError> for JsStatsError {
Expand All @@ -28,6 +34,12 @@ impl From<rspack_core::StatsError> for JsStatsError {
module_name: stats.module_name,
module_id: stats.module_id,
file: stats.file.map(|f| f.to_string_lossy().to_string()),
chunk_name: stats.chunk_name,
chunk_entry: stats.chunk_entry,
chunk_initial: stats.chunk_initial,
chunk_id: stats.chunk_id,
details: stats.details,
stack: stats.stack,
}
}
}
Expand Down
43 changes: 31 additions & 12 deletions crates/rspack_core/src/build_chunk_graph/code_splitter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use indexmap::{IndexMap, IndexSet};
use itertools::Itertools;
use num_bigint::BigUint;
use rspack_database::{Database, Ukey};
use rspack_error::{error, Error, Result};
use rspack_error::{error, Diagnostic, Error, Result};
use rustc_hash::{FxHashMap as HashMap, FxHashSet as HashSet, FxHasher};

use crate::dependencies_block::AsyncDependenciesToInitialChunkError;
Expand Down Expand Up @@ -515,12 +515,21 @@ impl<'me> CodeSplitter<'me> {
let depend_on = &options.depend_on;

if depend_on.is_some() && runtime.is_some() {
runtime_errors.push(Some(error!(
"Entrypoint '{name}' has 'dependOn' and 'runtime' specified. This is not valid.
runtime_errors.push((
Some(error!(
"Entrypoint '{name}' has 'dependOn' and 'runtime' specified. This is not valid.
Entrypoints that depend on other entrypoints do not have their own runtime.
They will use the runtime(s) from referenced entrypoints instead.
Remove the 'runtime' option from the entrypoint."
)));
)),
self.compilation.entrypoints.get(name).map(|key| {
self
.compilation
.chunk_group_by_ukey
.expect_get(key)
.get_entry_point_chunk()
}),
));
}

if let Some(depend_on) = &options.depend_on {
Expand Down Expand Up @@ -552,9 +561,12 @@ Remove the 'runtime' option from the entrypoint."
.expect_get(dependency_ukey)
.get_entry_point_chunk();
if referenced_chunks.contains(&dependency_chunk_ukey) {
runtime_errors.push(Some(error!(
"Entrypoints '{name}' and '{dep}' use 'dependOn' to depend on each other in a circular way."
)));
runtime_errors.push((
Some(error!(
"Entrypoints '{name}' and '{dep}' use 'dependOn' to depend on each other in a circular way."
)),
Some(entry_point.get_entry_point_chunk())
));
entry_point_runtime = Some(entry_point_chunk.ukey);
has_error = true;
break;
Expand Down Expand Up @@ -596,13 +608,16 @@ Remove the 'runtime' option from the entrypoint."
let chunk = match self.compilation.named_chunks.get(runtime) {
Some(ukey) => {
if !runtime_chunks.contains(ukey) {
runtime_errors.push(Some(error!(
let entry_chunk = entry_point.get_entry_point_chunk();
runtime_errors.push((
Some(error!(
"Entrypoint '{name}' has a 'runtime' option which points to another entrypoint named '{runtime}'.
It's not valid to use other entrypoints as runtime chunk.
Did you mean to use 'dependOn: \"{runtime}\"' instead to allow using entrypoint '{name}' within the runtime of entrypoint '{runtime}'? For this '{runtime}' must always be loaded when '{name}' is used.
Or do you want to use the entrypoints '{name}' and '{runtime}' independently on the same page with a shared runtime? In this case give them both the same value for the 'runtime' option. It must be a name not already used by an entrypoint."
)));
let entry_chunk = entry_point.get_entry_point_chunk();
)),
Some(entry_chunk),
));
entry_point.set_runtime_chunk(entry_chunk);
continue;
}
Expand All @@ -629,8 +644,12 @@ Or do you want to use the entrypoints '{name}' and '{runtime}' independently on
}
}

for err in runtime_errors.into_iter().flatten() {
self.compilation.push_diagnostic(err.into());
for (err, chunk_ukey) in runtime_errors {
if let Some(err) = err {
self
.compilation
.push_diagnostic(Diagnostic::from(err).with_chunk(chunk_ukey.map(|x| x.as_usize())));
}
}

Ok(input_entrypoints_and_modules)
Expand Down
11 changes: 10 additions & 1 deletion crates/rspack_core/src/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use rspack_error::{
thiserror::{self, Error},
DiagnosticExt, Error, TraceableError,
};
use rspack_util::ext::AsAny;

use crate::{BoxLoader, ErrorSpan};

Expand Down Expand Up @@ -153,6 +154,14 @@ pub fn map_box_diagnostics_to_module_parse_diagnostics(
) -> Vec<rspack_error::Diagnostic> {
errors
.into_iter()
.map(|e| rspack_error::miette::Error::new(ModuleParseError::new(e, loaders)).into())
.map(|e| {
let hide_stack = e
.as_any()
.downcast_ref::<TraceableError>()
.and_then(|e| e.hide_stack());
let diagnostic: rspack_error::Diagnostic =
rspack_error::miette::Error::new(ModuleParseError::new(e, loaders)).into();
diagnostic.with_hide_stack(hide_stack)
})
.collect()
}
13 changes: 9 additions & 4 deletions crates/rspack_core/src/normal_module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use std::{
use bitflags::bitflags;
use dashmap::DashMap;
use derivative::Derivative;
use rspack_error::{error, Diagnosable, Diagnostic, DiagnosticExt, Result, Severity};
use rspack_error::{error, Diagnosable, Diagnostic, DiagnosticExt, NodeError, Result, Severity};
use rspack_hash::RspackHash;
use rspack_hook::define_hook;
use rspack_identifier::Identifiable;
Expand Down Expand Up @@ -402,9 +402,14 @@ impl Module for NormalModule {
.await;
let (mut loader_result, ds) = match loader_result {
Ok(r) => r.split_into_parts(),
Err(e) => {
let e = ModuleBuildError(e).boxed();
let d = Diagnostic::from(e);
Err(r) => {
let node_error = r.downcast_ref::<NodeError>();
let stack = node_error.and_then(|e| e.stack.clone());
let hide_stack = node_error.and_then(|e| e.hide_stack);
let e = ModuleBuildError(r).boxed();
let d = Diagnostic::from(e)
.with_stack(stack)
.with_hide_stack(hide_stack);
self.source = NormalModuleSource::BuiltFailed(d.clone());
self.add_diagnostic(d);
let mut hasher = RspackHash::from(&build_context.compiler_options.output);
Expand Down
44 changes: 41 additions & 3 deletions crates/rspack_core/src/stats.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ use rustc_hash::{FxHashMap as HashMap, FxHashSet as HashSet};

use crate::{
get_chunk_from_ukey, get_chunk_group_from_ukey, BoxModule, BoxRuntimeModule, Chunk,
ChunkGroupOrderKey, ChunkGroupUkey, Compilation, ExecutedRuntimeModule, LogType, ModuleGraph,
ModuleIdentifier, ModuleType, OriginLocation, ProvidedExports, RuntimeSpec, SourceType,
UsedExports,
ChunkGroupOrderKey, ChunkGroupUkey, ChunkUkey, Compilation, ExecutedRuntimeModule, LogType,
ModuleGraph, ModuleIdentifier, ModuleType, OriginLocation, ProvidedExports, RuntimeSpec,
SourceType, UsedExports,
};

#[derive(Debug, Clone)]
Expand Down Expand Up @@ -552,6 +552,11 @@ impl Stats<'_> {
})
.unzip();

let chunk = d
.chunk()
.map(ChunkUkey::from)
.map(|key| self.compilation.chunk_by_ukey.expect_get(&key));

StatsError {
message: diagnostic_displayer
.emit_diagnostic(d)
Expand All @@ -560,6 +565,13 @@ impl Stats<'_> {
module_name,
module_id: module_id.flatten(),
file: d.file().map(ToOwned::to_owned),

chunk_name: chunk.and_then(|c| c.name.clone()),
chunk_entry: chunk.map(|c| c.has_runtime(&self.compilation.chunk_group_by_ukey)),
chunk_initial: chunk.map(|c| c.can_be_initial(&self.compilation.chunk_group_by_ukey)),
chunk_id: chunk.and_then(|c| c.id.clone()),
details: d.details(),
stack: d.stack(),
}
})
.collect()
Expand All @@ -580,6 +592,11 @@ impl Stats<'_> {
})
.unzip();

let chunk = d
.chunk()
.map(ChunkUkey::from)
.map(|key| self.compilation.chunk_by_ukey.expect_get(&key));

StatsWarning {
message: diagnostic_displayer
.emit_diagnostic(d)
Expand All @@ -588,6 +605,13 @@ impl Stats<'_> {
module_name,
module_id: module_id.flatten(),
file: d.file().map(ToOwned::to_owned),

chunk_name: chunk.and_then(|c| c.name.clone()),
chunk_entry: chunk.map(|c| c.has_runtime(&self.compilation.chunk_group_by_ukey)),
chunk_initial: chunk.map(|c| c.can_be_initial(&self.compilation.chunk_group_by_ukey)),
chunk_id: chunk.and_then(|c| c.id.clone()),
details: d.details(),
stack: d.stack(),
}
})
.collect()
Expand Down Expand Up @@ -1123,6 +1147,13 @@ pub struct StatsError {
pub module_name: Option<String>,
pub module_id: Option<String>,
pub file: Option<PathBuf>,

pub chunk_name: Option<String>,
pub chunk_entry: Option<bool>,
pub chunk_initial: Option<bool>,
pub chunk_id: Option<String>,
pub details: Option<String>,
pub stack: Option<String>,
}

#[derive(Debug)]
Expand All @@ -1132,6 +1163,13 @@ pub struct StatsWarning {
pub module_name: Option<String>,
pub module_id: Option<String>,
pub file: Option<PathBuf>,

pub chunk_name: Option<String>,
pub chunk_entry: Option<bool>,
pub chunk_initial: Option<bool>,
pub chunk_id: Option<String>,
pub details: Option<String>,
pub stack: Option<String>,
}

#[derive(Debug)]
Expand Down
Loading
Loading