Fix TypeScript module system detection in CommonJS packages#1996
Fix TypeScript module system detection in CommonJS packages#1996mohammedahmed18 wants to merge 2 commits intomainfrom
Conversation
Issue #10: TypeScript files (.ts, .tsx) were incorrectly detected as ESM regardless of package.json "type" field. This caused "Cannot use import statement outside a module" errors when the AI service generated ESM imports for TypeScript files in CommonJS packages. Root Cause: - module_system.py lines 73-77 returned ES_MODULE for ALL .ts/.tsx files - Skipped checking package.json "type" field - Confused TypeScript SOURCE syntax (always ESM) with RUNTIME module system Fix: - For .ts/.tsx files, defer to package.json "type" field - If no "type" field, default to CommonJS (Node.js default) - Explicit extensions (.mts, .cts) still override package.json Impact: - Systematic bug affecting all TypeScript files in CommonJS packages - 2 logs in current batch (backend-core/inMemoryQueue.ts) - High severity (blocks test generation) Testing: - 5 new regression tests covering all scenarios - 318 existing JavaScript tests pass - No linting/type errors Trace IDs: 3fccd364-bc04-4196-9f50-f45f29a632d3, 6d220d09-3292-41de-a52c-d0e961e615e9 Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
|
Claude finished @mohammedahmed18's task in 3m 6s —— View job PR Review SummaryPrek Checks
Code ReviewThe fix is correct and well-reasoned. The core logic change in
One minor observation: the Duplicate DetectionNo duplicates detected. Test CoverageSMALL PR — coverage analysis skipped per policy. The PR adds 5 focused regression tests that directly cover the fixed code paths, including all combinations of Verdict: LGTM. The fix correctly addresses the root cause (confusing TypeScript's source syntax with its runtime module system) and is consistent with Node.js behavior. Last updated: 2026-04-04 |
Co-authored-by: mohammed ahmed <undefined@users.noreply.github.com>
Issue #10: TypeScript Module System Detection Bug
Problem
TypeScript files (
.ts,.tsx) were incorrectly detected as ESM regardless ofpackage.json"type"field. This caused "Cannot use import statement outside a module" errors when the AI service generated ESM imports for TypeScript files in CommonJS packages.Root Cause
File:
/opt/codeflash/codeflash/languages/javascript/module_system.py(lines 73-77)The code confused:
import/export)Impact
backend-core/inMemoryQueue.ts)3fccd364-bc04-4196-9f50-f45f29a632d3,6d220d09-3292-41de-a52c-d0e961e615e9Fix
package.json"type"field (don't return early).mts(always ESM) and.cts(always CommonJS) still override package.jsonTesting
✅ 5 new regression tests covering all scenarios:
"type": "commonjs")"type": "module").mtsfiles (always ESM).ctsfiles (always CommonJS)✅ 318 existing JavaScript tests pass (no regressions)
✅ No linting/type errors (
uv run prek)Before/After
Before (broken):
After (fixed):
Files Changed
codeflash/languages/javascript/module_system.py- Fix detection logictests/test_languages/test_javascript_module_system.py- Update tests to match correct behaviortests/test_languages/test_typescript_commonjs_module_detection.py- New regression testsCategory
Systematic bug (reproducible on every TypeScript file in CommonJS packages)