Fix Vitest setupFiles path resolution and workspace detection#1986
Fix Vitest setupFiles path resolution and workspace detection#1986mohammedahmed18 wants to merge 2 commits intomainfrom
Conversation
**Problem:**
1. Vitest tests were failing with 'Cannot find module .../test/setup.ts'
when testing functions in nested directories (e.g., extensions/discord/).
2. Root cause had two parts:
- _is_vitest_workspace() was doing substring search for 'workspace',
matching it even in comments, causing false positives
- Custom vitest config wasn't overriding setupFiles, leaving relative
paths from original config that resolved incorrectly
**Solution:**
1. Improved workspace detection (vitest_runner.py:172-191):
- Use regex to match actual workspace config patterns
- Match defineWorkspace( function calls
- Match workspace: [ property assignments
- Ignore 'workspace' in comments
2. Override setupFiles in custom config (vitest_runner.py:235-242):
- Set setupFiles: [] to disable project setup files
- Prevents relative path resolution issues
- Safe since Codeflash tests are self-contained
**Testing:**
Added test_vitest_setupfiles_fix.py with 2 test cases:
- Verifies setupFiles is overridden in generated config
- Verifies configs without setupFiles still work
**Trace ID:** 161e21be-9306-4a4d-a9dc-978f65a1af7a
Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
|
Claude finished @mohammedahmed18's task in 4m 25s —— View job PR Review Summary
Prek ChecksPrek flagged 2 auto-fixable issues in
Committed as: Code ReviewBug: The function returns early if neither Correctness concern: Disabling all This is a valid trade-off for fixing the reported path resolution crash, but worth noting: if users report unexpected test failures on projects with setup files that configure globals, this Minor: regex pattern doesn't handle The code checks for No bugs introduced — the workspace detection improvement (regex vs substring) is correct and safe. Duplicate DetectionNo duplicates detected. Both changed functions exist only in Test CoverageNew tests in Last updated: 2026-04-03T20:08Z |
- Move `import re` to module-level (was inside function body) - Add encoding="utf-8" to read_text() call - Fix tests: use tmp_path fixture, add -> None return types, add encoding args Co-authored-by: mohammed ahmed <undefined@users.noreply.github.com>
Problem
Vitest tests were failing with error:
This occurred when testing functions in nested directories (e.g.,
extensions/discord/).Trace ID: 161e21be-9306-4a4d-a9dc-978f65a1af7a
Root Cause
Two bugs working together:
1. Workspace Detection Bug
_is_vitest_workspace()was doing a simple substring search for "workspace" in config files. This matched the word even in comments like:// Never count workspace packages/apps toward core coverage thresholds.This caused false positives, preventing custom config creation when it was actually needed.
2. Missing setupFiles Override
When the custom Vitest config was created, it only overrode
includeandpoolbut NOTsetupFiles. The relative setupFiles paths from the original config (["test/setup.ts"]) remained active and resolved incorrectly for nested directories:test/setup.ts(relative to repo root)extensions/discord/test/setup.ts(relative to function directory)Solution
1. Improved Workspace Detection (
vitest_runner.py:172-191)defineWorkspace(function callsworkspace: [property assignmentsvitest.workspace.ts/js)2. Override setupFiles in Custom Config (
vitest_runner.py:235-242)setupFiles: []in the merged configTesting
Added
tests/languages/javascript/test_vitest_setupfiles_fix.pywith 2 test cases:All existing vitest_runner tests pass (17/17).
Verification
Tested with the original failing trace (
161e21be-9306-4a4d-a9dc-978f65a1af7a):Error: Cannot find module '.../test/setup.ts'Impact
This fix enables Codeflash to optimize functions in nested directories within projects that have Vitest setupFiles configured.