Skip to content

fix: escape meta characters in paths during brace expansion#108

Merged
bmatcuk merged 2 commits intobmatcuk:masterfrom
toga4:fix-brace-exp-with-meta
Jan 10, 2026
Merged

fix: escape meta characters in paths during brace expansion#108
bmatcuk merged 2 commits intobmatcuk:masterfrom
toga4:fix-brace-exp-with-meta

Conversation

@toga4
Copy link
Copy Markdown
Contributor

@toga4 toga4 commented Jan 7, 2026

Overview

This PR fixes an issue where brace expansion patterns (e.g., {a,b}) fail to match correctly when the directory path contains glob meta characters (*, ?, [, ], {, }).

Problem

When using patterns like e/**/{z,other} to match files in directories with meta characters (e.g., e/[x]/[y]/z), the globbing would fail because the directory path e/[x]/[y] was being passed to buildAlt without escaping, causing the meta characters to be interpreted as glob patterns rather than literal characters.

Solution

  • Added escapeMeta function in utils.go to escape meta characters in paths
  • Modified globAlts in glob.go and doGlobAltsWalk in globwalk.go to escape the directory path d before passing it to buildAlt
  • Renamed metaReplacer to unescapeMetaReplacer for clarity

Test cases added

  • e/**/{z,other} matching e/[x]/[y]/z - directory with bracket characters
  • f/\{a,b\}/{c,other} matching f/{a,b}/c - directory with literal braces
  • f/\*/{a,other} matching f/*/a - directory with literal asterisk (non-Windows)
  • f/\?/{a,other} matching f/?/a - directory with literal question mark (non-Windows)

toga4 added 2 commits January 8, 2026 00:26
Add test cases that demonstrate the bug where brace expansion fails to match paths containing literal meta characters in directory names.

The following patterns fail to match correctly:
- e/**/{z,other} with e/[x]/[y]/z: brackets interpreted as character class
- f/\{a,b\}/{c,other} with f/{a,b}/c: braces interpreted as brace expansion
- f/\*/{a,other} with f/*/a: asterisk interpreted as wildcard (over-matches)
- f/\?/{a,other} with f/?/a: question mark interpreted as wildcard (over-matches)

These tests will pass once the fix for escaping meta characters in globAlts/buildAlt is implemented.
When processing brace expansion (e.g., `{a,b}`), the globAlts and doGlobAltsWalk functions build new patterns by combining filesystem paths with pattern fragments.
Previously, paths containing meta characters (e.g., `[orderID]`, `{a,b}`, `*`, `?`) were passed directly to buildAlt without escaping, causing them to be interpreted as glob patterns rather than literal path components.

This fix adds an escapeMeta function (the inverse of unescapeMeta) and applies it to filesystem-derived paths before building alternative patterns.

This ensures that:

- `[...]` in paths are not interpreted as character classes
- `{...}` in paths are not interpreted as brace expansions
- `*` and `?` in paths are not interpreted as wildcards

Also renamed metaReplacer to unescapeMetaReplacer for clarity.
@bmatcuk bmatcuk self-assigned this Jan 10, 2026
@bmatcuk bmatcuk added the bug label Jan 10, 2026
@bmatcuk bmatcuk merged commit 4ef2b00 into bmatcuk:master Jan 10, 2026
0 of 2 checks passed
@bmatcuk
Copy link
Copy Markdown
Owner

bmatcuk commented Jan 10, 2026

Thanks! Looks like there were a couple tests that were broken: tests using WithFailOnPatternNotExist were failing because the pattern f/whatever/{c,other} would end up trying the pattern f/whatever/other which had no meta characters and did not exist. Additionally, a test was failing on Windows because the new test directories/files weren't created on Windows, so the expected count was wrong.

@toga4
Copy link
Copy Markdown
Contributor Author

toga4 commented Jan 10, 2026

Thanks a lot for fixing the CI issues and merging this PR.
I also noticed that a new version has already been released — really appreciate the quick turnaround.
Thanks for your work on maintaining this project!

@bmatcuk
Copy link
Copy Markdown
Owner

bmatcuk commented Jan 10, 2026

No worries! Glad you find it useful =)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants