Skip to content

Negation not working when pattern begins with ! #62

Closed
forty8bits opened this Issue Mar 12, 2013 · 11 comments

8 participants

@forty8bits

Beginning a pattern with ! seems to be broken. The result actually matches the non-negated pattern... so for example:

glob('!package.json', function(err, res)...

returns res as ['package.json'], whereas

glob('./!(package.json)', function(err, res)...

returns an array containing all files/directories in the cwd except package.json, as expected.

@isaacs
Owner
isaacs commented Apr 12, 2013

Yep, this is a bug.

@isaacs
Owner
isaacs commented Apr 12, 2013

It's not property respecting the this.minimatch.negate flag, which should flip all the comparisons.

@laurelnaiad laurelnaiad referenced this issue in afterglowtech/angular-detour May 23, 2013
Open

clean up ignore in bower.json #3

@kschzt
kschzt commented May 31, 2013

@isaacs something like !(node_modules) should also list the entire tree, except for node_modules. This case is being incorrectly switched to https://github.com/isaacs/node-glob/blob/master/glob.js#L335 .. It then tries to stat '(node_modules)', which doesn't exist, and returns nothing. Easy to catch via https://github.com/isaacs/node-glob/blob/master/test/00-setup.js#L103

@sindresorhus

👍

@passy
passy commented Nov 11, 2013

👍

@SBoudrias

👍

@AluisioASG AluisioASG added a commit to AluisioASG/multiglob that referenced this issue Mar 5, 2014
@AluisioASG AluisioASG Allow negated pattern at beginning of pattern list
Work around isaacs/node-glob#62 by matching the whole tree first.
ca5ccd3
@sheerun
sheerun commented Apr 12, 2014

So what about it?

@isaacs
Owner
isaacs commented Apr 12, 2014

Patch welcome.

@bcardarella

@isaacs I believe I have found the cause of the issue but need guidance on how best to fix.

https://github.com/isaacs/minimatch/blob/master/minimatch.js#L237-L242
In minimatch: for any pattern that does not begin with a "!" the for loop is never run so the pattern is never marked as "negated". During the parse function the pattern is iterated over for any !s and will stringSub ^ to negate that pattern: https://github.com/isaacs/minimatch/blob/master/minimatch.js#L553

parse never uses the negate flag to flip the pattern. This is why ./(!whatever) works but !(whatever) doesn't.

There could be two solutions:

  1. The easiest is to just remove parseNegate all together. This might be ideal as the parse function seems to be better about where to negate. the makeRe function doesn't seem to be used, but it does respect the negate flag: https://github.com/isaacs/minimatch/blob/master/minimatch.js#L806. So makeRe could be removed

  2. Refactor parse to use makeRe and respect the negate flag.

Thoughts on how to approach? I can work on the fix either way.

@bcardarella

@isaacs I'm going to try to implement options 2 as the README seems to imply that makeRe is supposed to be used anyway.

@bcardarella

Although now I'm confused. Upon further inspection this commit: isaacs/minimatch@b458f5d is from 2 and half years ago is when makeRe was removed from use in favor of parse. The commit message only mentions a "rewrite". Not really sure which is the best path to take. It seems that parse and makeRe are duplicating effort

@bcardarella bcardarella added a commit to bcardarella/node-glob that referenced this issue Jul 5, 2014
@bcardarella bcardarella Fix negation bug
This addresses the bug where starting a pattern with ! was not
respecting negation.

Fixes #62
Fixes #105
f70b83f
@isaacs isaacs closed this in #108 Jul 11, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.