Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Glob patterns with ** are not properly converted to regular expressions #67

Closed
moretti opened this issue Jul 3, 2015 · 4 comments
Closed

Comments

@moretti
Copy link

moretti commented Jul 3, 2015

> minimatch('node_modules/foobar/foo.bar', 'node_modules/foobar/**/*.bar')
> true

> var re = minimatch.makeRe('node_modules/foobar/**/*.bar', { nocase: true });
/^(?:(?=.)node_modules\/(?=.)foobar\/(?:(?!(?:\/|^)\.).)*?\/(?!\.)(?=.)[^/]*?\.bar)$/i
> re.test('node_modules/foobar/foo.bar')
false
@isaacs
Copy link
Owner

isaacs commented Sep 8, 2016

So, here's the thing about this: what you're asking for is really hard, perhaps impossible, because glob expressions are not "regular" in quite the same sense that regular expressions are.

The RegExp that's generated by minimatch's makeRe method are not what node-glob or minimatch actually use for matching **, since ** is intended to match the bash 4.3 behavior identically, and there's no way to do that with the a single regular expression using the engine in JavaScript. (I'm told that it is possible in Perl and using some compiled implementations, but one of the constraints of this module is that it not require any compiled dependencies.)

The double-star is tricky, and there's this edge case where it has to allow any or no directories, none of which can start with a .. Check out the logic that actually handles this in pattern resolution, and you'll start to understand why.

If someone cleverer than me can adjust the regular expression generation such that it works in more cases, I'd be happy to accept a PR. It seems like this should also go in the documentation of the makeRe function, so that it's less of a footgun, at least.

@mmraff
Copy link

mmraff commented Sep 8, 2016

Sorry, I thought I was sure that I canceled my comment, when I started
to anticipate what you have said. Seems like cancellation didn't go
through. Bad GitHub.

On 9/8/16, isaacs notifications@github.com wrote:

So, here's the thing about this: what you're asking for is really hard,
perhaps impossible, because glob expressions are not "regular" in quite the
same sense that regular expressions are.

The RegExp that's generated by minimatch's makeRe method are not what
node-glob or minimatch actually use for matching **, since ** is
intended to match the bash 4.3 behavior identically, and there's no way to
do that with the a single regular expression using the engine in JavaScript.
(I'm told that it is possible in Perl and using some compiled
implementations, but one of the constraints of this module is that it not
require any compiled dependencies.)

The double-star is tricky, and there's this edge case where it has to allow
any or no directories, none of which can start with a .. Check out the
logic that actually handles this in pattern resolution, and you'll start to
understand why.

If someone cleverer than me can adjust the regular expression generation
such that it works in more cases, I'd be happy to accept a PR. It seems
like this should also go in the documentation of the makeRe function, so
that it's less of a footgun, at least.

You are receiving this because you commented.
Reply to this email directly or view it on GitHub:
#67 (comment)

@isaacs
Copy link
Owner

isaacs commented Sep 8, 2016

@mmraff No worries! Actually, I think what you'd posted is a valid bug, separate from this issue (which is about makeRe, not about the base match function).

@isaacs
Copy link
Owner

isaacs commented Feb 15, 2022

a979c06

@isaacs isaacs closed this as completed Feb 15, 2022
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

No branches or pull requests

3 participants