Skip to content

fix: skip Vitest fs module cache for modules importing macros - #180

Merged
sxzz merged 1 commit into
mainfrom
fix/macro-cache-stale-read-file
Oct 29, 2026
Merged

sxzz merged 1 commit into
mainfrom
fix/macro-cache-stale-read-file

Conversation

@sxzzbot

@sxzzbot sxzzbot commented Oct 29, 2026 •

Copy link
Copy Markdown
Contributor

Closes #179

Vitest's fsModuleCache keys a module by its id, its own source, and the config. When a macro, or a file a macro reads, changes, the importing module keeps the same key, so Vitest reuses stale macro output on the next run.

This adds a configureVitest hook to the Vite plugin that registers a cache key generator. It returns false (opting the module out of the cache) when the source may contain an import with the configured macro attributes (options.attrs). The generator runs before the transform, so the check is a cheap regex over the source. It can match modules that don't import macros (they just aren't cached), but shouldn't miss a real macro import.

  • Add vitest@^5.0.0 as an optional peer dependency (defineCacheKeyGenerator is Vitest 5 API). On older Vitest the hook does nothing.
  • Add unit tests for mayContainMacros.

Verified with the reproduction from the issue (macro reading a .template file, fsModuleCache: true): without this change the second run fails with expected 'one\n' to be 'two\n'; with it, consecutive runs pass after editing the template.

This is a workaround, similar to how Vitest already bails out of the cache for modules containing import.meta.glob(. A complete fix would need both sides:

  • Vitest would have to store extra dependency files (e.g. those reported via this.addWatchFile()) with each cache entry and check them on read. Today an entry only records its imports and checks that they still resolve, not whether their content changed.
  • The plugin would have to report those files. It can report macro modules and their imports, but not files a macro reads at runtime (e.g. via readFileSync) unless the macro declares them.

@bolt-new-by-stackblitz

Copy link
Copy Markdown

Review PR in StackBlitz Codeflow Run & review this pull request in StackBlitz Codeflow.

@pkg-pr-new

pkg-pr-new Bot commented Oct 29, 2026

Copy link
Copy Markdown

Open in StackBlitz

npm i https://pkg.pr.new/unplugin/unplugin-macros@180

commit: ebf3d4c

@sxzz sxzz changed the title fix(vite): skip Vitest fs module cache for modules importing macros fix: skip Vitest fs module cache for modules importing macros Oct 29, 2026
@sxzz
sxzz merged commit 83a742f into main Oct 29, 2026
12 checks passed
@sxzz
sxzz deleted the fix/macro-cache-stale-read-file branch October 29, 2026 04:39
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.

Vitest's module cache keeps old macro output after a read file changes

2 participants