Skip to content

Commit

Permalink
add windowsPathsNoEscape option
Browse files Browse the repository at this point in the history
Fix: #468
  • Loading branch information
isaacs committed May 12, 2022
1 parent d13cb06 commit 4251e42
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 2 deletions.
23 changes: 21 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -210,10 +210,24 @@ parallel glob operations will be sped up by sharing information about
the filesystem.

* `cwd` The current working directory in which to search. Defaults
to `process.cwd()`.
to `process.cwd()`. This option is always coerced to use
forward-slashes as a path separator, because it is not tested
as a glob pattern, so there is no need to escape anything.
* `root` The place where patterns starting with `/` will be mounted
onto. Defaults to `path.resolve(options.cwd, "/")` (`/` on Unix
systems, and `C:\` or some such on Windows.)
systems, and `C:\` or some such on Windows.) This option is
always coerced to use forward-slashes as a path separator,
because it is not tested as a glob pattern, so there is no need
to escape anything.
* `windowsPathsNoEscape` Use `\\` as a path separator _only_, and
_never_ as an escape character. If set, all `\\` characters
are replaced with `/` in the pattern. Note that this makes it
**impossible** to match against paths containing literal glob
pattern characters, but allows matching with patterns constructed
using `path.join()` and `path.resolve()` on Windows platforms,
mimicking the (buggy!) behavior of Glob v7 and before on
Windows. Please use with caution, and be mindful of [the caveat
below about Windows paths](#windows).
* `dot` Include `.dot` files in normal matches and `globstar` matches.
Note that an explicit dot in a portion of the pattern will always
match dot files.
Expand Down Expand Up @@ -332,6 +346,11 @@ Results from absolute patterns such as `/foo/*` are mounted onto the
root setting using `path.join`. On windows, this will by default result
in `/foo/*` matching `C:\foo\bar.txt`.

To automatically coerce all `\` characters to `/` in pattern
strings, **thus making it impossible to escape literal glob
characters**, you may set the `windowsPathsNoEscape` option to
`true`.

## Race Conditions

Glob searching, by its very nature, is susceptible to race conditions,
Expand Down
4 changes: 4 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 8.1

- Add `windowsPathsNoEscape` option

## 8.0

- Only support node v12 and higher
Expand Down
5 changes: 5 additions & 0 deletions common.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ function setopts (self, pattern, options) {
pattern = "**/" + pattern
}

self.windowsPathsNoEscape = !!options.windowsPathsNoEscape
if (self.windowsPathsNoEscape) {
pattern = pattern.replace(/\\/g, '/')
}

self.silent = !!options.silent
self.pattern = pattern
self.strict = options.strict !== false
Expand Down
34 changes: 34 additions & 0 deletions test/windows-paths-no-escape.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
const t = require('tap')
const g = require('../')

const platforms = ['win32', 'posix']
const originalPlatform = Object.getOwnPropertyDescriptor(process, 'platform')
for (const p of platforms) {
t.test(p, t => {
Object.defineProperty(process, 'platform', {
value: p,
enumerable: true,
configurable: true,
writable: true,
})
t.equal(process.platform, p, 'gut check: actually set platform')
const pattern = '/a/b/c/x\\[a-b\\]y\\*'
const def = new g.Glob(pattern, { noprocess: true })
const winpath = new g.Glob(pattern, {
windowsPathsNoEscape: true,
noprocess: true,
})
const nowinpath = new g.Glob(pattern, {
windowsPathsNoEscape: false,
noprocess: true,
})
t.strictSame([def.pattern, winpath.pattern, nowinpath.pattern], [
'/a/b/c/x\\[a-b\\]y\\*',
'/a/b/c/x/[a-b/]y/*',
'/a/b/c/x\\[a-b\\]y\\*',
])
t.end()
})
}

Object.defineProperty(process, 'platform', originalPlatform)

0 comments on commit 4251e42

Please sign in to comment.