Skip to content

Patterns with escaped characters are incorrectly handled on Windows #212

@yortus

Description

@yortus

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions