Skip to content
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: 0 additions & 10 deletions e2e/cases/config/stats-module-trace/index.test.ts

This file was deleted.

1 change: 0 additions & 1 deletion e2e/cases/config/stats-module-trace/src/index.tsx

This file was deleted.

1 change: 0 additions & 1 deletion e2e/cases/config/stats-module-trace/src/test.tsx

This file was deleted.

25 changes: 25 additions & 0 deletions e2e/cases/diagnostic/import-traces/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { expect, test } from '@e2e/helper';

const EXPECTED_LOG = `Import traces (entry → error):
./src/index.js
./src/child1.js
./src/child2.js
./src/child3.js ×`;

test('should print import traces if module build failed in dev', async ({
dev,
}) => {
const rsbuild = await dev();
await rsbuild.expectLog(EXPECTED_LOG);
});

test('should print import traces if module build failed in build', async ({
build,
}) => {
const rsbuild = await build({
catchBuildError: true,
});

expect(rsbuild.buildError).toBeTruthy();
await rsbuild.expectLog(EXPECTED_LOG);
});
1 change: 1 addition & 0 deletions e2e/cases/diagnostic/import-traces/src/child1.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import './child2';
1 change: 1 addition & 0 deletions e2e/cases/diagnostic/import-traces/src/child2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import './child3';
1 change: 1 addition & 0 deletions e2e/cases/diagnostic/import-traces/src/child3.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import './child4';
1 change: 1 addition & 0 deletions e2e/cases/diagnostic/import-traces/src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import './child1';
46 changes: 32 additions & 14 deletions packages/core/src/helpers/format.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ function resolveFileName(stats: StatsError) {
stats.moduleName;

if (file) {
return formatFileName(file);
return file;
}

// `moduleIdentifier` is the absolute path with inline loaders
Expand All @@ -30,26 +30,44 @@ function resolveFileName(stats: StatsError) {
if (matched) {
const fileName = matched.pop();
if (fileName) {
return formatFileName(fileName);
return fileName;
}
}
}

return '';
}

function resolveModuleTrace(stats: StatsError) {
let traceStr = '';
if (stats.moduleTrace) {
for (const trace of stats.moduleTrace) {
if (trace.originName) {
// TODO: missing moduleTrace.dependencies[].loc in rspack
traceStr += `\n @ ${trace.originName}`;
}
}
/**
* Format the module trace, the output be like:
* Import traces (entry → error):
* ./src/index.tsx
* ./src/App.tsx
* ./src/Foo.tsx ×
*/
function formatModuleTrace(stats: StatsError, errorFile: string) {
if (!stats.moduleTrace) {
return;
}
Comment thread
chenjiahan marked this conversation as resolved.

const moduleNames = stats.moduleTrace
.map((trace) => trace.originName)
.filter(Boolean) as string[];

if (!moduleNames.length) {
return;
}

return traceStr;
if (errorFile) {
moduleNames.unshift(`${errorFile} ${color.bold(color.red('×'))}`);
}

const rawTrace = moduleNames
.reverse()
Comment on lines +62 to +66

Copilot AI Sep 30, 2025

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The errorFile is being added to moduleNames with formatting, but this formatted string will be processed again in the reverse() and map() operations on lines 65-68. This could result in incorrect formatting or the error marker appearing in the wrong position in the final output.

Copilot uses AI. Check for mistakes.
.map((item) => `\n ${item}`)
.join('');

return color.dim(`Import traces (entry → error):${rawTrace}`);
}

function hintUnknownFiles(message: string): string {
Expand Down Expand Up @@ -202,9 +220,9 @@ export function formatStatsError(stats: StatsError, verbose?: boolean): string {
const details =
verbose && stats.details ? `\nDetails: ${stats.details}\n` : '';
const stack = verbose && stats.stack ? `\n${stats.stack}` : '';
const moduleTrace = resolveModuleTrace(stats);
const moduleTrace = formatModuleTrace(stats, fileName) ?? '';

message = `${fileName}${mainMessage}${details}${stack}${moduleTrace}`;
message = `${formatFileName(fileName)}${mainMessage}${details}${stack}${moduleTrace}`;

// Remove inner error message
const innerError = '-- inner error --';
Expand Down
Loading