Skip to content

fix: escape () and [] in scan directory globs - #627

Open
tzh476 wants to merge 2 commits into
unplugin:mainfrom
tzh476:fix/escape-scan-dir-glob-special-chars
Open

fix: escape () and [] in scan directory globs#627
tzh476 wants to merge 2 commits into
unplugin:mainfrom
tzh476:fix/escape-scan-dir-glob-special-chars

Conversation

@tzh476

@tzh476 tzh476 commented Aug 17, 2026

Copy link
Copy Markdown

Summary

  • Directory names containing () or [] (e.g. Code(Template)) are treated as picomatch / tinyglobby syntax, so dirs scanning and Vite HMR watchers match nothing.
  • Escape those characters after normalizeScanDirs and scan the escaped globs directly. #419 was closed without merge because the change needed to live next to glob resolution with an explanation; resolveGlobsExclude has since moved into unimport, and feeding escaped paths back through it turns \ into /.

Fixes #416

Test plan

  • vitest run (44 tests)
  • New coverage: project root Code(Template) and Code[Template] scan useSpecial; HMR matchers accept files under Code(Template)

Made with Cursor

@bolt-new-by-stackblitz

Copy link
Copy Markdown

Review PR in StackBlitz Codeflow Run & review this pull request in StackBlitz Codeflow.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 4063450d96

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread src/core/ctx.ts Outdated
? dirs.flatMap(dir => normalizeScanDirs([dir], {
...dirsScanOptions,
cwd: root,
})).map(dir => ({ ...dir, glob: escapeGlobPathChars(dir.glob) }))

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Preserve user-supplied glob syntax when escaping paths

When dirs or dirsScanOptions.filePatterns intentionally contains parentheses or a character class (for example, dirs: ['src/[ab]'] or filePatterns: ['*.[jt]s']), this post-normalization replacement escapes that syntax along with the literal project-root portion. Both directory scanning and the precompiled HMR matchers then look for literal brackets/parentheses instead of the requested glob matches, so previously supported configurations stop discovering exports. Escape only the literal resolved path prefix while preserving the user-supplied glob expression.

Useful? React with 👍 / 👎.

@tzh476
tzh476 force-pushed the fix/escape-scan-dir-glob-special-chars branch from 4063450 to 2a0b3d6 Compare August 17, 2026 16:28
Project paths like Code(Template) were treated as picomatch extglob
groups, so dir scanning and HMR watchers matched nothing.
@tzh476
tzh476 force-pushed the fix/escape-scan-dir-glob-special-chars branch from 2a0b3d6 to 1982e87 Compare August 17, 2026 16:28
The review pointed out that escaping the whole normalized glob breaks patterns the
user wrote deliberately: with `dirs: ['src/[ab]']` the character class became a
literal, so a directory named `[ab]` was required and `src/a` / `src/b` no longer
matched. Verified against unimport's normalizeScanDirs plus picomatch:

  dirs: ['src/[ab]']    before escaping -> src/a/x.ts true,  src/b/x.ts true
                        after  escaping -> src/a/x.ts false, src/b/x.ts false

Escape only the root prefix instead. That part is a real filesystem path, so
parentheses and brackets in it are always literal; everything after it comes from
the user's own `dirs` entry and is left untouched.

Two tests added, both failing with the previous implementation:
  - a character class in `dirs` still matches multiple directories
  - the same under a project path containing parentheses, so both behaviours hold
    at once

`npx vitest run`: 4 files, 46 tests, all passing.

Change-Id: I9be2a6c4c8122928e0d00373818a808eac174c62
Signed-off-by: tzh476 <tzh476@gmail.com>
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.

Directory with special characters does not work correctly

1 participant