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(stats): support StatsError.moduleTrace #7123

Merged
merged 5 commits into from
Jul 12, 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
21 changes: 20 additions & 1 deletion crates/node_binding/binding.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -597,6 597,7 @@ export interface JsStatsError {
chunkId?: string
details?: string
stack?: string
moduleTrace: Array<JsStatsModuleTrace>
}

export interface JsStatsGetAssets {
Expand Down Expand Up @@ -673,6 674,17 @@ export interface JsStatsModuleReason {
userRequest?: string
}

export interface JsStatsModuleTrace {
origin: JsStatsModuleTraceModule
module: JsStatsModuleTraceModule
}

export interface JsStatsModuleTraceModule {
identifier: string
name?: string
id?: string
}

export interface JsStatsOptimizationBailout {
inner: string
}
Expand All @@ -684,10 696,17 @@ export interface JsStatsSize {

export interface JsStatsWarning {
message: string
chunkName?: string
chunkEntry?: boolean
chunkInitial?: boolean
file?: string
moduleIdentifier?: string
moduleName?: string
moduleId?: string
file?: string
chunkId?: string
details?: string
stack?: string
moduleTrace: Array<JsStatsModuleTrace>
}

export interface JsTap {
Expand Down
60 changes: 59 additions & 1 deletion crates/rspack_binding_values/src/stats.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 24,7 @@ pub struct JsStatsError {
pub chunk_id: Option<String>,
pub details: Option<String>,
pub stack: Option<String>,
pub module_trace: Vec<JsStatsModuleTrace>,
}

impl From<rspack_core::StatsError> for JsStatsError {
Expand All @@ -40,17 41,29 @@ impl From<rspack_core::StatsError> for JsStatsError {
chunk_id: stats.chunk_id,
details: stats.details,
stack: stats.stack,
module_trace: stats
.module_trace
.into_iter()
.map(Into::into)
.collect::<Vec<_>>(),
}
}
}

#[napi(object)]
pub struct JsStatsWarning {
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>,
pub module_trace: Vec<JsStatsModuleTrace>,
}

impl From<rspack_core::StatsWarning> for JsStatsWarning {
Expand All @@ -61,6 74,51 @@ impl From<rspack_core::StatsWarning> for JsStatsWarning {
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,
module_trace: stats
.module_trace
.into_iter()
.map(Into::into)
.collect::<Vec<_>>(),
}
}
}

#[napi(object)]
#[derive(Debug)]
pub struct JsStatsModuleTrace {
pub origin: JsStatsModuleTraceModule,
pub module: JsStatsModuleTraceModule,
}

impl From<rspack_core::StatsModuleTrace> for JsStatsModuleTrace {
fn from(stats: rspack_core::StatsModuleTrace) -> Self {
Self {
origin: stats.origin.into(),
module: stats.module.into(),
}
}
}

#[napi(object)]
#[derive(Debug)]
pub struct JsStatsModuleTraceModule {
pub identifier: String,
pub name: Option<String>,
pub id: Option<String>,
}

impl From<rspack_core::StatsErrorModuleTraceModule> for JsStatsModuleTraceModule {
fn from(stats: rspack_core::StatsErrorModuleTraceModule) -> Self {
Self {
identifier: stats.identifier,
name: stats.name,
id: stats.id,
}
}
}
Expand Down
75 changes: 75 additions & 0 deletions crates/rspack_core/src/stats.rs
Original file line number Diff line number Diff line change
Expand Up @@ -557,6 557,7 @@ impl Stats<'_> {
.map(ChunkUkey::from)
.map(|key| self.compilation.chunk_by_ukey.expect_get(&key));

let module_trace = self.get_module_trace(module_identifier);
StatsError {
message: diagnostic_displayer
.emit_diagnostic(d)
Expand All @@ -572,6 573,7 @@ impl Stats<'_> {
chunk_id: chunk.and_then(|c| c.id.clone()),
details: d.details(),
stack: d.stack(),
module_trace,
}
})
.collect()
Expand All @@ -597,6 599,8 @@ impl Stats<'_> {
.map(ChunkUkey::from)
.map(|key| self.compilation.chunk_by_ukey.expect_get(&key));

let module_trace = self.get_module_trace(module_identifier);

StatsWarning {
message: diagnostic_displayer
.emit_diagnostic(d)
Expand All @@ -612,6 616,7 @@ impl Stats<'_> {
chunk_id: chunk.and_then(|c| c.id.clone()),
details: d.details(),
stack: d.stack(),
module_trace,
}
})
.collect()
Expand Down Expand Up @@ -1128,6 1133,61 @@ impl Stats<'_> {
siblings.sort();
(parents, children, siblings)
}

