Skip to content

Commit e80a0ca

Browse files
jeanp413bpasero
andauthored
Fixes glob patterns **/p* incorrectly match on /foo/ap (#144473)
* Fixes #144458 * fix tests Co-authored-by: Benjamin Pasero <[email protected]>
1 parent b9c773e commit e80a0ca

File tree

3 files changed

+7
-6
lines changed

3 files changed

+7
-6
lines changed

src/vs/base/common/glob.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,17 +47,18 @@ const PATH_REGEX = '[/\\\\]'; // any slash or backslash
4747
const NO_PATH_REGEX = '[^/\\\\]'; // any non-slash and non-backslash
4848
const ALL_FORWARD_SLASHES = /\//g;
4949

50-
function starsToRegExp(starCount: number): string {
50+
function starsToRegExp(starCount: number, isLastPattern?: boolean): string {
5151
switch (starCount) {
5252
case 0:
5353
return '';
5454
case 1:
5555
return `${NO_PATH_REGEX}*?`; // 1 star matches any number of characters except path separator (/ and \) - non greedy (?)
5656
default:
57-
// Matches: (Path Sep OR Path Val followed by Path Sep OR Path Sep followed by Path Val) 0-many times
57+
// Matches: (Path Sep OR Path Val followed by Path Sep) 0-many times except when it's the last pattern
58+
// in which case also matches (Path Sep followed by Path Val)
5859
// Group is non capturing because we don't need to capture at all (?:...)
5960
// Overall we use non-greedy matching because it could be that we match too much
60-
return `(?:${PATH_REGEX}|${NO_PATH_REGEX}+${PATH_REGEX}|${PATH_REGEX}${NO_PATH_REGEX}+)*?`;
61+
return `(?:${PATH_REGEX}|${NO_PATH_REGEX}+${PATH_REGEX}${isLastPattern ? `|${PATH_REGEX}${NO_PATH_REGEX}+` : ''})*?`;
6162
}
6263
}
6364

@@ -135,7 +136,7 @@ function parseRegExp(pattern: string): string {
135136
return;
136137
}
137138

138-
regEx += starsToRegExp(2);
139+
regEx += starsToRegExp(2, index === segments.length - 1);
139140
}
140141

141142
// Anything else, not globstar

src/vs/base/test/common/glob.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -725,7 +725,7 @@ suite('Glob', () => {
725725
assert.strictEqual(glob.match(expr, 'foo.as'), null);
726726
});
727727

728-
test.skip('expression with non-trivia glob (issue 144458)', function () {
728+
test('expression with non-trivia glob (issue 144458)', function () {
729729
let pattern = '**/p*';
730730

731731
assert.strictEqual(glob.match(pattern, 'foo/barp'), false);

src/vs/workbench/services/search/test/common/ignoreFile.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -477,7 +477,7 @@ suite('Parsing .gitignore files', () => {
477477
});
478478

479479
runTest({
480-
pattern: `[._]*.s[a-w][a-z]
480+
pattern: `[._]*s[a-w][a-z]
481481
[._]s[a-w][a-z]
482482
*.un~
483483
*~`,

0 commit comments

Comments
 (0)