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

Patterns with escaped characters are incorrectly handled on Windows #212

Closed
yortus opened this issue Jul 27, 2015 · 3 comments
Closed

Patterns with escaped characters are incorrectly handled on Windows #212

yortus opened this issue Jul 27, 2015 · 3 comments

Comments

@yortus
Copy link

yortus commented Jul 27, 2015

Problem

The node-glob README states (under the Windows heading):

Back-slashes will always be interpreted as escape characters, not path separators

However minimatchs implementation includes the following:

// windows support: need to use /, not \
  if (path.sep !== '/') {
    pattern = pattern.split(path.sep).join('/')
  }

On Windows (where path.sep is \), this code means all escape characters are treated as path separators. Therefore since minimatch does not treat \ as an escape character on Windows, then neither does node-glob, despite what its REAMDE says.

The impact is that on Windows platforms, escape characters in glob patterns will not work as intended.

This is an issue for cross-platform projects using this node-glob for globbing support.

Example

Suppose I want to match javascript files whose names start with [ and end with ].

A glob pattern for this, which requires escape characters, might be as follows:
\[*\].js

On Linux, minimatch converts the above pattern into the following Regex:
/^(?:(?=.)\[[^/]*?\]\.js)$/

On Windows, the regex created by minimatch is:
/^(?:\/(?=.)\[(?!\.)(?=.)[^/]*?\/\]\.js)$/

This latter regex does not match the specified glob pattern.

Solutions?

Arguably this is a minimatch issue, but minimatch's README makes no particular claims about Windows support or handling, whereas node-glob does make the above claim.

Possible solution in node-glob (leaving minimatch unaffected):

  • amend the node-glob README to state that escaped characters are not supported on Windows.

Possible solution in minimatch (leaving node-glob unaffected):

  • amend the minimatch README to require the same thing node-glob requires on Windows (ie: 'You must use forward-slashes only in glob expressions. Back-slashes will always be interpreted as escape characters, not path separators.')
  • remove the minimatchcode (snippet shown above) that turns \s into / on Windows.
  • --or-- add a minimatch option to explicitly specify the character to treat as the path separator.
@yortus
Copy link
Author

yortus commented Jul 27, 2015

Related: isaacs/minimatch#64

@erikkemperman
Copy link
Contributor

@isaacs This inconsistency between node-glob and minimatch on Windows is quite annoying... Escaping seems impossible in minimatch currently.

People have submitted issues about it for some time, and there's a PR that seems eminently reasonable.

Although having said that, I personally would probably prefer a major bump in minimatch adopting the same attitude as node-glob (forward slashes as path separators, backslashes as escape char, on all platforms).

Anyway, this has been going on for a while now. I don't mean to sound impatient, but is there anything that can be done to move this forward? Or do you not agree this is a problem?

@isaacs
Copy link
Owner

isaacs commented Feb 28, 2023

This has been handled in both minimatch and glob now. Patterns must use / unless windowsPathsNoEscape is set, in which case \ is only a path separator and not an escape character.

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