fn get_module_trace(&self, module_identifier: Option<Identifier>) -> Vec<StatsModuleTrace> {
let module_graph = self.compilation.get_module_graph();
let mut module_trace = vec![];
let mut visited_modules = HashSet::<Identifier>::default();
let mut current_module_identifier = module_identifier;
while let Some(module_identifier) = current_module_identifier {
if visited_modules.contains(&module_identifier) {
break;
}
visited_modules.insert(module_identifier);
let Some(origin_module) = module_graph.get_issuer(&module_identifier) else {
break;
};
let Some(current_module) = module_graph.module_by_identifier(&module_identifier) else {
break;
};
let origin_stats_module = StatsErrorModuleTraceModule {
identifier: origin_module.identifier().to_string(),
name: Some(
origin_module
.readable_identifier(&self.compilation.options.context)
.to_string(),
),
id: self
.compilation
.chunk_graph
.get_module_id(origin_module.identifier())
.to_owned(),
};

let current_stats_module = StatsErrorModuleTraceModule {
identifier: current_module.identifier().to_string(),
name: Some(
current_module
.readable_identifier(&self.compilation.options.context)
.to_string(),
),
id: self
.compilation
.chunk_graph
.get_module_id(current_module.identifier())
.to_owned(),
};

module_trace.push(StatsModuleTrace {
origin: origin_stats_module,
module: current_stats_module,
});

current_module_identifier = Some(origin_module.identifier());
}

module_trace
}
}

fn get_stats_module_name_and_id(
Expand All @@ -1154,6 1214,7 @@ pub struct StatsError {
pub chunk_id: Option<String>,
pub details: Option<String>,
pub stack: Option<String>,
pub module_trace: Vec<StatsModuleTrace>,
}

#[derive(Debug)]
Expand All @@ -1170,6 1231,20 @@ pub struct StatsWarning {
pub chunk_id: Option<String>,
pub details: Option<String>,
pub stack: Option<String>,
pub module_trace: Vec<StatsModuleTrace>,
}

#[derive(Debug)]
pub struct StatsModuleTrace {
pub origin: StatsErrorModuleTraceModule,
pub module: StatsErrorModuleTraceModule,
}

#[derive(Debug)]
pub struct StatsErrorModuleTraceModule {
pub identifier: String,
pub name: Option<String>,
pub id: Option<String>,
}

#[derive(Debug)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 302,8 @@ exports[`statsOutput statsOutput/parse-error should print correct stats for 1`]

help:
You may need an appropriate loader to handle this file type.
@ ./b.js [996]
@ ./a.js

Rspack compiled with 1 error"
`;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 1 @@
import './b';
Original file line number Diff line number Diff line change
@@ -0,0 1 @@
import './c';
Original file line number Diff line number Diff line change
@@ -0,0 1 @@
import './no-exist';
Original file line number Diff line number Diff line change
@@ -0,0 1,3 @@
module.exports = [
[/Module not found/]
];
Original file line number Diff line number Diff line change
@@ -0,0 1 @@
import './a';
Original file line number Diff line number Diff line change
@@ -0,0 1,21 @@
/** @type {import("@rspack/core").Configuration} */
module.exports = {
entry: "./index",
plugins: [
{
apply(compiler) {
compiler.hooks.done.tap("TestPlugin", stats => {
const errors = stats.toJson({ errors: true, ids: true }).errors;
expect(errors.length).toBe(1);
const moduleTrace = errors[0].moduleTrace;
expect(moduleTrace[0].module.name).toBe('./c.js');
expect(moduleTrace[0].origin.name).toBe('./b.js');
expect(moduleTrace[1].module.name).toBe('./b.js');
expect(moduleTrace[1].origin.name).toBe('./a.js');
expect(moduleTrace[2].module.name).toBe('./a.js');
expect(moduleTrace[2].origin.name).toBe('./index.js');
});
}
}
]
};
Original file line number Diff line number Diff line change
@@ -0,0 1,5 @@
module.exports = {
findBundle: function () {
return [];
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 5,15 @@ ERROR in ./node_modules/some-module/a.ts
· ────────
2 │ export * from "./c.js";
╰────
@ ./node_modules/some-module/a.ts
@ ./reexport.ts

ERROR in ./node_modules/some-module/a.ts
× Module not found: Can't resolve './c.js' in '<PROJECT_ROOT>/tests/diagnosticsCases/factorize/cannot-resolve-with-concatenate-conflict/node_modules/some-module'
╭─[1:1]
1 │ export * from "./b.js";
2 │ export * from "./c.js";
· ────────
╰────
╰────
@ ./node_modules/some-module/a.ts
@ ./reexport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 3,6 @@ ERROR in ./node_modules/some-module/a.ts
╭────
1 │ export * from "./b.js";
· ────────
╰────
╰────
@ ./node_modules/some-module/a.ts
@ ./reexport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 8,8 @@ WARNING in ./index.css
3 │ .class {
╰────
help: Remove '~' from the request.

@ ./index.css

WARNING in ./index.css
⚠ Module parse warning:
Expand All @@ -19,4 21,6 @@ WARNING in ./index.css
· ─────────────────
5 │ }
╰────
help: Remove '~' from the request.
help: Remove '~' from the request.

@ ./index.css
Loading
Loading