getMessagesFromFile crashes when processing malformed JavaScript files
❌ This issue is not open for contribution. Visit Contributing guidelines to learn about the contributing process and how to find suitable issues.
What happened?
The function getMessagesFromFile in packages/kolibri-i18n/src/astUtils.js throws an unhandled exception when attempting to extract translation messages from a JavaScript file with syntax errors. This causes the entire translation extraction process to crash instead of gracefully handling the malformed file.
Expected behavior
The function should gracefully handle malformed JavaScript files by:
- Catching the parsing error and logging a clear error message identifying the problematic file
- Returning an empty messages object (
{}) instead of throwing an exception - Allowing the extraction process to continue processing subsequent files
- Never calling
process.exit()for recoverable parsing errors
Actual behavior
- When
getMessagesFromFile()is called on a malformed JavaScript file, the AST parsing fails - The error is not properly caught, causing an exception to propagate up the call stack
- The translation extraction tool terminates unexpectedly
- Developers lose all extraction progress and cannot identify which file caused the failure
- The current error handling pattern for non-JS/Vue files is not consistently applied to syntax errors
Steps to reproduce
- Create a file named
malformed.jswith invalid JavaScript syntax:this is not valid javascript const = something - Run translation extraction tool pointing to this file:
node astUtils.getMessagesFromFile('path/to/malformed.js') - Observe that the extraction crashes instead of logging an error and continuing
Environment
- Node.js version: 16+ (typical for Kolibri projects)
- Operating System: Platform-independent (JavaScript issue)
- Affected module:
packages/kolibri-i18n/src/astUtils.js - Affected function:
getMessagesFromFile(filePath, verbose)
Related code locations
- Main function:
packages/kolibri-i18n/src/astUtils.js:633-658(getMessagesFromFile) - Helper functions:
getAstFromFile(filePath)- throws on parsing errorsextract$trs(ast, filePath)- assumes valid AST existsextractCreateTranslator(ast, filePath)- assumes valid AST exists
- Test file:
packages/kolibri-i18n/src/__tests__/astUtils.spec.js- Test suites: "File Type Handling", "AST parsing issues"
Anything else?
This issue affects the developer experience when working with i18n message extraction in large projects. A single malformed file can block an entire extraction pipeline. The existing codebase already has successful patterns for handling file type errors (non-.js/.vue files) that should be extended to cover syntax parsing errors.
Unit tests exist that verify the expected behavior but may currently be failing:
- "should handle Vue files with missing script section"
- "should handle malformed JavaScript files"
- These tests in
astUtils.spec.jsdemonstrate that error handling should log errors without callingprocess.exit()