Skip to content

Fix Vitest setupFiles path resolution and workspace detection#1986

Open
mohammedahmed18 wants to merge 2 commits intomainfrom
fix/vitest-setupfiles-path-resolution
Open

Fix Vitest setupFiles path resolution and workspace detection#1986
mohammedahmed18 wants to merge 2 commits intomainfrom
fix/vitest-setupfiles-path-resolution

Conversation

@mohammedahmed18
Copy link
Copy Markdown
Contributor

Problem

Vitest tests were failing with error:

Error: Cannot find module '/workspace/target/extensions/discord/test/setup.ts'

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 include and pool but NOT setupFiles. The relative setupFiles paths from the original config (["test/setup.ts"]) remained active and resolved incorrectly for nested directories:

  • Original path: test/setup.ts (relative to repo root)
  • Incorrectly resolved to: extensions/discord/test/setup.ts (relative to function directory)

Solution

1. Improved Workspace Detection (vitest_runner.py:172-191)

  • Use regex pattern matching instead of substring search
  • Match actual workspace config patterns:
    • defineWorkspace( function calls
    • workspace: [ property assignments
  • Check for separate workspace config files (vitest.workspace.ts/js)
  • Now correctly ignores "workspace" in comments

2. Override setupFiles in Custom Config (vitest_runner.py:235-242)

  • Set setupFiles: [] in the merged config
  • This disables project setup files for Codeflash-generated tests
  • Safe because Codeflash tests are self-contained and don't rely on project setup
  • Prevents all relative path resolution issues

Testing

Added tests/languages/javascript/test_vitest_setupfiles_fix.py with 2 test cases:

  • ✓ Verifies setupFiles is explicitly overridden in generated config
  • ✓ Verifies configs without setupFiles still work correctly

All existing vitest_runner tests pass (17/17).

Verification

Tested with the original failing trace (161e21be-9306-4a4d-a9dc-978f65a1af7a):

  • Before fix: Error: Cannot find module '.../test/setup.ts'
  • After fix: Setup.ts error gone ✓ (tests now encounter a different, unrelated error)

Impact

This fix enables Codeflash to optimize functions in nested directories within projects that have Vitest setupFiles configured.

**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
Copy link
Copy Markdown
Contributor

claude bot commented Apr 3, 2026

Claude finished @mohammedahmed18's task in 4m 25s —— View job


PR Review Summary

  • Triage PR size (SMALL — ~25 lines of production code)
  • Run lint/typecheck (auto-fixed; pushed a style commit)
  • Resolve stale threads (none)
  • Review code changes
  • Check for duplicates

Prek Checks

Prek flagged 2 auto-fixable issues in vitest_runner.py (bad-quotes in regex strings). I also identified and fixed:

  • import re was inside the function body — moved to module-level
  • read_text() was missing encoding="utf-8" — added per code style rules
  • Tests used tempfile.TemporaryDirectory() — replaced with tmp_path fixture per testing conventions
  • Test functions lacked -> None return type annotations — added

Committed as: style: fix linting issues in vitest setupFiles PR

Code Review

Bug: _is_vitest_workspace only checks for workspace config files inside the try block that requires vitest.config.ts to exist (vitest_runner.py:166-192)

The function returns early if neither vitest.config.ts nor vitest.config.js exists. The check for vitest.workspace.ts/js at line 188-190 is therefore only reachable when a vitest.config.ts/js is also present. A project could have vitest.workspace.ts as the sole config without a vitest.config.ts, which would go undetected. However, this is arguably a pre-existing structural limitation and low-risk in practice.

Correctness concern: setupFiles: [] may break tests that genuinely need setup (vitest_runner.py:261)

Disabling all setupFiles is a blunt approach. If a project uses setupFiles to set up mocks, globals, or polyfills that the source code under test relies on (not just the test file itself), then Codeflash-generated tests may fail for reasons unrelated to correctness. The PR argues these tests are "self-contained", but that's only true if the source code doesn't depend on side effects from setup files (e.g., globalThis.fetch = ..., process.env.NODE_ENV = 'test', etc.).

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 setupFiles: [] will be the cause. Fix this →

Minor: regex pattern doesn't handle vitest.workspace.mts/.mjs (vitest_runner.py:188-190)

The code checks for .ts and .js variants but misses .mts and .mjs. Low risk since these are uncommon, but inconsistent with how the rest of the file handles Vitest config extensions.

No bugs introduced — the workspace detection improvement (regex vs substring) is correct and safe.

Duplicate Detection

No duplicates detected. Both changed functions exist only in vitest_runner.py.

Test Coverage

New tests in test_vitest_setupfiles_fix.py cover both the setupFiles-present and setupFiles-absent paths. The workspace detection change lacks dedicated tests (no test for the false-positive "workspace in comment" case), but existing tests pass.


Last updated: 2026-04-03T20:08Z
| Branch

- 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>
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