Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/vs/workbench/services/search/common/queryBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -313,11 +313,11 @@ export class QueryBuilder {
}

const relPath = path.relative(searchRoot.fsPath, file.fsPath);
assertReturnsDefined(folderQuery.includePattern)[relPath.replace(/\\/g, '/')] = true;
assertReturnsDefined(folderQuery.includePattern)[escapeGlobPattern(relPath.replace(/\\/g, '/'))] = true;
} else {
if (file.fsPath) {
hasIncludedFile = true;
includePattern[file.fsPath] = true;
includePattern[escapeGlobPattern(file.fsPath)] = true;
}
}
});
Expand Down
24 changes: 24 additions & 0 deletions src/vs/workbench/services/search/test/common/queryBuilder.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import assert from 'assert';
import * as glob from '../../../../../base/common/glob.js';
import { isWindows } from '../../../../../base/common/platform.js';
import { URI } from '../../../../../base/common/uri.js';
import { ensureNoDisposablesAreLeakedInTestSuite } from '../../../../../base/test/common/utils.js';
Expand All @@ -29,4 +30,27 @@ suite('QueryBuilderCommon', () => {
const actual = resolveResourcesForSearchIncludes([URI.file(isWindows ? "C:\\testWorkspace\\pages\\blog\\[postId]" : "/testWorkspace/pages/blog/[postId]")], context);
assert.deepStrictEqual(actual, ["./pages/blog/[[]postId[]]"]);
});

test('escapeGlobPattern properly escapes square brackets for literal matching', () => {
// This test verifies the fix for issue #233049 where files with square brackets in names
// were not found when using "Search Only in Open Editors"

// Test file name with square brackets
const fileName = 'file[test].txt';

// Function matching the one used in queryBuilder.ts
function escapeGlobPattern(path: string): string {
Copy link
Member

Choose a reason for hiding this comment

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

But then this isn't testing the real function right?

return path.replace(/([?*[\]])/g, '[$1]');
}

// Without escaping, the pattern treats [test] as a character class
const unescapedResult = glob.match(fileName, fileName);
assert.strictEqual(unescapedResult, false, 'Unescaped pattern should not match due to character class interpretation');

// With escaping, the pattern matches literally
const escapedPattern = escapeGlobPattern(fileName);
const escapedResult = glob.match(escapedPattern, fileName);
assert.strictEqual(escapedResult, true, 'Escaped pattern should match literally');
assert.strictEqual(escapedPattern, 'file[[]test[]].txt', 'Pattern should have escaped brackets');
});
});
Loading