Repository navigation
Resolve directory matches in Vitest test.projects to their config - #2195
Conversation
String entries in `test.projects` were globbed for files only, and every
match was loaded as a config. `packages/*` crashed on `packages/CLAUDE.md`
("Unknown file extension .md", exit 2) and never followed the project
directories.
Match Vitest: glob all string entries together so negated patterns apply,
resolve matched directories to `vitest.config.*` or else `vite.config.*`,
and skip files that are not named like a config.
Closes webpro-nl#2194
commit: |
webpro
left a comment
There was a problem hiding this comment.
Thanks for the pull request! Resolving directory matches is a real improvement: with projects: ['packages/*'] in a single workspace, the project configs and their setup files were reported as unused before, and now they are picked up. There's one change needed before we merge, explained inline.
On the question in the description: Vitest does not skip a plain file matched by a projects glob, it throws The projects glob matched a file "packages/CLAUDE.md", but it should also either start with "vitest.config"/"vite.config" or match the pattern "(vitest|vite).*.config.*". So skipping these files cannot break a config that works in Vitest, and Knip no longer exits with a load error over a file that was never a config. Let's keep it.
| const [files, dirs] = await Promise.all([ | ||
| _glob({ cwd: options.cwd, patterns, gitignore: false }), | ||
| _dirGlob({ cwd: options.cwd, patterns, gitignore: false }), | ||
| ]); |
There was a problem hiding this comment.
The globs run once for every item in configs, and a function config contributes 8 of them (one per command, mode and SSR combination). So for a function config, both globs and the findFileWithExtensions lookups run 8 times over the same patterns. The single glob was already repeated before this change, but now each round has two globs plus up to 12 file checks per matched directory. With 200 project directories, resolveConfig for the root config went from about 17ms to 66ms.
Could you collect the string patterns from all configs first and glob once? For example:
const patterns = new Set<string>();
for (const cfg of configs) {
for (const project of cfg.test?.projects ?? []) {
if (typeof project === 'string') patterns.add(project);
else if (typeof project.extends === 'string') inputs.add(toConfig('vitest', project.extends));
}
}
if (patterns.size > 0) {
const [files, dirs] = await Promise.all([
_glob({ cwd: options.cwd, patterns: [...patterns], gitignore: false }),
_dirGlob({ cwd: options.cwd, patterns: [...patterns], gitignore: false }),
]);
// ...unchanged
}In the same setup this brings it down to about 11ms, faster than before this change, with identical results.
There was a problem hiding this comment.
Thanks, done in 349cf75. The string patterns from all configs now go into one Set and get globbed once. On my 200-project setup with the function config it went from ~31ms to ~4ms, and the resolved entries are the same.
One side effect: since it's a single glob now, a negated pattern in one config applies to the patterns from the other configs too. I think that's fine for this case, but let me know if you'd rather keep them separate.
A function config yields 8 configs (one per command, mode and SSR combination) that share the same `test.projects`. The file and directory globs and the config lookups ran once per config, over the same patterns. Collect the string patterns from all configs into a Set and run one `_glob` and one `_dirGlob`.
webpro
left a comment
There was a problem hiding this comment.
Thank you for the quick update and for checking the numbers! On the negated patterns: they all come from the same root config (one per command and mode evaluation), and Vitest evaluates it only once, so a single shared glob is fine. Looks good to merge.
|
🚀 This pull request is included in v6.43.0. See Release 6.43.0 for release notes. Using Knip in a commercial project? Please consider becoming a sponsor. |
Closes #2194
String entries in
test.projectswere only globbed for files, and each match was loaded as a Vitest config. Withprojects: ['packages/*'], knip tried to loadpackages/CLAUDE.md, failed with "Unknown file extension .md" (exit 2), and never picked up the project directories.Now all string entries are globbed together, so negated patterns like
!packages/legacyapply. Matched directories resolve tovitest.config.*, falling back tovite.config.*. Matched files are only used if they're named like a config.One thing to flag: a plain file that isn't named like a config is now skipped. That follows how Vitest 4 treats
projects. Happy to change it if you'd rather keep the old behavior for those.Tested with the new
vitest19fixture, which fails without the change and passes with it on both bun and node. The other vitest plugin tests andpnpm run cipass too.I used Claude Code for the change and checked the repro and tests locally.