Skip to content

Fix glob ? wildcard in non-final path segments#1242

Merged
nfischer merged 1 commit intoshelljs:mainfrom
veeceey:fix/issue-1197-glob-wildcard
Feb 14, 2026
Merged

Fix glob ? wildcard in non-final path segments#1242
nfischer merged 1 commit intoshelljs:mainfrom
veeceey:fix/issue-1197-glob-wildcard

Conversation

@veeceey
Copy link
Copy Markdown
Contributor

@veeceey veeceey commented Feb 13, 2026

Fixes #1197

The ? wildcard stopped working in non-final path segments after the switch to fast-glob in v0.9.0. For example:

common.expand(['test/r?sources/file1.txt'])
// Returns: ['test/r?sources/file1.txt'] (literal, not expanded)
// Expected: ['test/resources/file1.txt']

Root cause

fast-glob uses glob-parent to determine the base directory for filesystem walking. glob-parent correctly identifies * as a glob character but fails to recognize ? in non-final path segments. So for test/r?sources/file1.txt, it returns test/r?sources as the base directory (a non-existent path), and the glob returns no matches.

Fix

Before passing patterns to fast-glob, convert standalone ? characters to the equivalent character class [^/]. Both match exactly one non-slash character, but glob-parent handles [^/] correctly and returns the right base directory.

The conversion is careful to preserve:

  • ? inside character classes (e.g. [?] for literal ?)
  • Escaped ? (e.g. \?)
  • Extglob patterns (e.g. ?(...))

Testing

  • Un-skipped the existing test for this issue
  • Added 4 new tests covering ? in directory names, combined with *, and ? in both directory and file segments
  • All 615 passing tests still pass, no regressions

fast-glob's dependency glob-parent does not recognize the ? wildcard
when it appears in non-final path segments (e.g. test/r?sources/file.txt).
It incorrectly treats "test/r?sources" as a literal directory name, causing
the glob to fail silently and return no matches.

Work around this by converting standalone ? characters to the equivalent
character class [^/] before passing patterns to fast-glob. Both match
exactly one non-slash character, but glob-parent correctly handles [^/].

The conversion preserves ? inside character classes ([?]), escaped
characters (\?), and extglob patterns (?(...)).

Un-skip the existing test for this case and add new test coverage.

Fixes shelljs#1197
@codecov
Copy link
Copy Markdown

codecov Bot commented Feb 14, 2026

Codecov Report

❌ Patch coverage is 68.18182% with 7 lines in your changes missing coverage. Please review.
✅ Project coverage is 97.09%. Comparing base (013fc00) to head (635dab8).
⚠️ Report is 6 commits behind head on main.

Files with missing lines Patch % Lines
src/common.js 68.18% 7 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1242      +/-   ##
==========================================
- Coverage   97.52%   97.09%   -0.43%     
==========================================
  Files          36       36              
  Lines        1493     1514      +21     
==========================================
+ Hits         1456     1470      +14     
- Misses         37       44       +7     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Copy link
Copy Markdown
Member

@nfischer nfischer left a comment

Choose a reason for hiding this comment

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

Wow, thanks so much for fixing this!

@nfischer
Copy link
Copy Markdown
Member

Let me know if you'd also like to contribute unit tests to cover the special cases in your code (handling \\, [, and ] characters). This would be a welcome addition!

@nfischer nfischer merged commit 1dd3fe7 into shelljs:main Feb 14, 2026
9 of 11 checks passed
@nfischer nfischer modified the milestones: v0.9.x, v0.10.x Feb 14, 2026
@nfischer nfischer added fix Bug/defect, or a fix for such a problem glob labels Apr 16, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

fix Bug/defect, or a fix for such a problem glob

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Glob only expands "?" wildcard if it's in last path segment

2 participants