Skip to content

fix: strip file extensions from JS/TS import paths in generated tests#1215

Merged
misrasaurabh1 merged 2 commits intomainfrom
fix/strip-js-import-extensions
Jan 31, 2026
Merged

fix: strip file extensions from JS/TS import paths in generated tests#1215
misrasaurabh1 merged 2 commits intomainfrom
fix/strip-js-import-extensions

Conversation

@misrasaurabh1
Copy link
Contributor

@misrasaurabh1 misrasaurabh1 commented Jan 31, 2026

Summary

Fixes the root cause of "Cannot find module" errors when running JS/TS tests.

The add_js_extension() function in module_system.py was adding .js extensions when converting CommonJS require() to ESM import statements. TypeScript and Jest don't require explicit extensions and adding them causes module resolution failures.

Problem

When the CLI converted CommonJS to ESM:

// Before (CommonJS from aiservice)
const { func } = require('../utils/helper');

// After conversion (ESM with added .js - WRONG!)
import { func } from '../utils/helper.js';

This caused:

Cannot find module '../utils/helper.js' from 'test/test.ts'

Solution

Modified add_js_extension() to NOT add extensions. TypeScript and modern bundlers handle extension resolution automatically.

Note: Extension stripping for LLM-generated tests is now handled by the aiservice (see codeflash-internal PR #2341).

Test plan

  • All existing tests pass
  • Manual testing confirms no more "Cannot find module" errors

🤖 Generated with Claude Code

LLMs often add .js extensions to TypeScript import paths (e.g.,
`import { func } from '../module.js'`), but TypeScript's module resolution
doesn't require explicit extensions. This causes "Cannot find module" errors
when Jest tries to resolve these imports.

This change adds `strip_js_extensions()` to remove .js/.ts/.tsx/.jsx/.mjs/.mts
extensions from relative import paths in generated tests. The function handles:
- ES module imports: import { x } from '../path/file.js'
- CommonJS requires: require('../path/file.js')
- Jest mocks: jest.mock('../path/file.js'), jest.doMock(), etc.

External package imports (lodash, react, etc.) and alias imports (@/components)
are preserved.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
@misrasaurabh1 misrasaurabh1 force-pushed the fix/strip-js-import-extensions branch from e684ce7 to 92a18ed Compare January 31, 2026 03:18
The `add_js_extension()` function in `module_system.py` was adding .js
extensions when converting CommonJS requires to ESM imports. This caused
"Cannot find module" errors because TypeScript/Jest module resolution
doesn't require explicit extensions.

This change modifies `add_js_extension()` to NOT add extensions, as
TypeScript and modern bundlers handle extension resolution automatically.

Note: The aiservice now handles stripping any extensions that LLMs might
add (see codeflash-internal PR #2341), so CLI-side stripping is removed.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
@misrasaurabh1 misrasaurabh1 force-pushed the fix/strip-js-import-extensions branch from c53bd2c to a3d6f47 Compare January 31, 2026 04:33
@misrasaurabh1 misrasaurabh1 merged commit e8989c2 into main Jan 31, 2026
18 of 26 checks passed
@misrasaurabh1 misrasaurabh1 deleted the fix/strip-js-import-extensions branch January 31, 2026 04:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant