Skip to content

Fix glob() not negating bracket expressions that start with "^" - #24422

Open
nicolas-grekas wants to merge 2 commits into
php:PHP-8.5from
nicolas-grekas:glob-circumflex-negation
Open

nicolas-grekas wants to merge 2 commits into
php:PHP-8.5from
nicolas-grekas:glob-circumflex-negation

Conversation

@nicolas-grekas

Copy link
Copy Markdown
Contributor

Since #18164, glob() uses the bundled implementation on all platforms. That implementation negates a bracket expression only when it starts with !. Before, on Linux, the system glob() also negated [^a]. Since 8.5, ^ is a literal there:

// in a directory that contains the files a, b and ^
glob('[^a]'); // 8.4 on Linux: ['^', 'b'], 8.5: ['^', 'a']

POSIX leaves [^...] unspecified, but glibc and our own fnmatch() implementation treat it like [!...]. This PR does the same in glob(). On Windows, [^a] now negates too, like fnmatch() already does there.

Found while working on symfony/symfony#67133.

@nicolas-grekas
nicolas-grekas requested a review from bukka as a code owner October 19, 2026 07:24
nicolas-grekas added a commit to symfony/symfony that referenced this pull request Oct 19, 2026
…or "^" in glob patterns (nicolas-grekas)

This PR was merged into the 8.2 branch.

Discussion
----------

[Finder] Negate bracket expressions that start with "!" or "^" in glob patterns

| Q             | A
| ------------- | ---
| Branch?       | 8.2
| Bug fix?      | no
| New feature?  | no
| Deprecations? | no
| Issues        | -
| License       | MIT

Follow-up to #67075. In glob patterns, `[!a]` and `[^a]` matched `!` or `^`, or `a`. They now match any character except `a`, as `fnmatch()` does. Like `?`, they don't match `/`, except when the regexp is built with `$strictWildcardSlash = false`, as AssetMapper does.

This changes what `Finder::name()` and `notName()`, the `**` patterns of `GlobResource`, the `excluded_patterns` of AssetMapper and the `places` globs of Workflow match when a bracket starts with `!` or `^`. The native `glob()` path of `GlobResource` already negated them, so both paths now agree. Except for `[^`: since PHP 8.5, and on Windows, `glob()` reads `^` as a literal. php/php-src#24422 fixes that.

Commits
-------

6f55435 [Finder] Negate bracket expressions that start with "!" or "^" in glob patterns
@NattyNarwhal

Copy link
Copy Markdown
Member

I would make a note that it's a deviation from OpenBSD's glob implementation (less obvious than i.e. the ZendMM adaptation) in case we need to sync again in the future.

@nicolas-grekas

Copy link
Copy Markdown
Contributor Author

Good point, comment updated.

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants