Skip to content

Commit fc240ed

Browse files
authored
fix: keep the message of stack up-to-date (#5640)
1 parent 766dbf9 commit fc240ed

File tree

3 files changed

+54
-30
lines changed

3 files changed

+54
-30
lines changed

src/utils/logs.ts

+50-29
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,9 @@ export function augmentLogMessage(log: AugmentedRollupLog): void {
9393
const position = log.loc ? ` (${log.loc.line}:${log.loc.column})` : '';
9494
prefix += `${relativeId(id)}${position}: `;
9595
}
96-
96+
const oldMessage = log.message;
9797
log.message = prefix + log.message;
98+
tweakStackMessage(log, oldMessage);
9899
}
99100

100101
// Error codes should be sorted alphabetically while errors should be sorted by
@@ -513,34 +514,51 @@ export function logCannotAssignModuleToChunk(
513514
};
514515
}
515516

517+
function tweakStackMessage(error: RollupLog, oldMessage: string): RollupLog {
518+
if (!error.stack) {
519+
return error;
520+
}
521+
error.stack = error.stack.replace(oldMessage, error.message);
522+
return error;
523+
}
524+
516525
export function logCannotBundleConfigAsEsm(originalError: Error): RollupLog {
517-
return {
518-
cause: originalError,
519-
code: INVALID_CONFIG_MODULE_FORMAT,
520-
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}`,
521-
stack: originalError.stack,
522-
url: getRollupUrl(URL_BUNDLE_CONFIG_AS_CJS)
523-
};
526+
return tweakStackMessage(
527+
{
528+
cause: originalError,
529+
code: INVALID_CONFIG_MODULE_FORMAT,
530+
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}`,
531+
stack: originalError.stack,
532+
url: getRollupUrl(URL_BUNDLE_CONFIG_AS_CJS)
533+
},
534+
originalError.message
535+
);
524536
}
525537

526538
export function logCannotLoadConfigAsCjs(originalError: Error): RollupLog {
527-
return {
528-
cause: originalError,
529-
code: INVALID_CONFIG_MODULE_FORMAT,
530-
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}`,
531-
stack: originalError.stack,
532-
url: getRollupUrl(URL_BUNDLE_CONFIG_AS_CJS)
533-
};
539+
return tweakStackMessage(
540+
{
541+
cause: originalError,
542+
code: INVALID_CONFIG_MODULE_FORMAT,
543+
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}`,
544+
stack: originalError.stack,
545+
url: getRollupUrl(URL_BUNDLE_CONFIG_AS_CJS)
546+
},
547+
originalError.message
548+
);
534549
}
535550

536551
export function logCannotLoadConfigAsEsm(originalError: Error): RollupLog {
537-
return {
538-
cause: originalError,
539-
code: INVALID_CONFIG_MODULE_FORMAT,
540-
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}`,
541-
stack: originalError.stack,
542-
url: getRollupUrl(URL_BUNDLE_CONFIG_AS_CJS)
543-
};
552+
return tweakStackMessage(
553+
{
554+
cause: originalError,
555+
code: INVALID_CONFIG_MODULE_FORMAT,
556+
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}`,
557+
stack: originalError.stack,
558+
url: getRollupUrl(URL_BUNDLE_CONFIG_AS_CJS)
559+
},
560+
originalError.message
561+
);
544562
}
545563

546564
export function logInvalidExportOptionValue(optionValue: string): RollupLog {
@@ -891,13 +909,16 @@ export function logModuleParseError(error: Error, moduleId: string): RollupLog {
891909
} else if (!moduleId.endsWith('.js')) {
892910
message += ' (Note that you need plugins to import files that are not JavaScript)';
893911
}
894-
return {
895-
cause: error,
896-
code: PARSE_ERROR,
897-
id: moduleId,
898-
message,
899-
stack: error.stack
900-
};
912+
return tweakStackMessage(
913+
{
914+
cause: error,
915+
code: PARSE_ERROR,
916+
id: moduleId,
917+
message,
918+
stack: error.stack
919+
},
920+
error.message
921+
);
901922
}
902923

903924
export function logPluginError(

test/cli/samples/config-type-module/_config.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ module.exports = defineTest({
88
stderr: stderr => {
99
assertIncludes(
1010
stderr,
11-
'ReferenceError: module is not defined in ES module scope\n' +
11+
'Original error: module is not defined in ES module scope\n' +
1212
"This file is being treated as an ES module because it has a '.js' file extension and"
1313
);
1414
assertIncludes(

test/utils.js

+3
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,9 @@ function normaliseError(error) {
8686
* @param {RollupError} expected
8787
*/
8888
exports.compareError = function compareError(actual, expected) {
89+
if (actual.stack) {
90+
assert.ok(actual.stack.includes(expected.message));
91+
}
8992
actual = normaliseError(actual);
9093
if (expected.frame) {
9194
expected.frame = deindent(expected.frame);

0 commit comments

Comments
 (0)