fix: Add .js extensions to relative imports in ESM TypeScript projects#1957
fix: Add .js extensions to relative imports in ESM TypeScript projects#1957
Conversation
## Problem Generated tests for ESM TypeScript projects were importing from relative paths without .js extensions (e.g., `import X from './module'`), causing ERR_MODULE_NOT_FOUND errors when tests run. Node.js ESM requires explicit .js extensions for relative imports, even when the source files are .ts. This is a TypeScript/ESM specification requirement. ## Solution Added `add_js_extensions_to_relative_imports()` function that: - Adds .js extensions to relative imports (./x or ../x) without extensions - Preserves imports that already have extensions (.js, .ts, etc.) - Leaves non-relative imports (node modules) unchanged - Only runs for ESM projects (CommonJS doesn't need extensions) Integrated into test processing pipeline after module system conversion. ## Testing - Added 7 unit tests covering various import patterns - All 35 module_system tests pass - All 315 JavaScript language tests pass - Verified fix resolves ERR_MODULE_NOT_FOUND for trace 17751b8f-fa61-48bc-bdee-b924f0c7afc4 ## References Trace IDs with this issue: 17751b8f-fa61-48bc-bdee-b924f0c7afc4, 3b985200-a906-4c54-a685-df40361d6b2c, 91795877-3ccf-482c-86bd-748834b76f6e, 0298c59c-8980-4aed-b05d-b94940a6544f, ec2864a4-0de0-4ce9-9ec8-b545c82a4f53 Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
|
Codeflash Bot seems not to be a GitHub user. You need a GitHub account to be able to sign the CLA. If you have already a GitHub account, please add the email address used for this commit to your account. You have signed the CLA already but the status is still pending? Let us recheck it. |
|
Claude finished @mohammedahmed18's task in 5m 31s —— View job PR Review Summary
Prek ChecksAll ruff checks pass. No issues to fix. Code ReviewRegex misses
# Before
r"(import\s+(?:(?:\{[^}]*\})|(?:\*\s+as\s+\w+)|(?:\w+))\s+from\s+)..."
# After
r"(import\s+(?:type\s+)?(?:(?:\{[^}]*\})|(?:\*\s+as\s+\w+)|(?:\w+))\s+from\s+)..."Mixed default+named imports not matched (low)
Inconsistent import placement in
Docstrings on new functions (low)
Duplicate Detection
Test Coverage7 new unit tests added and all pass. Missing coverage for:
Optimization PRs
Last updated: 2026-04-01 |
| return "\n".join(lines) | ||
|
|
||
|
|
||
| def add_js_extensions_to_relative_imports(code: str) -> str: |
There was a problem hiding this comment.
Not sure if this is existing, the question remains that we are not following some conviction on function name and claude code end up creating the same functions quickly in some other helper file than searching for existing logics
Summary
./module→./module.js)Problem
Generated tests were importing from relative paths without
.jsextensions:This causes
ERR_MODULE_NOT_FOUNDin Node.js ESM, which requires explicit extensions for relative imports, even when source files are.ts.Solution
Added
add_js_extensions_to_relative_imports()function that:.jsto relative imports without extensionsTest Plan
17751b8f-fa61-48bc-bdee-b924f0c7afc4Affected Trace IDs
17751b8f-fa61-48bc-bdee-b924f0c7afc4,3b985200-a906-4c54-a685-df40361d6b2c,91795877-3ccf-482c-86bd-748834b76f6e,0298c59c-8980-4aed-b05d-b94940a6544f,ec2864a4-0de0-4ce9-9ec8-b545c82a4f53🤖 Generated with Claude Code