Skip to content

Conversation

bryanchen-d
Copy link
Contributor

Files with square brackets in their names (e.g.,foo[.js, component[id].tsx) leads to unexpected error when using "Search Only in Open Editors" because square brackets were interpreted as glob character classes instead of literal characters.

Fixes #233049

Root Cause

When "Search Only in Open Editors" builds search queries, it creates include patterns from open file paths to restrict search scope. However, these file paths were used directly as glob patterns without escaping special characters. Square brackets [...] have special meaning in glob syntax - they define character classes that match any single character from the bracketed set. So a file named component[id].tsx created the pattern component[id].tsx, which the glob matcher interpreted as "component" + (any character from {i,d}) + ".tsx", failing to match the literal filename component[id].tsx.

Solution

Applied the existing escapeGlobPattern function to file paths before adding them to include patterns in the commonQueryFromFileList method. This function escapes special glob characters by wrapping them in brackets - converting component[id].tsx to component[[]id[]].tsx, where [[] represents a literal opening bracket and []] represents a literal closing bracket. The escaped pattern now matches the literal filename correctly. Added comprehensive tests to verify the fix handles square brackets properly without breaking existing functionality.

@Copilot Copilot AI review requested due to automatic review settings October 9, 2025 18:59
@bryanchen-d bryanchen-d added search Search widget and operation issues search-editor labels Oct 9, 2025
@bryanchen-d bryanchen-d added this to the October 2025 milestone Oct 9, 2025
@bryanchen-d bryanchen-d requested review from roblourens and removed request for Copilot October 9, 2025 18:59
@bryanchen-d bryanchen-d assigned bryanchen-d and unassigned roblourens Oct 9, 2025
@bryanchen-d bryanchen-d requested a review from Copilot October 9, 2025 18:59
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR fixes a bug where files with square brackets in their names (e.g., foo[.js, component[id].tsx) were not found when using "Search Only in Open Editors" because square brackets were being interpreted as glob character classes instead of literal characters.

  • Applied the existing escapeGlobPattern function to file paths before adding them to include patterns
  • Added comprehensive tests to verify the fix handles square brackets properly

Reviewed Changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.

File Description
src/vs/workbench/services/search/common/queryBuilder.ts Applied escapeGlobPattern to file paths in commonQueryFromFileList method to escape special glob characters
src/vs/workbench/services/search/test/common/queryBuilder.test.ts Added test case to verify escapeGlobPattern properly handles square brackets and added glob import
src/vs/workbench/services/search/test/browser/queryBuilder.test.ts Added blank lines (formatting only)

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
search Search widget and operation issues search-editor
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Search Only in Open Editors does not work if file name has square bracket
2 participants