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

fix: should provide full error message with error name when hideStack is set to true #7867

Merged
merged 2 commits into from
Sep 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
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). `;
Loading