Repository navigation
fix: skip Vitest fs module cache for modules importing macros - #180
Merged
Merged
Conversation
|
|
commit: |
sxzz
approved these changes
Oct 29, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #179
Vitest's
fsModuleCachekeys 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
configureVitesthook to the Vite plugin that registers a cache key generator. It returnsfalse(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.vitest@^5.0.0as an optional peer dependency (defineCacheKeyGeneratoris Vitest 5 API). On older Vitest the hook does nothing.mayContainMacros.Verified with the reproduction from the issue (macro reading a
.templatefile,fsModuleCache: true): without this change the second run fails withexpected '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: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.readFileSync) unless the macro declares them.