Skip to content

Watch mode does not rerun tests when a source file outside root changes and fsModuleCache already holds that file #11584

Closed

Description

@quantizor

Describe the bug

Hi, and thank you for the work on Vitest 5.

With the default fsModuleCache: true, the first vitest --watch after a cold cache reruns a test when a linked workspace package's source (a file outside the project root) changes. Every later vitest --watch in the same checkout, once the cache holds the transformed module, prints nothing when the same file changes, and the test is not rerun. Running with --fsModuleCache=false restores the rerun. A file inside root reruns in both cases.

The failure is silent, and the cache entry persists across processes, so the same checkout behaves differently on its first and second vitest --watch.

Where (inferred from reading, not verified with a patch): transformService.ts#L324-L405 restores a cached module and rebuilds its importers/importedModules "to make the watch mode work" (L385), but nothing in that path registers the file with the dev server's file watcher. Vite does that while it transforms a file outside root, which a cache hit skips. Files inside root are already covered by the root watcher, which would explain why they still rerun.

Reproduction

Repository: https://github.com/quantizor/vitest-workspace-repros/tree/main/fscache-watch (steps in the root README). The same files are inline below.

Same layout as the @repro/lib workspace below. There is no optimizer and no --changed involved.

package.json { "name": "repro-root", "private": true, "type": "module" }, pnpm-workspace.yaml packages: [ "packages/*" ], .gitignore node_modules.

packages/lib/package.json

{ "name": "@repro/lib", "version": "0.0.0", "type": "module", "exports": { ".": "./src/index.ts" } }

packages/lib/src/index.ts: export { helper } from "./helper.ts";
packages/lib/src/helper.ts: export const helper = () => 1;

packages/app/package.json

{
  "name": "@repro/app",
  "version": "0.0.0",
  "private": true,
  "type": "module",
  "dependencies": { "@repro/lib": "workspace:*" },
  "devDependencies": { "vitest": "5.0.4" }
}

packages/app/test/uses-lib.test.ts

import { expect, test } from "vitest";
import { helper } from "@repro/lib";

test("uses lib", () => expect(helper()).toBe(1));

(No vitest.config.ts.) Steps:

pnpm install
cd packages/app
pnpm exec vitest --watch        # first run; wait for "Waiting for file changes"
# in another terminal: edit packages/lib/src/helper.ts so it returns 2
#   -> RERUN  ../lib/src/helper.ts x1   (cold cache: works)
# stop vitest (q), then start it again:
pnpm exec vitest --watch
# edit packages/lib/src/helper.ts again
#   -> nothing is printed, no rerun (warm cache)
pnpm exec vitest --watch --fsModuleCache=false
# edit packages/lib/src/helper.ts again
#   -> RERUN  ../lib/src/helper.ts x1

I drove this with a script (spawn vitest --watch, wait for "Waiting for file changes", edit the file, wait 20 s, print what arrived). Results on 5.0.4:

run 1 (cold cache):       RERUN  ../lib/src/helper.ts x1     rerun triggered: true
run 2 (warm cache):       (nothing)                           rerun triggered: false
run 3 (--fsModuleCache=false):  RERUN  ../lib/src/helper.ts x1     rerun triggered: true

The same warm-cache run reruns for an edit to a file inside packages/app (src/local.ts imported by a test), three times out of three. A test that imports the file by a relative path (../../lib/src/helper.ts) behaves like the package import, so the linked symlink is not the trigger; being outside root is.

Expected

Editing packages/lib/src/helper.ts reruns test/uses-lib.test.ts in watch mode, with the cache cold or warm.

Suggested fix

On a cache hit for a file outside root, add the file to the environment's watcher as the uncached path does (untested).

System Info

  System:
    OS: macOS 27.0.1
    CPU: (12) arm64 Apple M2 Pro
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 24.11.0
    npm: 11.6.1
    pnpm: 12.8.1
  npmPackages:
    vite: 8.3.4 (transitive)
    vitest: 5.0.4 => 5.0.4

Used Package Manager

pnpm

Activity

  1. PrithwishDas101 commented on Oct 17, 2026

    @PrithwishDas101

    I traced this to the filesystem-cache restoration path in packages/vitest/src/node/environments/transformService.ts.

    getCachedModule() restores the cached module and reconstructs its importer relationships, but it appears not to register an outside-root source file with the current Vite watcher. That would explain why a cold-cache run watches the file correctly, while a later warm-cache run silently misses changes.

    Would registering the cached outside-root file with the environment's watcher on a cache hit be the intended fix? I'd also add an integration regression test that starts watch mode twice with the same cache directory and verifies that editing the linked workspace source reruns the dependent test on the second run.

    I haven't tested a patch yet, so I'd appreciate confirmation of the watcher lifecycle before implementing it.

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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions