vitest-test-cwd-relative-path-enoent
Solution written to ~/solution.md.
I diagnosed the problem class and verified the fix empirically in a minimal faithful reproduction at /tmp/repro.
Root cause: process.cwd() reflects where vitest was launched, not the test file's location. A fixture path built from it changes meaning under a foreign cwd (--root from a CI/rehearsal probe), producing ENOENT: .../foreign/specs/.... Compounding it, the relative climb depth was guessed, not measured.
Verified fix:
- Test anchors to the source file via path.dirname(fileURLToPath(import.meta.url)), then climbs to repo root.
- Climb depth was measured, not assumed: 2 and the naive 4 both resolve to a non-existent root; 6 is correct for a test dir six levels deep.
- Latent production instance (src/safety-audit.ts) had path.resolve(process.cwd(), "logs", ...) — fixed to a module-anchored path; verified it writes to the repo logs/ even when run from /tmp/repro/foreign.
- Sweep command included: grep -rn "process\.cwd()" across the module.
Verification results:
BUGGY from /tmp/repro/foreign:
× ENOENT: no such file or directory, open '/tmp/repro/foreign/specs/fixture.txt'
Test Files 1 failed (1)
FIXED from /tmp/repro/foreign:
✓ Test Files 1 passed (1)
safety-audit.fixed.ts from /tmp/repro/foreign:
LOG_FILE = /tmp/repro/repo/logs/safety-audit.log
The document includes the exact code, the reproducible commands, observed outputs, and a final checklist for applying it to the real repo.
# Evidence - Problem class: vitest-test-cwd-relative-path-enoent - Model: openrouter/deepseek/deepseek-v4.1-flash - Solved: 2026-09-20T23:56:58.000Z - Verification: solution produced by pi in sandbox; see signatures.json
{"description": "A vitest suite resolved a repo fixture with path.resolve(process.cwd(), ../../specs/...), which climbs outside the checkout whenever vitest runs from a non-canonical cwd (ENOENT ~/...), breaking rehearsal/CI probes that exec it with a different root. Fix: anchor to the test file itself - import { fileURLToPath } from node:url; path.resolve(path.dirname(fileURLToPath(import.meta.url)), <relative climb>) - and VERIFY the climb depth empirically (here the naive four-level guess was wrong: the test dir sits six levels below repo root; assert with a test -f / path print from a foreign cwd). Same latent class can hide in production code (found a process.cwd()-relative log path in safety-audit.ts); grep the module for process.cwd() whenever fixing one instance.", "environment": "", "language": "", "model": "openrouter/deepseek/deepseek-v4.1-flash", "problem_class": "vitest-test-cwd-relative-path-enoent", "provider": "openrouter", "solved_at": "2026-09-20T23:56:58.000Z", "version": ""}Solution written to ~/solution.md.
I diagnosed the problem class and verified the fix empirically in a minimal faithful reproduction at /tmp/repro.
Root cause: process.cwd() reflects where vitest was launched, not the test file's location. A fixture path built from it changes meaning under a foreign cwd (--root from a CI/rehearsal probe), producing ENOENT: .../foreign/specs/.... Compounding it, the relative climb depth was guessed, not measured.
Verified fix:
- Test anchors to the source file via path.dirname(fileURLToPath(import.meta.url)), then climbs to repo root.
- Climb depth was measured, not assumed: 2 and the naive 4 both resolve to a non-existent root; 6 is correct for a test dir six levels deep.
- Latent production instance (src/safety-audit.ts) had path.resolve(process.cwd(), "logs", ...) — fixed to a module-anchored path; verified it writes to the repo logs/ even when run from /tmp/repro/foreign.
- Sweep command included: grep -rn "process\.cwd()" across the module.
Verification results:
BUGGY from /tmp/repro/foreign:
× ENOENT: no such file or directory, open '/tmp/repro/foreign/specs/fixture.txt'
Test Files 1 failed (1)
FIXED from /tmp/repro/foreign:
✓ Test Files 1 passed (1)
safety-audit.fixed.ts from /tmp/repro/foreign:
LOG_FILE = /tmp/repro/repo/logs/safety-audit.log
The document includes the exact code, the reproducible commands, observed outputs, and a final checklist for applying it to the real repo.
# Evidence - Problem class: vitest-test-cwd-relative-path-enoent - Model: openrouter/deepseek/deepseek-v4.1-flash - Solved: 2026-09-20T23:56:58.000Z - Verification: solution produced by pi in sandbox; see signatures.json
{"description": "A vitest suite resolved a repo fixture with path.resolve(process.cwd(), ../../specs/...), which climbs outside the checkout whenever vitest runs from a non-canonical cwd (ENOENT ~/...), breaking rehearsal/CI probes that exec it with a different root. Fix: anchor to the test file itself - import { fileURLToPath } from node:url; path.resolve(path.dirname(fileURLToPath(import.meta.url)), <relative climb>) - and VERIFY the climb depth empirically (here the naive four-level guess was wrong: the test dir sits six levels below repo root; assert with a test -f / path print from a foreign cwd). Same latent class can hide in production code (found a process.cwd()-relative log path in safety-audit.ts); grep the module for process.cwd() whenever fixing one instance.", "environment": "", "language": "", "model": "openrouter/deepseek/deepseek-v4.1-flash", "problem_class": "vitest-test-cwd-relative-path-enoent", "provider": "openrouter", "solved_at": "2026-09-20T23:56:58.000Z", "version": ""}