Skip to content

Commit 7727ca8

Browse files
authored
fix(workspace): fix glob pattern detection (#6502)
1 parent f7da619 commit 7727ca8

File tree

10 files changed

+112
-1
lines changed

10 files changed

+112
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
type Pattern = string
2+
type PatternTypeOptions = any
3+
4+
// copy of fast-glob's isDynamicPattern until it's implemented on tinyglobby
5+
// https://github.com/SuperchupuDev/tinyglobby/issues/28
6+
// https://github.com/mrmlnc/fast-glob/blob/da648078ae87bce81fcd42e64d5b2b1360c47b09/src/utils/pattern.ts#L35
7+
8+
/*
9+
MIT License
10+
11+
Copyright (c) Denis Malinochkin
12+
13+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
14+
15+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
16+
17+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
18+
*/
19+
20+
const ESCAPE_SYMBOL = '\\'
21+
22+
const COMMON_GLOB_SYMBOLS_RE = /[*?]|^!/
23+
const REGEX_CHARACTER_CLASS_SYMBOLS_RE = /\[[^[]*\]/
24+
const REGEX_GROUP_SYMBOLS_RE = /(?:^|[^!*+?@])\([^(]*\|[^|]*\)/
25+
const GLOB_EXTENSION_SYMBOLS_RE = /[!*+?@]\([^(]*\)/
26+
const BRACE_EXPANSION_SEPARATORS_RE = /,|\.\./
27+
28+
export function isDynamicPattern(pattern: Pattern, options: PatternTypeOptions = {}): boolean {
29+
/**
30+
* A special case with an empty string is necessary for matching patterns that start with a forward slash.
31+
* An empty string cannot be a dynamic pattern.
32+
* For example, the pattern `/lib/*` will be spread into parts: '', 'lib', '*'.
33+
*/
34+
if (pattern === '') {
35+
return false
36+
}
37+
38+
/**
39+
* When the `caseSensitiveMatch` option is disabled, all patterns must be marked as dynamic, because we cannot check
40+
* filepath directly (without read directory).
41+
*/
42+
if (options.caseSensitiveMatch === false || pattern.includes(ESCAPE_SYMBOL)) {
43+
return true
44+
}
45+
46+
if (COMMON_GLOB_SYMBOLS_RE.test(pattern) || REGEX_CHARACTER_CLASS_SYMBOLS_RE.test(pattern) || REGEX_GROUP_SYMBOLS_RE.test(pattern)) {
47+
return true
48+
}
49+
50+
if (options.extglob !== false && GLOB_EXTENSION_SYMBOLS_RE.test(pattern)) {
51+
return true
52+
}
53+
54+
if (options.braceExpansion !== false && hasBraceExpansion(pattern)) {
55+
return true
56+
}
57+
58+
return false
59+
}
60+
61+
function hasBraceExpansion(pattern: string): boolean {
62+
const openingBraceIndex = pattern.indexOf('{')
63+
64+
if (openingBraceIndex === -1) {
65+
return false
66+
}
67+
68+
const closingBraceIndex = pattern.indexOf('}', openingBraceIndex + 1)
69+
70+
if (closingBraceIndex === -1) {
71+
return false
72+
}
73+
74+
const braceContent = pattern.slice(openingBraceIndex, closingBraceIndex)
75+
76+
return BRACE_EXPANSION_SEPARATORS_RE.test(braceContent)
77+
}

packages/vitest/src/node/workspace/resolveWorkspace.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import type { UserConfig, UserWorkspaceConfig, WorkspaceProjectConfiguration } f
88
import type { WorkspaceProject } from '../workspace'
99
import { initializeProject } from '../workspace'
1010
import { configFiles as defaultConfigFiles } from '../../constants'
11+
import { isDynamicPattern } from './fast-glob-pattern'
1112

1213
export async function resolveWorkspace(
1314
vitest: Vitest,
@@ -158,7 +159,7 @@ async function resolveWorkspaceProjectConfigs(
158159
const stringOption = definition.replace('<rootDir>', vitest.config.root)
159160
// if the string doesn't contain a glob, we can resolve it directly
160161
// ['./vitest.config.js']
161-
if (!stringOption.includes('*')) {
162+
if (!isDynamicPattern(stringOption)) {
162163
const file = resolve(vitest.config.root, stringOption)
163164

164165
if (!existsSync(file)) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"name": "a"
3+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import { test } from 'vitest';
2+
3+
test('test - a')
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"name": "b"
3+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import { test } from 'vitest';
2+
3+
test('test - b')
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"name": "c"
3+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import { test } from 'vitest';
2+
3+
test('test - c')
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export default [
2+
'packages/*',
3+
'!packages/b'
4+
]

test/config/test/workspace.test.ts

+11
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,17 @@ it('correctly resolves workspace projects with a several folder globs', async ()
3535
expect(stdout).toContain('test - b')
3636
})
3737

38+
it('supports glob negation pattern', async () => {
39+
const { stderr, stdout } = await runVitest({
40+
root: 'fixtures/workspace/negated',
41+
workspace: './fixtures/workspace/negated/vitest.workspace.ts',
42+
})
43+
expect(stderr).toBe('')
44+
expect(stdout).toContain('test - a')
45+
expect(stdout).toContain('test - c')
46+
expect(stdout).not.toContain('test - b')
47+
})
48+
3849
it('fails if project names are identical with a nice error message', async () => {
3950
const { stderr } = await runVitest({
4051
root: 'fixtures/workspace/invalid-duplicate-configs',

0 commit comments

Comments
 (0)