Skip to content

Commit e8989c2

Browse files
Merge pull request #1215 from codeflash-ai/fix/strip-js-import-extensions
fix: strip file extensions from JS/TS import paths in generated tests
2 parents 7e34379 + a3d6f47 commit e8989c2

3 files changed

Lines changed: 14 additions & 6 deletions

File tree

codeflash/languages/javascript/module_system.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -184,10 +184,18 @@ def _get_relative_import_path(target_path: Path, source_path: Path) -> str:
184184

185185

186186
def add_js_extension(module_path: str) -> str:
187-
"""Add .js extension to relative module paths for ESM compatibility."""
188-
if module_path.startswith(("./", "../")):
189-
if not module_path.endswith(".js") and not module_path.endswith(".mjs"):
190-
return module_path + ".js"
187+
"""Process module path for ESM compatibility.
188+
189+
NOTE: This function intentionally does NOT add extensions because:
190+
1. TypeScript projects resolve modules without explicit extensions
191+
2. Adding .js to .ts imports causes "Cannot find module" errors
192+
3. Modern bundlers (webpack, vite, etc.) handle extension resolution automatically
193+
194+
The function name is preserved for backward compatibility but the behavior
195+
has been changed to NOT add extensions.
196+
"""
197+
# Previously this function added .js extensions, but this caused module resolution
198+
# errors in TypeScript projects. We now preserve paths without adding extensions.
191199
return module_path
192200

193201

codeflash/verification/verifier.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ def generate_tests(
7575
func_name = function_to_optimize.function_name
7676
qualified_name = function_to_optimize.qualified_name
7777

78-
# First validate and fix import styles
78+
# Validate and fix import styles (default vs named exports)
7979
generated_test_source = validate_and_fix_import_style(generated_test_source, source_file, func_name)
8080

8181
# Convert module system if needed (e.g., CommonJS -> ESM for ESM projects)

tests/test_languages/test_javascript_instrumentation.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -651,4 +651,4 @@ def test_this_method_call_exact_output(self):
651651

652652
expected = " return codeflash.capture('Class.fibonacci', '1', this.fibonacci.bind(this), n - 1);"
653653
assert transformed == expected, f"Expected:\n{expected}\nGot:\n{transformed}"
654-
assert counter == 1
654+
assert counter == 1

0 commit comments

Comments
 (0)