Skip to content

fsModuleCache (on by default since 5.0.4) fails every test when the project directory is not writable #11777

Closed

Description

@fey

Describe the bug

Since 5.0.4 fsModuleCache is on by default (#11435). When the project directory is not writable, Vitest fails every test file instead of running without the cache:

FAIL  sum.test.js [ sum.test.js ]
Error: ENOENT: no such file or directory, mkdir '/app/node_modules/.vitest-cache'

On a non-root user over a root-owned project the same happens with EACCES: permission denied, mkdir '<root>/node_modules/.vitest-cache'.

The cache is an optimization, but mkdirSync(cacheRoot, { recursive: true }) in FileSystemModuleCache has no error handling (fsModuleCache.ts at v5.0.5), so a failed directory creation turns into a failed suite. The same setup passes on 5.0.3.

Where this hits: read-only container root filesystems, sandboxes that mount the project read-only and run tests as another user, CI with a pre-installed node_modules owned by root. In our case (an education platform running student exercises in locked-down containers) every JS exercise broke after bumping to 5.0.5. --no-fsModuleCache or fsModuleCachePath work around it, but each project has to know about it, and there is no environment variable to set it once per image.

Suggested fix: if the cache directory cannot be created or written (EACCES, EPERM, EROFS, ENOENT on a read-only fs), log a single warning and continue with the fs module cache disabled. I'm happy to send a PR with a test if this direction is accepted.

Reproduction

FROM node:24-slim
ARG V=5.0.5
WORKDIR /app
RUN echo '{"type":"module"}' > package.json && npm i -D vitest@${V} \
 && echo "import { test, expect } from 'vitest'; test('ok', () => expect(1).toBe(1))" > sum.test.js
docker build -t vitest-ro --build-arg V=5.0.5 .
docker run --rm --read-only --tmpfs /tmp -e HOME=/tmp vitest-ro npx vitest run
# FAIL sum.test.js  Error: ENOENT: no such file or directory, mkdir '/app/node_modules/.vitest-cache'

docker run --rm --read-only --tmpfs /tmp -e HOME=/tmp vitest-ro npx vitest run --no-fsModuleCache
# Tests 1 passed

docker build -t vitest-ro-503 --build-arg V=5.0.3 .
docker run --rm --read-only --tmpfs /tmp -e HOME=/tmp vitest-ro-503 npx vitest run
# Tests 1 passed

(HOME=/tmp is needed because the API token is written under $HOME or node_modules/.vitest; without it startup fails earlier with a similar error, which may be worth the same treatment.)

System Info

Docker image node:24-slim (Debian), Node.js 24, vitest 5.0.5; host Linux 6.8

Used Package Manager

npm

Validations

Activity

  1. added
    p3-minor-bug
    approved
    on Oct 28, 2026
  2. github-actions commented on Oct 28, 2026

    @github-actions

    This issue is approved, so the team accepts a pull request for it. To work on it, open a pull request against main and link this issue in its description with a closing keyword, for example Fixes #11777.

    The team accepts one pull request per issue: if another pull request for this issue is already open, a new one is closed automatically. See our pull request policy for more context.

  3. OKAN-SOFTWARE commented on Oct 28, 2026

    @OKAN-SOFTWARE

    I'm interested in contributing a focused fix for this issue, including regression coverage for read-only project directories. I see the issue is approved. Before I begin, could you confirm whether a PR is already being prepared?

  4. locked and limited conversation to collaborators on Nov 14, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    approvedp3-minor-bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions