Skip to content

Commit

Permalink
fix: should provide full error message with error name when `hideStac…
Browse files Browse the repository at this point in the history
…k` is set to `true` (#7867)

* fix: init

* fix
  • Loading branch information
h-a-n-a authored Sep 12, 2024
1 parent 501c12f commit fa38b86
Show file tree
Hide file tree
Showing 18 changed files with 141 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
it("should include loader thrown error", () => {
let errored = false;
try {
require("./lib");
} catch (e) {
errored = true;
expect(e.message).toContain("Failed to load");
}
expect(errored).toBeTruthy()
});
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const lib = "lib";
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module.exports = function (context) {
this.emitError(new Error("Failed to load"));
return ""
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/**
* @type {import('@rspack/core').RspackOptions}
*/
module.exports = {
context: __dirname,
module: {
rules: [
{
test: /lib\.js$/,
use: [
{
loader: "./my-loader.js"
}
]
}
]
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
ERROR in ./lib.js
× ModuleError: Failed to load (from: <PROJECT_ROOT>/tests/diagnosticsCases/module-build-failed/loader-emit-error/my-loader.js)
│ at xxx
│ at xxx
│ at xxx
│ at xxx
│ at xxx
│ at xxx
│ at xxx
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
it("should include loader thrown error", () => {
let errored = false;
try {
require("./lib");
} catch (e) {
errored = true;
expect(e.message).toContain("Failed to load");
}
expect(errored).toBeTruthy()
});
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const lib = "lib";
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module.exports = function (context) {
let e;
e = new Error("Failed to load");
e.hideStack = true;
this.emitError(e);

e = new Error("Failed to load");
e.hideStack = true;
this.emitWarning(e);
return ""
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/**
* @type {import('@rspack/core').RspackOptions}
*/
module.exports = {
context: __dirname,
module: {
rules: [
{
test: /lib\.js$/,
use: [
{
loader: "./my-loader.js"
}
]
}
]
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
WARNING in ./lib.js
⚠ ModuleWarning: Failed to load (from: <PROJECT_ROOT>/tests/diagnosticsCases/module-build-failed/loader-emit-hide-stack/my-loader.js)

ERROR in ./lib.js
× ModuleError: Failed to load (from: <PROJECT_ROOT>/tests/diagnosticsCases/module-build-failed/loader-emit-hide-stack/my-loader.js)
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
it("should include loader thrown error", () => {
let errored = false;
try {
require("./lib");
} catch (e) {
errored = true;
expect(e.message).toContain("Failed to load");
}
expect(errored).toBeTruthy()
});
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const lib = "lib";
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module.exports = function (context) {
this.emitWarning(new Error("Failed to load"));
return ""
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/**
* @type {import('@rspack/core').RspackOptions}
*/
module.exports = {
context: __dirname,
module: {
rules: [
{
test: /lib\.js$/,
use: [
{
loader: "./my-loader.js"
}
]
}
]
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
WARNING in ./lib.js
⚠ ModuleWarning: Failed to load (from: <PROJECT_ROOT>/tests/diagnosticsCases/module-build-failed/loader-emit-warning/my-loader.js)
│ at xxx
│ at xxx
│ at xxx
│ at xxx
│ at xxx
│ at xxx
│ at xxx
4 changes: 0 additions & 4 deletions packages/rspack/src/loader-runner/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -618,12 +618,10 @@ export async function runLoaders(
if (!(error instanceof Error)) {
error = new NonErrorEmittedError(error);
}
const hasStack = !!error.stack;
error.name = "ModuleError";
error.message = `${error.message} (from: ${stringifyLoaderObject(
loaderContext.loaders[loaderContext.loaderIndex]
)})`;
!hasStack && Error.captureStackTrace(error);
error = concatErrorMsgAndStack(error);
(error as RspackError).moduleIdentifier = this._module.identifier();
compiler._lastCompilation!.__internal__pushDiagnostic({
Expand All @@ -636,12 +634,10 @@ export async function runLoaders(
if (!(warning instanceof Error)) {
warning = new NonErrorEmittedError(warning);
}
const hasStack = !!warning.stack;
warning.name = "ModuleWarning";
warning.message = `${warning.message} (from: ${stringifyLoaderObject(
loaderContext.loaders[loaderContext.loaderIndex]
)})`;
hasStack && Error.captureStackTrace(warning);
warning = concatErrorMsgAndStack(warning);
(warning as RspackError).moduleIdentifier = this._module.identifier();
compiler._lastCompilation!.__internal__pushDiagnostic({
Expand Down
12 changes: 11 additions & 1 deletion packages/rspack/src/util/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,17 @@ export function concatErrorMsgAndStack(
}
const hideStack = "hideStack" in err && err.hideStack;
if (!hideStack && "stack" in err) {
err.message = err.stack || err.message;
// This is intended to be different than webpack,
// here we want to treat the almost the same as `Error.stack` just without the stack.
// Webpack uses `Error.message`, however it does not contain the `Error.prototype.name`
// `xxx` -> `Error: xxx`. So they behave the same even if `hideStack` is set to `true`.
err.message = err.stack || err.toString();
} else {
// This is intended to be different than webpack,
// here we want to treat the almost the same as `Error.stack` just without the stack.
// Webpack uses `Error.message`, however it does not contain the `Error.prototype.name`
// `xxx` -> `Error: xxx`. So they behave the same even if `hideStack` is set to `true`.
err.message = err.toString();
}
// maybe `null`, use `undefined` to compatible with `Option<String>`
err.stack = err.stack || undefined;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
// decide bailout on builtin css module

module.exports = `WARNING in ./style.css
⚠ use type 'css' and \`CssExtractRspackPlugin\` together, please set \`experiments.css\` to \`false\` or set \`{ type: "javascript/auto" }\` for rules with \`CssExtractRspackPlugin\` in your rspack config (now \`CssExtractRspackPlugin\` does nothing).
ModuleWarning: use type 'css' and \`CssExtractRspackPlugin\` together, please set \`experiments.css\` to \`false\` or set \`{ type: "javascript/auto" }\` for rules with \`CssExtractRspackPlugin\` in your rspack config (now \`CssExtractRspackPlugin\` does nothing).
WARNING in ./style.css
⚠ ModuleWarning: You can't use \`experiments.css\` (\`experiments.futureDefaults\` enable built-in CSS support by default) and \`css-loader\` together, please set \`experiments.css\` to \`false\` or set \`{ type: "javascript/auto" }\` for rules with \`css-loader\` in your webpack config (now css-loader does nothing). `;

2 comments on commit fa38b86

@rspack-bot
Copy link

Choose a reason for hiding this comment

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

📝 Benchmark detail: Open

Name Base (2024-09-12 b1bf153) Current Change
10000_development-mode + exec 2.25 s ± 51 ms 2.23 s ± 17 ms -0.92 %
10000_development-mode_hmr + exec 724 ms ± 4.9 ms 697 ms ± 6.7 ms -3.66 %
10000_production-mode + exec 2.84 s ± 49 ms 2.85 s ± 29 ms +0.62 %
arco-pro_development-mode + exec 1.84 s ± 71 ms 1.86 s ± 63 ms +1.35 %
arco-pro_development-mode_hmr + exec 434 ms ± 1.5 ms 436 ms ± 3.9 ms +0.41 %
arco-pro_production-mode + exec 3.24 s ± 86 ms 3.25 s ± 56 ms +0.45 %
arco-pro_production-mode_generate-package-json-webpack-plugin + exec 3.31 s ± 72 ms 3.35 s ± 80 ms +1.43 %
threejs_development-mode_10x + exec 1.68 s ± 11 ms 1.68 s ± 12 ms -0.21 %
threejs_development-mode_10x_hmr + exec 807 ms ± 15 ms 808 ms ± 2.6 ms +0.05 %
threejs_production-mode_10x + exec 5.14 s ± 42 ms 5.19 s ± 35 ms +0.89 %

@rspack-bot
Copy link

Choose a reason for hiding this comment

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

📝 Ran ecosystem CI: Open

suite result
modernjs ✅ success
_selftest ✅ success
nx ❌ failure
rspress ✅ success
rslib ❌ failure
rsbuild ❌ failure
examples ✅ success

Please sign in to comment.