Skip to content

Commit

Permalink
fix: keep the message of stack up-to-date (#5640)
Browse files Browse the repository at this point in the history
  • Loading branch information
TrickyPi committed Sep 12, 2024
1 parent 766dbf9 commit fc240ed
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 30 deletions.
79 changes: 50 additions & 29 deletions src/utils/logs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,9 @@ export function augmentLogMessage(log: AugmentedRollupLog): void {
const position = log.loc ? ` (${log.loc.line}:${log.loc.column})` : '';
prefix += `${relativeId(id)}${position}: `;
}

const oldMessage = log.message;
log.message = prefix + log.message;
tweakStackMessage(log, oldMessage);
}

// Error codes should be sorted alphabetically while errors should be sorted by
Expand Down Expand Up @@ -513,34 +514,51 @@ export function logCannotAssignModuleToChunk(
};
}

function tweakStackMessage(error: RollupLog, oldMessage: string): RollupLog {
if (!error.stack) {
return error;
}
error.stack = error.stack.replace(oldMessage, error.message);
return error;
}

export function logCannotBundleConfigAsEsm(originalError: Error): RollupLog {
return {
cause: originalError,
code: INVALID_CONFIG_MODULE_FORMAT,
message: `Rollup transpiled your configuration to an ES module even though it appears to contain CommonJS elements. To resolve this, you can pass the "--bundleConfigAsCjs" flag to Rollup or change your configuration to only contain valid ESM code.\n\nOriginal error: ${originalError.message}`,
stack: originalError.stack,
url: getRollupUrl(URL_BUNDLE_CONFIG_AS_CJS)
};
return tweakStackMessage(
{
cause: originalError,
code: INVALID_CONFIG_MODULE_FORMAT,
message: `Rollup transpiled your configuration to an ES module even though it appears to contain CommonJS elements. To resolve this, you can pass the "--bundleConfigAsCjs" flag to Rollup or change your configuration to only contain valid ESM code.\n\nOriginal error: ${originalError.message}`,
stack: originalError.stack,
url: getRollupUrl(URL_BUNDLE_CONFIG_AS_CJS)
},
originalError.message
);
}

export function logCannotLoadConfigAsCjs(originalError: Error): RollupLog {
return {
cause: originalError,
code: INVALID_CONFIG_MODULE_FORMAT,
message: `Node tried to load your configuration file as CommonJS even though it is likely an ES module. To resolve this, change the extension of your configuration to ".mjs", set "type": "module" in your package.json file or pass the "--bundleConfigAsCjs" flag.\n\nOriginal error: ${originalError.message}`,
stack: originalError.stack,
url: getRollupUrl(URL_BUNDLE_CONFIG_AS_CJS)
};
return tweakStackMessage(
{
cause: originalError,
code: INVALID_CONFIG_MODULE_FORMAT,
message: `Node tried to load your configuration file as CommonJS even though it is likely an ES module. To resolve this, change the extension of your configuration to ".mjs", set "type": "module" in your package.json file or pass the "--bundleConfigAsCjs" flag.\n\nOriginal error: ${originalError.message}`,
stack: originalError.stack,
url: getRollupUrl(URL_BUNDLE_CONFIG_AS_CJS)
},
originalError.message
);
}

export function logCannotLoadConfigAsEsm(originalError: Error): RollupLog {
return {
cause: originalError,
code: INVALID_CONFIG_MODULE_FORMAT,
message: `Node tried to load your configuration as an ES module even though it is likely CommonJS. To resolve this, change the extension of your configuration to ".cjs" or pass the "--bundleConfigAsCjs" flag.\n\nOriginal error: ${originalError.message}`,
stack: originalError.stack,
url: getRollupUrl(URL_BUNDLE_CONFIG_AS_CJS)
};
return tweakStackMessage(
{
cause: originalError,
code: INVALID_CONFIG_MODULE_FORMAT,
message: `Node tried to load your configuration as an ES module even though it is likely CommonJS. To resolve this, change the extension of your configuration to ".cjs" or pass the "--bundleConfigAsCjs" flag.\n\nOriginal error: ${originalError.message}`,
stack: originalError.stack,
url: getRollupUrl(URL_BUNDLE_CONFIG_AS_CJS)
},
originalError.message
);
}

export function logInvalidExportOptionValue(optionValue: string): RollupLog {
Expand Down Expand Up @@ -891,13 +909,16 @@ export function logModuleParseError(error: Error, moduleId: string): RollupLog {
} else if (!moduleId.endsWith('.js')) {
message += ' (Note that you need plugins to import files that are not JavaScript)';
}
return {
cause: error,
code: PARSE_ERROR,
id: moduleId,
message,
stack: error.stack
};
return tweakStackMessage(
{
cause: error,
code: PARSE_ERROR,
id: moduleId,
message,
stack: error.stack
},
error.message
);
}

export function logPluginError(
Expand Down
2 changes: 1 addition & 1 deletion test/cli/samples/config-type-module/_config.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ module.exports = defineTest({
stderr: stderr => {
assertIncludes(
stderr,
'ReferenceError: module is not defined in ES module scope\n' +
'Original error: module is not defined in ES module scope\n' +
"This file is being treated as an ES module because it has a '.js' file extension and"
);
assertIncludes(
Expand Down
3 changes: 3 additions & 0 deletions test/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,9 @@ function normaliseError(error) {
* @param {RollupError} expected
*/
exports.compareError = function compareError(actual, expected) {
if (actual.stack) {
assert.ok(actual.stack.includes(expected.message));
}
actual = normaliseError(actual);
if (expected.frame) {
expected.frame = deindent(expected.frame);
Expand Down

0 comments on commit fc240ed

Please sign in to comment.