kolibri icon indicating copy to clipboard operation
kolibri copied to clipboard

getMessagesFromFile crashes when processing malformed JavaScript files

Open DarshanCodes09 opened this issue 7 months ago • 0 comments

❌ 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:

  1. Catching the parsing error and logging a clear error message identifying the problematic file
  2. Returning an empty messages object ({}) instead of throwing an exception
  3. Allowing the extraction process to continue processing subsequent files
  4. 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

  1. Create a file named malformed.js with invalid JavaScript syntax:
    this is not valid javascript
    const = something
    
  2. Run translation extraction tool pointing to this file:
    node astUtils.getMessagesFromFile('path/to/malformed.js')
    
  3. 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 errors
    • extract$trs(ast, filePath) - assumes valid AST exists
    • extractCreateTranslator(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.js demonstrate that error handling should log errors without calling process.exit()

DarshanCodes09 avatar Apr 22 '26 18:04 DarshanCodes09