Skip to content

Resolve directory matches in Vitest test.projects to their config - #2195

Merged
webpro merged 2 commits into
webpro-nl:mainfrom
fixfon:fix/2194-vitest-project-globs
Oct 23, 2026
Merged

webpro merged 2 commits into
webpro-nl:mainfrom
fixfon:fix/2194-vitest-project-globs

Conversation

@fixfon

@fixfon fixfon commented Oct 22, 2026

Copy link
Copy Markdown
Contributor

Closes #2194

String entries in test.projects were only globbed for files, and each match was loaded as a Vitest config. With projects: ['packages/*'], knip tried to load packages/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/legacy apply. Matched directories resolve to vitest.config.*, falling back to vite.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 vitest19 fixture, which fails without the change and passes with it on both bun and node. The other vitest plugin tests and pnpm run ci pass too.

I used Claude Code for the change and checked the repro and tests locally.

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
@pkg-pr-new

pkg-pr-new Bot commented Oct 22, 2026 •

Copy link
Copy Markdown

Open in StackBlitz

npm i https://pkg.pr.new/knip@2195
npm i https://pkg.pr.new/@knip/language-server@2195
npm i https://pkg.pr.new/@knip/mcp@2195

commit: 349cf75

@webpro webpro left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment on lines +156 to +159
const [files, dirs] = await Promise.all([
_glob({ cwd: options.cwd, patterns, gitignore: false }),
_dirGlob({ cwd: options.cwd, patterns, gitignore: false }),
]);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 webpro left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@webpro
webpro merged commit 4d38613 into webpro-nl:main Oct 23, 2026
32 checks passed
@webpro

webpro commented Oct 24, 2026

Copy link
Copy Markdown
Member

🚀 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.

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 plugin: string test.projects globs match plain files (e.g. CLAUDE.md) and are loaded as vitest configs → exit 2

2 participants