Giter Site home page Giter Site logo

node-glob's Introduction

Glob

Match files using the patterns the shell uses.

The most correct and second fastest glob implementation in JavaScript. (See Comparison to Other JavaScript Glob Implementations at the bottom of this readme.)

a fun cartoon logo made of glob characters

Usage

Install with npm

npm i glob

Note the npm package name is not node-glob that's a different thing that was abandoned years ago. Just glob.

// load using import
import { glob, globSync, globStream, globStreamSync, Glob } from 'glob'
// or using commonjs, that's fine, too
const {
  glob,
  globSync,
  globStream,
  globStreamSync,
  Glob,
} = require('glob')

// the main glob() and globSync() resolve/return array of filenames

// all js files, but don't look in node_modules
const jsfiles = await glob('**/*.js', { ignore: 'node_modules/**' })

// pass in a signal to cancel the glob walk
const stopAfter100ms = await glob('**/*.css', {
  signal: AbortSignal.timeout(100),
})

// multiple patterns supported as well
const images = await glob(['css/*.{png,jpeg}', 'public/*.{png,jpeg}'])

// but of course you can do that with the glob pattern also
// the sync function is the same, just returns a string[] instead
// of Promise<string[]>
const imagesAlt = globSync('{css,public}/*.{png,jpeg}')

// you can also stream them, this is a Minipass stream
const filesStream = globStream(['**/*.dat', 'logs/**/*.log'])

// construct a Glob object if you wanna do it that way, which
// allows for much faster walks if you have to look in the same
// folder multiple times.
const g = new Glob('**/foo', {})
// glob objects are async iterators, can also do globIterate() or
// g.iterate(), same deal
for await (const file of g) {
  console.log('found a foo file:', file)
}
// pass a glob as the glob options to reuse its settings and caches
const g2 = new Glob('**/bar', g)
// sync iteration works as well
for (const file of g2) {
  console.log('found a bar file:', file)
}

// you can also pass withFileTypes: true to get Path objects
// these are like a Dirent, but with some more added powers
// check out http://npm.im/path-scurry for more info on their API
const g3 = new Glob('**/baz/**', { withFileTypes: true })
g3.stream().on('data', path => {
  console.log(
    'got a path object',
    path.fullpath(),
    path.isDirectory(),
    path.readdirSync().map(e => e.name),
  )
})

// if you use stat:true and withFileTypes, you can sort results
// by things like modified time, filter by permission mode, etc.
// All Stats fields will be available in that case. Slightly
// slower, though.
// For example:
const results = await glob('**', { stat: true, withFileTypes: true })

const timeSortedFiles = results
  .sort((a, b) => a.mtimeMs - b.mtimeMs)
  .map(path => path.fullpath())

const groupReadableFiles = results
  .filter(path => path.mode & 0o040)
  .map(path => path.fullpath())

// custom ignores can be done like this, for example by saying
// you'll ignore all markdown files, and all folders named 'docs'
const customIgnoreResults = await glob('**', {
  ignore: {
    ignored: p => /\.md$/.test(p.name),
    childrenIgnored: p => p.isNamed('docs'),
  },
})

// another fun use case, only return files with the same name as
// their parent folder, plus either `.ts` or `.js`
const folderNamedModules = await glob('**/*.{ts,js}', {
  ignore: {
    ignored: p => {
      const pp = p.parent
      return !(p.isNamed(pp.name + '.ts') || p.isNamed(pp.name + '.js'))
    },
  },
})

// find all files edited in the last hour, to do this, we ignore
// all of them that are more than an hour old
const newFiles = await glob('**', {
  // need stat so we have mtime
  stat: true,
  // only want the files, not the dirs
  nodir: true,
  ignore: {
    ignored: p => {
      return new Date() - p.mtime > 60 * 60 * 1000
    },
    // could add similar childrenIgnored here as well, but
    // directory mtime is inconsistent across platforms, so
    // probably better not to, unless you know the system
    // tracks this reliably.
  },
})

Note Glob patterns should always use / as a path separator, even on Windows systems, as \ is used to escape glob characters. If you wish to use \ as a path separator instead of using it as an escape character on Windows platforms, you may set windowsPathsNoEscape:true in the options. In this mode, special glob characters cannot be escaped, making it impossible to match a literal * ? and so on in filenames.

Command Line Interface

$ glob -h

Usage:
  glob [options] [<pattern> [<pattern> ...]]

Expand the positional glob expression arguments into any matching file system
paths found.

  -c<command> --cmd=<command>
                         Run the command provided, passing the glob expression
                         matches as arguments.

  -A --all               By default, the glob cli command will not expand any
                         arguments that are an exact match to a file on disk.

                         This prevents double-expanding, in case the shell
                         expands an argument whose filename is a glob
                         expression.

                         For example, if 'app/*.ts' would match 'app/[id].ts',
                         then on Windows powershell or cmd.exe, 'glob app/*.ts'
                         will expand to 'app/[id].ts', as expected. However, in
                         posix shells such as bash or zsh, the shell will first
                         expand 'app/*.ts' to a list of filenames. Then glob
                         will look for a file matching 'app/[id].ts' (ie,
                         'app/i.ts' or 'app/d.ts'), which is unexpected.

                         Setting '--all' prevents this behavior, causing glob to
                         treat ALL patterns as glob expressions to be expanded,
                         even if they are an exact match to a file on disk.

                         When setting this option, be sure to enquote arguments
                         so that the shell will not expand them prior to passing
                         them to the glob command process.

  -a --absolute          Expand to absolute paths
  -d --dot-relative      Prepend './' on relative matches
  -m --mark              Append a / on any directories matched
  -x --posix             Always resolve to posix style paths, using '/' as the
                         directory separator, even on Windows. Drive letter
                         absolute matches on Windows will be expanded to their
                         full resolved UNC maths, eg instead of 'C:\foo\bar', it
                         will expand to '//?/C:/foo/bar'.

  -f --follow            Follow symlinked directories when expanding '**'
  -R --realpath          Call 'fs.realpath' on all of the results. In the case
                         of an entry that cannot be resolved, the entry is
                         omitted. This incurs a slight performance penalty, of
                         course, because of the added system calls.

  -s --stat              Call 'fs.lstat' on all entries, whether required or not
                         to determine if it's a valid match.

  -b --match-base        Perform a basename-only match if the pattern does not
                         contain any slash characters. That is, '*.js' would be
                         treated as equivalent to '**/*.js', matching js files
                         in all directories.

  --dot                  Allow patterns to match files/directories that start
                         with '.', even if the pattern does not start with '.'

  --nobrace              Do not expand {...} patterns
  --nocase               Perform a case-insensitive match. This defaults to
                         'true' on macOS and Windows platforms, and false on all
                         others.

                         Note: 'nocase' should only be explicitly set when it is
                         known that the filesystem's case sensitivity differs
                         from the platform default. If set 'true' on
                         case-insensitive file systems, then the walk may return
                         more or less results than expected.

  --nodir                Do not match directories, only files.

                         Note: to *only* match directories, append a '/' at the
                         end of the pattern.

  --noext                Do not expand extglob patterns, such as '+(a|b)'
  --noglobstar           Do not expand '**' against multiple path portions. Ie,
                         treat it as a normal '*' instead.

  --windows-path-no-escape
                         Use '\' as a path separator *only*, and *never* as an
                         escape character. If set, all '\' characters are
                         replaced with '/' in the pattern.

  -D<n> --max-depth=<n>  Maximum depth to traverse from the current working
                         directory

  -C<cwd> --cwd=<cwd>    Current working directory to execute/match in
  -r<root> --root=<root> A string path resolved against the 'cwd', which is used
                         as the starting point for absolute patterns that start
                         with '/' (but not drive letters or UNC paths on
                         Windows).

                         Note that this *doesn't* necessarily limit the walk to
                         the 'root' directory, and doesn't affect the cwd
                         starting point for non-absolute patterns. A pattern
                         containing '..' will still be able to traverse out of
                         the root directory, if it is not an actual root
                         directory on the filesystem, and any non-absolute
                         patterns will still be matched in the 'cwd'.

                         To start absolute and non-absolute patterns in the same
                         path, you can use '--root=' to set it to the empty
                         string. However, be aware that on Windows systems, a
                         pattern like 'x:/*' or '//host/share/*' will *always*
                         start in the 'x:/' or '//host/share/' directory,
                         regardless of the --root setting.

  --platform=<platform>  Defaults to the value of 'process.platform' if
                         available, or 'linux' if not. Setting --platform=win32
                         on non-Windows systems may cause strange behavior!

  -i<ignore> --ignore=<ignore>
                         Glob patterns to ignore Can be set multiple times
  -v --debug             Output a huge amount of noisy debug information about
                         patterns as they are parsed and used to match files.

  -h --help              Show this usage information

glob(pattern: string | string[], options?: GlobOptions) => Promise<string[] | Path[]>

Perform an asynchronous glob search for the pattern(s) specified. Returns Path objects if the withFileTypes option is set to true. See below for full options field desciptions.

globSync(pattern: string | string[], options?: GlobOptions) => string[] | Path[]

Synchronous form of glob().

Alias: glob.sync()

globIterate(pattern: string | string[], options?: GlobOptions) => AsyncGenerator<string>

Return an async iterator for walking glob pattern matches.

Alias: glob.iterate()

globIterateSync(pattern: string | string[], options?: GlobOptions) => Generator<string>

Return a sync iterator for walking glob pattern matches.

Alias: glob.iterate.sync(), glob.sync.iterate()

globStream(pattern: string | string[], options?: GlobOptions) => Minipass<string | Path>

Return a stream that emits all the strings or Path objects and then emits end when completed.

Alias: glob.stream()

globStreamSync(pattern: string | string[], options?: GlobOptions) => Minipass<string | Path>

Syncronous form of globStream(). Will read all the matches as fast as you consume them, even all in a single tick if you consume them immediately, but will still respond to backpressure if they're not consumed immediately.

Alias: glob.stream.sync(), glob.sync.stream()

hasMagic(pattern: string | string[], options?: GlobOptions) => boolean

Returns true if the provided pattern contains any "magic" glob characters, given the options provided.

Brace expansion is not considered "magic" unless the magicalBraces option is set, as brace expansion just turns one string into an array of strings. So a pattern like 'x{a,b}y' would return false, because 'xay' and 'xby' both do not contain any magic glob characters, and it's treated the same as if you had called it on ['xay', 'xby']. When magicalBraces:true is in the options, brace expansion is treated as a pattern having magic.

escape(pattern: string, options?: GlobOptions) => string

Escape all magic characters in a glob pattern, so that it will only ever match literal strings

If the windowsPathsNoEscape option is used, then characters are escaped by wrapping in [], because a magic character wrapped in a character class can only be satisfied by that exact character.

Slashes (and backslashes in windowsPathsNoEscape mode) cannot be escaped or unescaped.

unescape(pattern: string, options?: GlobOptions) => string

Un-escape a glob string that may contain some escaped characters.

If the windowsPathsNoEscape option is used, then square-brace escapes are removed, but not backslash escapes. For example, it will turn the string '[*]' into *, but it will not turn '\\*' into '*', because \ is a path separator in windowsPathsNoEscape mode.

When windowsPathsNoEscape is not set, then both brace escapes and backslash escapes are removed.

Slashes (and backslashes in windowsPathsNoEscape mode) cannot be escaped or unescaped.

Class Glob

An object that can perform glob pattern traversals.

const g = new Glob(pattern: string | string[], options: GlobOptions)

Options object is required.

See full options descriptions below.

Note that a previous Glob object can be passed as the GlobOptions to another Glob instantiation to re-use settings and caches with a new pattern.

Traversal functions can be called multiple times to run the walk again.

g.stream()

Stream results asynchronously,

g.streamSync()

Stream results synchronously.

g.iterate()

Default async iteration function. Returns an AsyncGenerator that iterates over the results.

g.iterateSync()

Default sync iteration function. Returns a Generator that iterates over the results.

g.walk()

Returns a Promise that resolves to the results array.

g.walkSync()

Returns a results array.

Properties

All options are stored as properties on the Glob object.

  • opts The options provided to the constructor.
  • patterns An array of parsed immutable Pattern objects.

Options

Exported as GlobOptions TypeScript interface. A GlobOptions object may be provided to any of the exported methods, and must be provided to the Glob constructor.

All options are optional, boolean, and false by default, unless otherwise noted.

All resolved options are added to the Glob object as properties.

If you are running many glob operations, you can pass a Glob object as the options argument to a subsequent operation to share the previously loaded cache.

  • cwd String path or file:// string or URL object. The current working directory in which to search. Defaults to process.cwd(). See also: "Windows, CWDs, Drive Letters, and UNC Paths", below.

    This option may be either a string path or a file:// URL object or string.

  • root A string path resolved against the cwd option, which is used as the starting point for absolute patterns that start with /, (but not drive letters or UNC paths on Windows).

    Note that this doesn't necessarily limit the walk to the root directory, and doesn't affect the cwd starting point for non-absolute patterns. A pattern containing .. will still be able to traverse out of the root directory, if it is not an actual root directory on the filesystem, and any non-absolute patterns will be matched in the cwd. For example, the pattern /../* with {root:'/some/path'} will return all files in /some, not all files in /some/path. The pattern * with {root:'/some/path'} will return all the entries in the cwd, not the entries in /some/path.

    To start absolute and non-absolute patterns in the same path, you can use {root:''}. However, be aware that on Windows systems, a pattern like x:/* or //host/share/* will always start in the x:/ or //host/share directory, regardless of the root setting.

  • 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. (For legacy reasons, this is also set if allowWindowsEscape is set to the exact value false.)

  • 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.

  • magicalBraces Treat brace expansion like {a,b} as a "magic" pattern. Has no effect if {@link nobrace} is set.

    Only has effect on the {@link hasMagic} function, no effect on glob pattern matching itself.

  • dotRelative Prepend all relative path strings with ./ (or .\ on Windows).

    Without this option, returned relative paths are "bare", so instead of returning './foo/bar', they are returned as 'foo/bar'.

    Relative patterns starting with '../' are not prepended with ./, even if this option is set.

  • mark Add a / character to directory matches. Note that this requires additional stat calls.

  • nobrace Do not expand {a,b} and {1..3} brace sets.

  • noglobstar Do not match ** against multiple filenames. (Ie, treat it as a normal * instead.)

  • noext Do not match "extglob" patterns such as +(a|b).

  • nocase Perform a case-insensitive match. This defaults to true on macOS and Windows systems, and false on all others.

    Note nocase should only be explicitly set when it is known that the filesystem's case sensitivity differs from the platform default. If set true on case-sensitive file systems, or false on case-insensitive file systems, then the walk may return more or less results than expected.

  • maxDepth Specify a number to limit the depth of the directory traversal to this many levels below the cwd.

  • matchBase Perform a basename-only match if the pattern does not contain any slash characters. That is, *.js would be treated as equivalent to **/*.js, matching all js files in all directories.

  • nodir Do not match directories, only files. (Note: to match only directories, put a / at the end of the pattern.)

    Note: when follow and nodir are both set, then symbolic links to directories are also omitted.

  • stat Call lstat() on all entries, whether required or not to determine whether it's a valid match. When used with withFileTypes, this means that matches will include data such as modified time, permissions, and so on. Note that this will incur a performance cost due to the added system calls.

  • ignore string or string[], or an object with ignore and ignoreChildren methods.

    If a string or string[] is provided, then this is treated as a glob pattern or array of glob patterns to exclude from matches. To ignore all children within a directory, as well as the entry itself, append '/**' to the ignore pattern.

    Note ignore patterns are always in dot:true mode, regardless of any other settings.

    If an object is provided that has ignored(path) and/or childrenIgnored(path) methods, then these methods will be called to determine whether any Path is a match or if its children should be traversed, respectively.

  • follow Follow symlinked directories when expanding ** patterns. This can result in a lot of duplicate references in the presence of cyclic links, and make performance quite bad.

    By default, a ** in a pattern will follow 1 symbolic link if it is not the first item in the pattern, or none if it is the first item in the pattern, following the same behavior as Bash.

    Note: when follow and nodir are both set, then symbolic links to directories are also omitted.

  • realpath Set to true to call fs.realpath on all of the results. In the case of an entry that cannot be resolved, the entry is omitted. This incurs a slight performance penalty, of course, because of the added system calls.

  • absolute Set to true to always receive absolute paths for matched files. Set to false to always receive relative paths for matched files.

    By default, when this option is not set, absolute paths are returned for patterns that are absolute, and otherwise paths are returned that are relative to the cwd setting.

    This does not make an extra system call to get the realpath, it only does string path resolution.

    absolute may not be used along with withFileTypes.

  • posix Set to true to use / as the path separator in returned results. On posix systems, this has no effect. On Windows systems, this will return / delimited path results, and absolute paths will be returned in their full resolved UNC path form, eg insted of 'C:\\foo\\bar', it will return //?/C:/foo/bar.

  • platform Defaults to value of process.platform if available, or 'linux' if not. Setting platform:'win32' on non-Windows systems may cause strange behavior.

  • withFileTypes Return PathScurry Path objects instead of strings. These are similar to a NodeJS Dirent object, but with additional methods and properties.

    withFileTypes may not be used along with absolute.

  • signal An AbortSignal which will cancel the Glob walk when triggered.

  • fs An override object to pass in custom filesystem methods. See PathScurry docs for what can be overridden.

  • scurry A PathScurry object used to traverse the file system. If the nocase option is set explicitly, then any provided scurry object must match this setting.

  • includeChildMatches boolean, default true. Do not match any children of any matches. For example, the pattern **\/foo would match a/foo, but not a/foo/b/foo in this mode.

    This is especially useful for cases like "find all node_modules folders, but not the ones in node_modules".

    In order to support this, the Ignore implementation must support an add(pattern: string) method. If using the default Ignore class, then this is fine, but if this is set to false, and a custom Ignore is provided that does not have an add() method, then it will throw an error.

    Caveat It only ignores matches that would be a descendant of a previous match, and only if that descendant is matched after the ancestor is encountered. Since the file system walk happens in indeterminate order, it's possible that a match will already be added before its ancestor, if multiple or braced patterns are used.

    For example:

    const results = await glob(
      [
        // likely to match first, since it's just a stat
        'a/b/c/d/e/f',
    
        // this pattern is more complicated! It must to various readdir()
        // calls and test the results against a regular expression, and that
        // is certainly going to take a little bit longer.
        //
        // So, later on, it encounters a match at 'a/b/c/d/e', but it's too
        // late to ignore a/b/c/d/e/f, because it's already been emitted.
        'a/[bdf]/?/[a-z]/*',
      ],
      { includeChildMatches: false },
    )

    It's best to only set this to false if you can be reasonably sure that no components of the pattern will potentially match one another's file system descendants, or if the occasional included child entry will not cause problems.

Glob Primer

Much more information about glob pattern expansion can be found by running man bash and searching for Pattern Matching.

"Globs" are the patterns you type when you do stuff like ls *.js on the command line, or put build/* in a .gitignore file.

Before parsing the path part patterns, braced sections are expanded into a set. Braced sections start with { and end with }, with 2 or more comma-delimited sections within. Braced sections may contain slash characters, so a{/b/c,bcd} would expand into a/b/c and abcd.

The following characters have special magic meaning when used in a path portion. With the exception of **, none of these match path separators (ie, / on all platforms, and \ on Windows).

  • * Matches 0 or more characters in a single path portion. When alone in a path portion, it must match at least 1 character. If dot:true is not specified, then * will not match against a . character at the start of a path portion.
  • ? Matches 1 character. If dot:true is not specified, then ? will not match against a . character at the start of a path portion.
  • [...] Matches a range of characters, similar to a RegExp range. If the first character of the range is ! or ^ then it matches any character not in the range. If the first character is ], then it will be considered the same as \], rather than the end of the character class.
  • !(pattern|pattern|pattern) Matches anything that does not match any of the patterns provided. May not contain / characters. Similar to *, if alone in a path portion, then the path portion must have at least one character.
  • ?(pattern|pattern|pattern) Matches zero or one occurrence of the patterns provided. May not contain / characters.
  • +(pattern|pattern|pattern) Matches one or more occurrences of the patterns provided. May not contain / characters.
  • *(a|b|c) Matches zero or more occurrences of the patterns provided. May not contain / characters.
  • @(pattern|pat*|pat?erN) Matches exactly one of the patterns provided. May not contain / characters.
  • ** If a "globstar" is alone in a path portion, then it matches zero or more directories and subdirectories searching for matches. It does not crawl symlinked directories, unless {follow:true} is passed in the options object. A pattern like a/b/** will only match a/b if it is a directory. Follows 1 symbolic link if not the first item in the pattern, or 0 if it is the first item, unless follow:true is set, in which case it follows all symbolic links.

[:class:] patterns are supported by this implementation, but [=c=] and [.symbol.] style class patterns are not.

Dots

If a file or directory path portion has a . as the first character, then it will not match any glob pattern unless that pattern's corresponding path part also has a . as its first character.

For example, the pattern a/.*/c would match the file at a/.b/c. However the pattern a/*/c would not, because * does not start with a dot character.

You can make glob treat dots as normal characters by setting dot:true in the options.

Basename Matching

If you set matchBase:true in the options, and the pattern has no slashes in it, then it will seek for any file anywhere in the tree with a matching basename. For example, *.js would match test/simple/basic.js.

Empty Sets

If no matching files are found, then an empty array is returned. This differs from the shell, where the pattern itself is returned. For example:

$ echo a*s*d*f
a*s*d*f

Comparisons to other fnmatch/glob implementations

While strict compliance with the existing standards is a worthwhile goal, some discrepancies exist between node-glob and other implementations, and are intentional.

The double-star character ** is supported by default, unless the noglobstar flag is set. This is supported in the manner of bsdglob and bash 5, where ** only has special significance if it is the only thing in a path part. That is, a/**/b will match a/x/y/b, but a/**b will not.

Note that symlinked directories are not traversed as part of a **, though their contents may match against subsequent portions of the pattern. This prevents infinite loops and duplicates and the like. You can force glob to traverse symlinks with ** by setting {follow:true} in the options.

There is no equivalent of the nonull option. A pattern that does not find any matches simply resolves to nothing. (An empty array, immediately ended stream, etc.)

If brace expansion is not disabled, then it is performed before any other interpretation of the glob pattern. Thus, a pattern like +(a|{b),c)}, which would not be valid in bash or zsh, is expanded first into the set of +(a|b) and +(a|c), and those patterns are checked for validity. Since those two are valid, matching proceeds.

The character class patterns [:class:] (posix standard named classes) style class patterns are supported and unicode-aware, but [=c=] (locale-specific character collation weight), and [.symbol.] (collating symbol), are not.

Repeated Slashes

Unlike Bash and zsh, repeated / are always coalesced into a single path separator.

Comments and Negation

Previously, this module let you mark a pattern as a "comment" if it started with a # character, or a "negated" pattern if it started with a ! character.

These options were deprecated in version 5, and removed in version 6.

To specify things that should not match, use the ignore option.

Windows

Please only use forward-slashes in glob expressions.

Though windows uses either / or \ as its path separator, only / characters are used by this glob implementation. You must use forward-slashes only in glob expressions. Back-slashes will always be interpreted as escape characters, not path separators.

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.

Windows, CWDs, Drive Letters, and UNC Paths

On posix systems, when a pattern starts with /, any cwd option is ignored, and the traversal starts at /, plus any non-magic path portions specified in the pattern.

On Windows systems, the behavior is similar, but the concept of an "absolute path" is somewhat more involved.

UNC Paths

A UNC path may be used as the start of a pattern on Windows platforms. For example, a pattern like: //?/x:/* will return all file entries in the root of the x: drive. A pattern like //ComputerName/Share/* will return all files in the associated share.

UNC path roots are always compared case insensitively.

Drive Letters

A pattern starting with a drive letter, like c:/*, will search in that drive, regardless of any cwd option provided.

If the pattern starts with /, and is not a UNC path, and there is an explicit cwd option set with a drive letter, then the drive letter in the cwd is used as the root of the directory traversal.

For example, glob('/tmp', { cwd: 'c:/any/thing' }) will return ['c:/tmp'] as the result.

If an explicit cwd option is not provided, and the pattern starts with /, then the traversal will run on the root of the drive provided as the cwd option. (That is, it is the result of path.resolve('/').)

Race Conditions

Glob searching, by its very nature, is susceptible to race conditions, since it relies on directory walking.

As a result, it is possible that a file that exists when glob looks for it may have been deleted or modified by the time it returns the result.

By design, this implementation caches all readdir calls that it makes, in order to cut down on system overhead. However, this also makes it even more susceptible to races, especially if the cache object is reused between glob calls.

Users are thus advised not to use a glob result as a guarantee of filesystem state in the face of rapid changes. For the vast majority of operations, this is never a problem.

See Also:

Glob Logo

Glob's logo was created by Tanya Brassie. Logo files can be found here.

The logo is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.

Contributing

Any change to behavior (including bugfixes) must come with a test.

Patches that fail tests or reduce performance will be rejected.

# to run tests
npm test

# to re-generate test fixtures
npm run test-regen

# run the benchmarks
npm run bench

# to profile javascript
npm run prof

Comparison to Other JavaScript Glob Implementations

tl;dr

  • If you want glob matching that is as faithful as possible to Bash pattern expansion semantics, and as fast as possible within that constraint, use this module.
  • If you are reasonably sure that the patterns you will encounter are relatively simple, and want the absolutely fastest glob matcher out there, use fast-glob.
  • If you are reasonably sure that the patterns you will encounter are relatively simple, and want the convenience of automatically respecting .gitignore files, use globby.

There are some other glob matcher libraries on npm, but these three are (in my opinion, as of 2023) the best.


full explanation

Every library reflects a set of opinions and priorities in the trade-offs it makes. Other than this library, I can personally recommend both globby and fast-glob, though they differ in their benefits and drawbacks.

Both have very nice APIs and are reasonably fast.

fast-glob is, as far as I am aware, the fastest glob implementation in JavaScript today. However, there are many cases where the choices that fast-glob makes in pursuit of speed mean that its results differ from the results returned by Bash and other sh-like shells, which may be surprising.

In my testing, fast-glob is around 10-20% faster than this module when walking over 200k files nested 4 directories deep1. However, there are some inconsistencies with Bash matching behavior that this module does not suffer from:

  • ** only matches files, not directories
  • .. path portions are not handled unless they appear at the start of the pattern
  • ./!(<pattern>) will not match any files that start with <pattern>, even if they do not match <pattern>. For example, !(9).txt will not match 9999.txt.
  • Some brace patterns in the middle of a pattern will result in failing to find certain matches.
  • Extglob patterns are allowed to contain / characters.

Globby exhibits all of the same pattern semantics as fast-glob, (as it is a wrapper around fast-glob) and is slightly slower than node-glob (by about 10-20% in the benchmark test set, or in other words, anywhere from 20-50% slower than fast-glob). However, it adds some API conveniences that may be worth the costs.

  • Support for .gitignore and other ignore files.
  • Support for negated globs (ie, patterns starting with ! rather than using a separate ignore option).

The priority of this module is "correctness" in the sense of performing a glob pattern expansion as faithfully as possible to the behavior of Bash and other sh-like shells, with as much speed as possible.

Note that prior versions of node-glob are not on this list. Former versions of this module are far too slow for any cases where performance matters at all, and were designed with APIs that are extremely dated by current JavaScript standards.


[1]: In the cases where this module returns results and fast-glob doesn't, it's even faster, of course.

lumpy space princess saying 'oh my GLOB'

Benchmark Results

First number is time, smaller is better.

Second number is the count of results returned.

--- pattern: '**' ---
~~ sync ~~
node fast-glob sync             0m0.598s  200364
node globby sync                0m0.765s  200364
node current globSync mjs       0m0.683s  222656
node current glob syncStream    0m0.649s  222656
~~ async ~~
node fast-glob async            0m0.350s  200364
node globby async               0m0.509s  200364
node current glob async mjs     0m0.463s  222656
node current glob stream        0m0.411s  222656

--- pattern: '**/..' ---
~~ sync ~~
node fast-glob sync             0m0.486s  0
node globby sync                0m0.769s  200364
node current globSync mjs       0m0.564s  2242
node current glob syncStream    0m0.583s  2242
~~ async ~~
node fast-glob async            0m0.283s  0
node globby async               0m0.512s  200364
node current glob async mjs     0m0.299s  2242
node current glob stream        0m0.312s  2242

--- pattern: './**/0/**/0/**/0/**/0/**/*.txt' ---
~~ sync ~~
node fast-glob sync             0m0.490s  10
node globby sync                0m0.517s  10
node current globSync mjs       0m0.540s  10
node current glob syncStream    0m0.550s  10
~~ async ~~
node fast-glob async            0m0.290s  10
node globby async               0m0.296s  10
node current glob async mjs     0m0.278s  10
node current glob stream        0m0.302s  10

--- pattern: './**/[01]/**/[12]/**/[23]/**/[45]/**/*.txt' ---
~~ sync ~~
node fast-glob sync             0m0.500s  160
node globby sync                0m0.528s  160
node current globSync mjs       0m0.556s  160
node current glob syncStream    0m0.573s  160
~~ async ~~
node fast-glob async            0m0.283s  160
node globby async               0m0.301s  160
node current glob async mjs     0m0.306s  160
node current glob stream        0m0.322s  160

--- pattern: './**/0/**/0/**/*.txt' ---
~~ sync ~~
node fast-glob sync             0m0.502s  5230
node globby sync                0m0.527s  5230
node current globSync mjs       0m0.544s  5230
node current glob syncStream    0m0.557s  5230
~~ async ~~
node fast-glob async            0m0.285s  5230
node globby async               0m0.305s  5230
node current glob async mjs     0m0.304s  5230
node current glob stream        0m0.310s  5230

--- pattern: '**/*.txt' ---
~~ sync ~~
node fast-glob sync             0m0.580s  200023
node globby sync                0m0.771s  200023
node current globSync mjs       0m0.685s  200023
node current glob syncStream    0m0.649s  200023
~~ async ~~
node fast-glob async            0m0.349s  200023
node globby async               0m0.509s  200023
node current glob async mjs     0m0.427s  200023
node current glob stream        0m0.388s  200023

--- pattern: '{**/*.txt,**/?/**/*.txt,**/?/**/?/**/*.txt,**/?/**/?/**/?/**/*.txt,**/?/**/?/**/?/**/?/**/*.txt}' ---
~~ sync ~~
node fast-glob sync             0m0.589s  200023
node globby sync                0m0.771s  200023
node current globSync mjs       0m0.716s  200023
node current glob syncStream    0m0.684s  200023
~~ async ~~
node fast-glob async            0m0.351s  200023
node globby async               0m0.518s  200023
node current glob async mjs     0m0.462s  200023
node current glob stream        0m0.468s  200023

--- pattern: '**/5555/0000/*.txt' ---
~~ sync ~~
node fast-glob sync             0m0.496s  1000
node globby sync                0m0.519s  1000
node current globSync mjs       0m0.539s  1000
node current glob syncStream    0m0.567s  1000
~~ async ~~
node fast-glob async            0m0.285s  1000
node globby async               0m0.299s  1000
node current glob async mjs     0m0.305s  1000
node current glob stream        0m0.301s  1000

--- pattern: './**/0/**/../[01]/**/0/../**/0/*.txt' ---
~~ sync ~~
node fast-glob sync             0m0.484s  0
node globby sync                0m0.507s  0
node current globSync mjs       0m0.577s  4880
node current glob syncStream    0m0.586s  4880
~~ async ~~
node fast-glob async            0m0.280s  0
node globby async               0m0.298s  0
node current glob async mjs     0m0.327s  4880
node current glob stream        0m0.324s  4880

--- pattern: '**/????/????/????/????/*.txt' ---
~~ sync ~~
node fast-glob sync             0m0.547s  100000
node globby sync                0m0.673s  100000
node current globSync mjs       0m0.626s  100000
node current glob syncStream    0m0.618s  100000
~~ async ~~
node fast-glob async            0m0.315s  100000
node globby async               0m0.414s  100000
node current glob async mjs     0m0.366s  100000
node current glob stream        0m0.345s  100000

--- pattern: './{**/?{/**/?{/**/?{/**/?,,,,},,,,},,,,},,,}/**/*.txt' ---
~~ sync ~~
node fast-glob sync             0m0.588s  100000
node globby sync                0m0.670s  100000
node current globSync mjs       0m0.717s  200023
node current glob syncStream    0m0.687s  200023
~~ async ~~
node fast-glob async            0m0.343s  100000
node globby async               0m0.418s  100000
node current glob async mjs     0m0.519s  200023
node current glob stream        0m0.451s  200023

--- pattern: '**/!(0|9).txt' ---
~~ sync ~~
node fast-glob sync             0m0.573s  160023
node globby sync                0m0.731s  160023
node current globSync mjs       0m0.680s  180023
node current glob syncStream    0m0.659s  180023
~~ async ~~
node fast-glob async            0m0.345s  160023
node globby async               0m0.476s  160023
node current glob async mjs     0m0.427s  180023
node current glob stream        0m0.388s  180023

--- pattern: './{*/**/../{*/**/../{*/**/../{*/**/../{*/**,,,,},,,,},,,,},,,,},,,,}/*.txt' ---
~~ sync ~~
node fast-glob sync             0m0.483s  0
node globby sync                0m0.512s  0
node current globSync mjs       0m0.811s  200023
node current glob syncStream    0m0.773s  200023
~~ async ~~
node fast-glob async            0m0.280s  0
node globby async               0m0.299s  0
node current glob async mjs     0m0.617s  200023
node current glob stream        0m0.568s  200023

--- pattern: './*/**/../*/**/../*/**/../*/**/../*/**/../*/**/../*/**/../*/**/*.txt' ---
~~ sync ~~
node fast-glob sync             0m0.485s  0
node globby sync                0m0.507s  0
node current globSync mjs       0m0.759s  200023
node current glob syncStream    0m0.740s  200023
~~ async ~~
node fast-glob async            0m0.281s  0
node globby async               0m0.297s  0
node current glob async mjs     0m0.544s  200023
node current glob stream        0m0.464s  200023

--- pattern: './*/**/../*/**/../*/**/../*/**/../*/**/*.txt' ---
~~ sync ~~
node fast-glob sync             0m0.486s  0
node globby sync                0m0.513s  0
node current globSync mjs       0m0.734s  200023
node current glob syncStream    0m0.696s  200023
~~ async ~~
node fast-glob async            0m0.286s  0
node globby async               0m0.296s  0
node current glob async mjs     0m0.506s  200023
node current glob stream        0m0.483s  200023

--- pattern: './0/**/../1/**/../2/**/../3/**/../4/**/../5/**/../6/**/../7/**/*.txt' ---
~~ sync ~~
node fast-glob sync             0m0.060s  0
node globby sync                0m0.074s  0
node current globSync mjs       0m0.067s  0
node current glob syncStream    0m0.066s  0
~~ async ~~
node fast-glob async            0m0.060s  0
node globby async               0m0.075s  0
node current glob async mjs     0m0.066s  0
node current glob stream        0m0.067s  0

--- pattern: './**/?/**/?/**/?/**/?/**/*.txt' ---
~~ sync ~~
node fast-glob sync             0m0.568s  100000
node globby sync                0m0.651s  100000
node current globSync mjs       0m0.619s  100000
node current glob syncStream    0m0.617s  100000
~~ async ~~
node fast-glob async            0m0.332s  100000
node globby async               0m0.409s  100000
node current glob async mjs     0m0.372s  100000
node current glob stream        0m0.351s  100000

--- pattern: '**/*/**/*/**/*/**/*/**' ---
~~ sync ~~
node fast-glob sync             0m0.603s  200113
node globby sync                0m0.798s  200113
node current globSync mjs       0m0.730s  222137
node current glob syncStream    0m0.693s  222137
~~ async ~~
node fast-glob async            0m0.356s  200113
node globby async               0m0.525s  200113
node current glob async mjs     0m0.508s  222137
node current glob stream        0m0.455s  222137

--- pattern: './**/*/**/*/**/*/**/*/**/*.txt' ---
~~ sync ~~
node fast-glob sync             0m0.622s  200000
node globby sync                0m0.792s  200000
node current globSync mjs       0m0.722s  200000
node current glob syncStream    0m0.695s  200000
~~ async ~~
node fast-glob async            0m0.369s  200000
node globby async               0m0.527s  200000
node current glob async mjs     0m0.502s  200000
node current glob stream        0m0.481s  200000

--- pattern: '**/*.txt' ---
~~ sync ~~
node fast-glob sync             0m0.588s  200023
node globby sync                0m0.771s  200023
node current globSync mjs       0m0.684s  200023
node current glob syncStream    0m0.658s  200023
~~ async ~~
node fast-glob async            0m0.352s  200023
node globby async               0m0.516s  200023
node current glob async mjs     0m0.432s  200023
node current glob stream        0m0.384s  200023

--- pattern: './**/**/**/**/**/**/**/**/*.txt' ---
~~ sync ~~
node fast-glob sync             0m0.589s  200023
node globby sync                0m0.766s  200023
node current globSync mjs       0m0.682s  200023
node current glob syncStream    0m0.652s  200023
~~ async ~~
node fast-glob async            0m0.352s  200023
node globby async               0m0.523s  200023
node current glob async mjs     0m0.436s  200023
node current glob stream        0m0.380s  200023

--- pattern: '**/*/*.txt' ---
~~ sync ~~
node fast-glob sync             0m0.592s  200023
node globby sync                0m0.776s  200023
node current globSync mjs       0m0.691s  200023
node current glob syncStream    0m0.659s  200023
~~ async ~~
node fast-glob async            0m0.357s  200023
node globby async               0m0.513s  200023
node current glob async mjs     0m0.471s  200023
node current glob stream        0m0.424s  200023

--- pattern: '**/*/**/*.txt' ---
~~ sync ~~
node fast-glob sync             0m0.585s  200023
node globby sync                0m0.766s  200023
node current globSync mjs       0m0.694s  200023
node current glob syncStream    0m0.664s  200023
~~ async ~~
node fast-glob async            0m0.350s  200023
node globby async               0m0.514s  200023
node current glob async mjs     0m0.472s  200023
node current glob stream        0m0.424s  200023

--- pattern: '**/[0-9]/**/*.txt' ---
~~ sync ~~
node fast-glob sync             0m0.544s  100000
node globby sync                0m0.636s  100000
node current globSync mjs       0m0.626s  100000
node current glob syncStream    0m0.621s  100000
~~ async ~~
node fast-glob async            0m0.322s  100000
node globby async               0m0.404s  100000
node current glob async mjs     0m0.360s  100000
node current glob stream        0m0.352s  100000

node-glob's People

Contributors

amelon-bflo avatar basarat avatar bcardarella avatar benhutchins avatar bkjohnson avatar boneskull avatar colinrotherham avatar denis-sokolov avatar edi avatar erikkemperman avatar falkendk avatar gyandeeps avatar isaacs avatar jakeboone02 avatar jakobmattsson avatar jimt avatar jldec avatar jsha avatar kieran-ryan avatar kimjoar avatar ljharb avatar marclipovsky avatar neverendingqs avatar nschonni avatar papandreou avatar phated avatar schnittstabil avatar sindresorhus avatar tanyabrassie avatar wdavidw avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

node-glob's Issues

support extended globs

As far as I can tell "extended globs" have been implemented independently in Ruby's dir.c, bash 4.01's lib/glob.c, in zsh, and somewhere in the Perl ecosystem.

GNU Bash's glob is GPL, of course, which is unfortunate. Ruby's is GPL or some custom stuff, probably workable. Bash's and zsh's are the best in terms of spec compliance and general utility, but both are less than completely independent from their respective programs. Ruby's seems pretty complete as well, but I'm not sure how great a fit it is for node's use.

Another option is to take a look at the posix spec and implement it that way. But that sucks.

Issues installing on OSX Lion

Sorry to report that the installation is failing under OSX Lion, with node 0.5.7:

$ npm install glob

[email protected] preinstall /Users/danski/Sites/spah/node_modules/glob
node-waf clean || true; node-waf configure build

Nothing to clean (project not configured)
Checking for program g++ or c++ : /usr/bin/g++
Checking for program cpp : /usr/bin/cpp
Checking for program ar : /usr/bin/ar
Checking for program ranlib : /usr/bin/ranlib
Checking for g++ : ok
Checking for node path : not found
Checking for node prefix : ok /usr/local
'configure' finished successfully (0.149s)
Waf: Entering directory /Users/danski/Sites/spah/node_modules/glob/build' [1/8] cxx: deps/glob/glob.c -> build/Release/deps/glob/glob_1.o [2/8] cxx: deps/fnmatch/fnmatch.c -> build/Release/deps/fnmatch/fnmatch_2.o [3/8] cxx: src/glob.cc -> build/Release/src/glob_3.o [4/8] cxx: deps/glob/glob.c -> build/Release/deps/glob/glob_4.o [5/8] cxx: deps/fnmatch/fnmatch.c -> build/Release/deps/fnmatch/fnmatch_5.o [6/8] cxx: src/glob.cc -> build/Release/src/glob_6.o ../src/glob.cc: In function ‘int EIO_Glob(eio_req*)’: ../src/glob.cc:87: warning: format ‘%i’ expects type ‘int’, but argument 3 has type > ‘const char*’ ../src/glob.cc:87: warning: format ‘%i’ expects type ‘int’, but argument 3 has type > ‘const char*’ ../src/glob.cc: In function ‘int EIO_GlobAfter(eio_req*)’: ../src/glob.cc:101: warning: format ‘%i’ expects type ‘int’, but argument 3 has type > ‘const char*’ ../src/glob.cc:101: warning: format ‘%i’ expects type ‘int’, but argument 3 has type > ‘const char*’ ../src/glob.cc:120: warning: format ‘%i’ expects type ‘int’, but argument 3 has type > ‘glob_t*’ ../src/glob.cc:120: warning: format ‘%i’ expects type ‘int’, but argument 3 has type > ‘glob_t*’ ../src/glob.cc:120: warning: format ‘%i’ expects type ‘int’, but argument 3 has type > ‘const char*’ ../src/glob.cc:120: warning: format ‘%i’ expects type ‘int’, but argument 3 has type > ‘const char*’ ../src/glob.cc:122: warning: format ‘%i’ expects type ‘int’, but argument 3 has type > ‘glob_request*’ ../src/glob.cc:122: warning: format ‘%i’ expects type ‘int’, but argument 3 has type > ‘glob_request*’ ../src/glob.cc:122: warning: format ‘%i’ expects type ‘int’, but argument 3 has type > ‘const char*’ ../src/glob.cc:122: warning: format ‘%i’ expects type ‘int’, but argument 3 has type > ‘const char*’ ../src/glob.cc: In function ‘v8::Handle<v8::Value> GlobAsync(const v8::Arguments&)’: ../src/glob.cc:142: warning: format ‘%i’ expects type ‘int’, but argument 3 has type > ‘glob_request*’ ../src/glob.cc:142: warning: format ‘%i’ expects type ‘int’, but argument 3 has type > ‘glob_request*’ ../src/glob.cc:142: warning: format ‘%i’ expects type ‘int’, but argument 3 has type > ‘const char*’ ../src/glob.cc:142: warning: format ‘%i’ expects type ‘int’, but argument 3 has type > ‘const char*’ ../src/glob.cc:150: error: invalid conversion from ‘int (*)(eio_req*)’ to ‘void (*)(> eio_req*)’ ../src/glob.cc: In function ‘v8::Handle<v8::Value> GlobAsync(const v8::Arguments&)’: ../src/glob.cc:150: error: invalid conversion from ‘int (*)(eio_req*)’ to ‘void (*)(> eio_req*)’ ../src/glob.cc:150: error: initializing argument 1 of ‘eio_req* eio_custom(void (*)(> eio_req*), int, int (*)(eio_req*), void*)’ ../src/glob.cc:150: error: initializing argument 1 of ‘eio_req* eio_custom(void (*)(> eio_req*), int, int (*)(eio_req*), void*)’ ../src/glob.cc:151: warning: format ‘%i’ expects type ‘int’, but argument 3 has type > ‘eio_req*’ ../src/glob.cc:151: warning: format ‘%i’ expects type ‘int’, but argument 3 has type > ‘eio_req*’ ../src/glob.cc:151: warning: format ‘%i’ expects type ‘int’, but argument 3 has type > ‘const char*’ ../src/glob.cc:151: warning: format ‘%i’ expects type ‘int’, but argument 3 has type > ‘const char*’ ../src/glob.cc: In function ‘v8::Handle<v8::Value> GlobSync(const v8::Arguments&)’: ../src/glob.cc:170: warning: format ‘%i’ expects type ‘int’, but argument 3 has type > ‘const char*’ ../src/glob.cc:170: warning: format ‘%i’ expects type ‘int’, but argument 3 has type > ‘const char*’ ../src/glob.cc:175: warning: format ‘%i’ expects type ‘int’, but argument 3 has type > ‘const char*’ ../src/glob.cc:175: warning: format ‘%i’ expects type ‘int’, but argument 3 has type > ‘const char*’ Waf: Leaving directory/Users/danski/Sites/spah/node_modules/glob/build'
Build failed:
-> task failed (err #1):
{task: cxx glob.cc -> glob_3.o}
-> task failed (err #1):
{task: cxx glob.cc -> glob_6.o}
npm ERR! error installing [email protected] Error: [email protected] preinstall: node-waf clean || > true; node-waf configure build
npm ERR! error installing [email protected] sh "-c" "node-waf clean || true; node-waf > configure build" failed with 1
npm ERR! error installing [email protected] at ChildProcess. (> /usr/local/lib/node_modules/npm/lib/utils/exec.js:49:20)
npm ERR! error installing [email protected] at ChildProcess.emit (events.js:70:17)
npm ERR! error installing [email protected] at maybeExit (child_process_uv.js:227:16)
npm ERR! error installing [email protected] at Process.onexit (child_process_uv.js:262:5)
npm ERR! [email protected] preinstall: node-waf clean || true; node-waf configure build
npm ERR! sh "-c" "node-waf clean || true; node-waf configure build" failed with 1
npm ERR!
npm ERR! Failed at the [email protected] preinstall script.
npm ERR! This is most likely a problem with the glob package,
npm ERR! not with npm itself.
npm ERR! Tell the author that this fails on your system:
npm ERR! node-waf clean || true; node-waf configure build
npm ERR! You can get their info via:
npm ERR! npm owner ls glob
npm ERR! There is likely additional logging output above.
npm ERR!
npm ERR! System Darwin 11.0.0
npm ERR! command "node" "/usr/local/bin/npm" "install" "glob"
npm ERR!
npm ERR! Additional logging details can be found in:
npm ERR! /Users/danski/Sites/spah/npm-debug.log
npm not ok

Patterns starting with / don't work in 3.0.0

andreas@ubuntu:~$ npm -v
1.0.106
andreas@ubuntu:~$ node -v
v0.4.13-pre
andreas@ubuntu:~$ node
> require('glob').sync('/');
[]
> require('glob').sync('/*');
[]
> require('glob').sync('/usr/');
[]
> require('glob').sync('/usr/*');
[]

It used to work:

> andreas@ubuntu:~$ npm install [email protected]
[...]
node
> require('glob').sync('/');
[ '/' ]
> require('glob').sync('/*');
[ '/bin/',
  '/boot/',
  '/cdrom/',
  '/dev/',
  [...]
  '/vmlinuz']

npm install failure

'configure' finished successfully (0.397s)
Waf: Entering directory `/usr/local/lib/node/.npm/glob/2.0.2/package/build'
[1/4] cxx: deps/glob/glob.c -> build/default/deps/glob/glob_1.o
[2/4] cxx: deps/fnmatch/fnmatch.c -> build/default/deps/fnmatch/fnmatch_2.o
[3/4] cxx: src/glob.cc -> build/default/src/glob_3.o
[4/4] cxx_link: build/default/src/glob_3.o build/default/deps/glob/glob_1.o build/default/deps/fnmatch/fnmatch_2.o -> build/default/glob.node
/usr/bin/ld: default/deps/glob/glob_1.o: relocation R_X86_64_32 against `.rodata' can not be used when making a shared object; recompile with -fPIC
default/deps/glob/glob_1.o: could not read symbols: Bad value
collect2: ld returned 1 exit status
Waf: Leaving directory `/usr/local/lib/node/.npm/glob/2.0.2/package/build'
Build failed:  -> task failed (err #1): 
    {task: cxx_link glob_3.o,glob_1.o,fnmatch_2.o -> glob.node}
npm info [email protected] Failed to exec preinstall script
npm ERR! install failed Error: [email protected] preinstall: `node-waf clean || true; node-waf configure build`
npm ERR! install failed `sh` failed with 1
npm ERR! install failed     at ChildProcess.<anonymous> (/usr/local/lib/node/.npm/npm/0.2.17/package/lib/utils/exec.js:25:18)
npm ERR! install failed     at ChildProcess.emit (events.js:45:17)
npm ERR! install failed     at ChildProcess.onexit (child_process.js:171:12)
npm info install failed rollback

I get this error when attempting to run 'npm install glob.'

Impossible to compile

Checking for program g++ or c++ : /usr/lib/ccache/bin/g++
Checking for program cpp : /usr/lib/ccache/bin/cpp
Checking for program ar : /usr/bin/ar
Checking for program ranlib : /usr/bin/ranlib
Checking for g++ : ok
Checking for node path : not found
Checking for node prefix : ok /usr
'configure' finished successfully (0.036s)
Waf: Entering directory /home/gildo/pub/npm/node_modules/glob/build' [1/8] cxx: deps/glob/glob.c -> build/Release/deps/glob/glob_1.o [2/8] cxx: deps/fnmatch/fnmatch.c -> build/Release/deps/fnmatch/fnmatch_2.o [3/8] cxx: src/glob.cc -> build/Release/src/glob_3.o [4/8] cxx: deps/glob/glob.c -> build/Release/deps/glob/glob_4.o [5/8] cxx: deps/fnmatch/fnmatch.c -> build/Release/deps/fnmatch/fnmatch_5.o [6/8] cxx: src/glob.cc -> build/Release/src/glob_6.o ../src/glob.cc: In function ‘v8::Handle<v8::Value> GlobAsync(const v8::Arguments&)’: ../src/glob.cc:150:76: error: invalid conversion from ‘int (*)(eio_req*) {aka int (*)(eio_req*)}’ to ‘void (*)(eio_req*) {aka void (*)(eio_req*)}’ [-fpermissive] /usr/include/node/eio.h:334:10: error: initializing argument 1 of ‘eio_req* eio_custom(void (*)(eio_req*), int, eio_cb, void*)’ [-fpermissive] ../src/glob.cc: In function ‘int EIO_Glob(eio_req*)’: ../src/glob.cc:87:27: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast] ../src/glob.cc: In function ‘int EIO_GlobAfter(eio_req*)’: ../src/glob.cc:101:29: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast] ../src/glob.cc: In function ‘v8::Handle<v8::Value> GlobAsync(const v8::Arguments&)’: ../src/glob.cc:150:76: error: invalid conversion from ‘int (*)(eio_req*) {aka int (*)(eio_req*)}’ to ‘void (*)(eio_req*) {aka void (*)(eio_req*)}’ [-fpermissive] /usr/include/node/eio.h:334:10: error: initializing argument 1 of ‘eio_req* eio_custom(void (*)(eio_req*), int, eio_cb, void*)’ [-fpermissive] ../src/glob.cc: In function ‘v8::Handle<v8::Value> GlobSync(const v8::Arguments&)’: ../src/glob.cc:170:23: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast] ../src/glob.cc:175:25: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast] Waf: Leaving directory/home/gildo/pub/npm/node_modules/glob/build'
Build failed:
-> task failed (err #1):
{task: cxx glob.cc -> glob_3.o}
-> task failed (err #1):
{task: cxx glob.cc -> glob_6.o}
npm ERR! error installing [email protected] Error: [email protected] preinstall: node-waf clean || true; node-waf configure build
npm ERR! error installing [email protected] sh "-c" "node-waf clean || true; node-waf configure build" failed with 1
npm ERR! error installing [email protected] at ChildProcess. (/usr/lib/node_modules/npm/lib/utils/exec.js:49:20)
npm ERR! error installing [email protected] at ChildProcess.emit (events.js:70:17)
npm ERR! error installing [email protected] at ChildProcess.onexit (child_process_legacy.js:246:12)
npm ERR! [email protected] preinstall: node-waf clean || true; node-waf configure build
npm ERR! sh "-c" "node-waf clean || true; node-waf configure build" failed with 1
npm ERR!
npm ERR! Failed at the [email protected] preinstall script.
npm ERR! This is most likely a problem with the glob package,
npm ERR! not with npm itself.
npm ERR! Tell the author that this fails on your system:
npm ERR! node-waf clean || true; node-waf configure build
npm ERR! You can get their info via:
npm ERR! npm owner ls glob
npm ERR! There is likely additional logging output above.
npm ERR!
npm ERR! System Linux 3.0-ARCH
npm ERR! command "node" "/usr/bin/npm" "install" "glob"
npm ERR! cwd /home/gildo/pub/npm
npm ERR! node -v v0.5.5-pre
npm ERR! npm -v 1.0.27
npm ERR!
npm ERR! Additional logging details can be found in:
npm ERR! /home/gildo/pub/npm/npm-debug.log

Problem installing glob with npm bundle

Hey,

I'm having an issue running "npm bundle" with npm version 0.3.18 and it seems to be failing with glob. Running "npm bundle install glob" gives the following output:

# npm bundle install glob
npm info it worked if it ends with ok
npm info using [email protected]
npm info using [email protected]
npm info preinstall [email protected]
chdir(): Permission denied
npm info [email protected] Failed to exec preinstall script
npm ERR! install failed Error: [email protected] preinstall: `node-waf clean || true; node-waf configure build`
npm ERR! install failed `sh "-c" "node-waf clean || true; node-waf configure build"` failed with 127
npm ERR! install failed     at ChildProcess.<anonymous> (/usr/lib/node/.npm/npm/0.3.18/package/lib/utils/exec.js:49:20)
npm ERR! install failed     at ChildProcess.emit (events.js:45:17)
npm ERR! install failed     at ChildProcess.onexit (child_process.js:171:12)
npm info install failed rollback
npm info uninstall [ '[email protected]' ]
npm info preuninstall [email protected]
npm info uninstall [email protected]
npm info auto-deactive not symlink
npm info postuninstall [email protected]
npm info uninstall [email protected] complete
npm info install failed rolled back
npm ERR! Error: [email protected] preinstall: `node-waf clean || true; node-waf configure build`
npm ERR! `sh "-c" "node-waf clean || true; node-waf configure build"` failed with 127
npm ERR!     at ChildProcess.<anonymous> (/usr/lib/node/.npm/npm/0.3.18/package/lib/utils/exec.js:49:20)
npm ERR!     at ChildProcess.emit (events.js:45:17)
npm ERR!     at ChildProcess.onexit (child_process.js:171:12)
npm ERR! 
npm ERR! Failed at the [email protected] preinstall script.
npm ERR! This is most likely a problem with the glob package,
npm ERR! not with npm itself.
npm ERR! Tell the author that this fails on your system:
npm ERR!     node-waf clean || true; node-waf configure build
npm ERR! You can get their info via:
npm ERR!     npm owner ls glob
npm ERR! There is likely additional logging output above.
npm ERR! System Linux 2.6.32-305-ec2
npm ERR! argv { remain: [ 'glob' ],
npm ERR! argv   cooked: 
npm ERR! argv    [ 'bundle',
npm ERR! argv      'install',
npm ERR! argv      'glob' ],
npm ERR! argv   original: 
npm ERR! argv    [ 'bundle',
npm ERR! argv      'install',
npm ERR! argv      'glob' ] }
npm not ok

Trying a pure "npm install glob" the install runs correctly and I'm able to require('glob'). Even after doing this "npm bundle" fails. I'm not sure how to diagnose the problem further, so any advice would be greatly appreciated!

Thanks!
Greg

Issue with version > 3.1.14

This works:

var glob = require('glob');
var path = require('path');

glob('**/*', function (err, matches) {
 console.log(matches); // matches is ok
});

This doesn't work:

var glob = require('glob');
var path = require('path');

glob(__dirname + '/**/*', function (err, matches) {
 console.log(matches); // matches is []
});

Happens in windows, dunno in other OS's.

Segfault

require("glob").glob("*/.js", function (err, paths) {})

[1] 6330 segmentation fault node

The glob 2.0.7 release includes a /build folder

It happens to even the best of us, I guess :)

andreas@ubuntu:~$ npm install -g [email protected]

> [email protected] preinstall /home/andreas/.local/lib/node_modules/glob
> node-waf clean || true; node-waf configure build

Nothing to clean (project not configured)
Checking for program g++ or c++          : /usr/bin/g++ 
Checking for program cpp                 : /usr/bin/cpp 
Checking for program ar                  : /usr/bin/ar 
Checking for program ranlib              : /usr/bin/ranlib 
Checking for g++                         : ok  
Checking for node path                   : ok /home/andreas/.node_libraries 
Checking for node prefix                 : ok /home/andreas/.local 
'configure' finished successfully (0.125s)
Waf: Entering directory `/Users/isaacs/dev-src/js/node-glob/build'
Waf: Leaving directory `/Users/isaacs/dev-src/js/node-glob/build'
Traceback (most recent call last):
  File "/home/andreas/.local/bin/node-waf", line 16, in <module>
    Scripting.prepare(t, os.getcwd(), VERSION, wafdir)
  File "/home/andreas/.local/bin/../lib/node/wafadmin/Scripting.py", line 145, in prepare
    prepare_impl(t, cwd, ver, wafdir)
  File "/home/andreas/.local/bin/../lib/node/wafadmin/Scripting.py", line 135, in prepare_impl
    main()
  File "/home/andreas/.local/bin/../lib/node/wafadmin/Scripting.py", line 188, in main
    fun(ctx)
  File "/home/andreas/.local/bin/../lib/node/wafadmin/Scripting.py", line 386, in build
    return build_impl(bld)
  File "/home/andreas/.local/bin/../lib/node/wafadmin/Scripting.py", line 405, in build_impl
    bld.compile()
  File "/home/andreas/.local/bin/../lib/node/wafadmin/Build.py", line 268, in compile
    os.chdir(self.bldnode.abspath())
OSError: [Errno 2] No such file or directory: '/Users/isaacs/dev-src/js/node-glob/build'
npm ERR! error installing [email protected] Error: [email protected] preinstall: `node-waf clean || true; node-waf configure build`
npm ERR! error installing [email protected] `sh "-c" "node-waf clean || true; node-waf configure build"` failed with 1
npm ERR! error installing [email protected]     at ChildProcess.<anonymous>     (/home/andreas/.local/lib/node_modules/npm/lib/utils/exec.js:49:20)
npm ERR! error installing [email protected]     at ChildProcess.emit (events.js:67:17)
npm ERR! error installing [email protected]     at ChildProcess.onexit (child_process.js:192:12)
npm ERR! [email protected] preinstall: `node-waf clean || true; node-waf configure build`
npm ERR! `sh "-c" "node-waf clean || true; node-waf configure build"` failed with 1
npm ERR! 
npm ERR! Failed at the [email protected] preinstall script.
npm ERR! This is most likely a problem with the glob package,
npm ERR! not with npm itself.
npm ERR! Tell the author that this fails on your system:
npm ERR!     node-waf clean || true; node-waf configure build
npm ERR! You can get their info via:
npm ERR!     npm owner ls glob
npm ERR! There is likely additional logging output above.
npm ERR! 
npm ERR! System Linux 2.6.35-28-generic
npm ERR! command "node" "/home/andreas/.local/bin/npm" "install" "-g" "[email protected]"
npm ERR! 
npm ERR! Additional logging details can be found in:
npm ERR!     /home/andreas/.npm/glob/npm-debug.log
npm not ok

Here's the problem:

andreas@ubuntu:~$ tar tz < ~/.npm/glob/2.0.7/package.tgz 
package/wscript
package/LICENCE
package/README.md
package/.gitignore
package/Makefile
package/package.json
package/lib/glob.js
package/src/glob_constants.h
package/src/glob.cc
package/deps/fnmatch/LICENSE
package/deps/fnmatch/fnmatch.c
package/deps/fnmatch/fnmatch.h
package/deps/glob/LICENSE
package/deps/glob/glob.c
package/deps/glob/glob.h
package/test/test.js
package/test/foo/bar
package/test/foo/baz/bar
package/test/foo/baz/quux/bar
package/build/config.log
package/build/.wafpickle-7
package/build/c4che/default.cache.py
package/build/c4che/build.config.py
package/build/default/glob_g.node
package/build/default/glob.node
package/build/default/src/glob_6.o
package/build/default/src/glob_3.o
package/build/default/deps/fnmatch/fnmatch_2.o
package/build/default/deps/fnmatch/fnmatch_5.o
package/build/default/deps/glob/glob_4.o
package/build/default/deps/glob/glob_1.o

undefined symbol: ev_rt_now with node v0.5.4-pre

I get the following error when trying to use glob with node v0.5.4-pre...

Error: /home/tim/local/lib/node_modules/glob/build/default/glob.node: undefined symbol: ev_rt_now

I'm sure this has to do with changes in node itself but a fix for glob would be most appreciated!

Drive letters on Windows return empty results

If I pass glob a search spec that includes a drive letter, it returns an empty array.

> process.cwd()
'C:\\dev'
> glob.sync('/temp/*.txt')
[ '\\temp/1.txt' ]
> glob.sync('c:/temp/*.txt')
[]

The two queries should return the same results (since C: is the current drive), but when I specify the full path including drive letter, glob returns nothing.

GLOB_NOSPACE error when glob results in >128 files

To reproduce, generate 129 or more files in an empty directory:

perl -e 'for (1..129) {system "touch $_.dat";}'

And then run this snippet in the node repl:

require('glob').glob('*', function (err, fileNames) {console.log(arguments);});

On Ubuntu 10.10 and glob 2.0.5 this results in:

> { '0': 
   { message: 'GLOB_NOSPACE'
   , stack: [Getter/Setter]
   , errno: -1
   }
, '1': '*'
}

From a couple of quick tests I concluded that it's the number of entries in the result that matters, not the number of candidate files.

glob 1.1.0 worked fine with any number of entries I've thrown at it so far.

Build fails (‘strlcpy’ was not declared in this scope)

On a fresh ubuntu maverick install, installing node-glob with npm fails:

'clean' finished successfully (0.020s)
Checking for program g++ or c++          : /usr/bin/g++ 
Checking for program cpp                 : /usr/bin/cpp 
Checking for program ar                  : /usr/bin/ar 
Checking for program ranlib              : /usr/bin/ranlib 
Checking for g++                         : ok  
Checking for node path                   : not found 
Checking for node prefix                 : ok /opt/node-0.4.0 
'configure' finished successfully (0.060s)
Waf: Entering directory `/opt/node-0.4.0/lib/node/.npm/.cache/glob/2.0.0/package/build'
 [1/4] cxx: deps/glob/glob.c -> build/default/deps/glob/glob_1.o
../deps/glob/glob.c:36: warning: extra tokens at end of #ifdef directive
../deps/glob/glob.c:1188: warning: extra tokens at end of #ifdef directive
../deps/glob/glob.c: In function ‘DIR* g_opendir(Char*, glob_t*)’:
../deps/glob/glob.c:1074: error: ‘strlcpy’ was not declared in this scope
Waf: Leaving directory `/opt/node-0.4.0/lib/node/.npm/.cache/glob/2.0.0/package/build'
Build failed:  -> task failed (err \#1): 
    {task: cxx glob.c -> glob_1.o}

strlcpy is available in libbsd on debian based distros.

compile-time warnings

isabel:webclient pyrotechnick$ npm i glob

> [email protected] preinstall /Users/pyrotechnick/webclient/node_modules/glob
> node-waf clean || true; node-waf configure build

Nothing to clean (project not configured)
Checking for program g++ or c++          : /usr/bin/g++ 
Checking for program cpp                 : /usr/bin/cpp 
Checking for program ar                  : /usr/bin/ar 
Checking for program ranlib              : /usr/bin/ranlib 
Checking for g++                         : ok  
Checking for node path                   : ok /usr/local/lib/node_modules 
Checking for node prefix                 : ok /usr/local 
'configure' finished successfully (0.236s)
Waf: Entering directory `/Users/pyrotechnick/webclient/node_modules/glob/build'
[1/8] cxx: deps/glob/glob.c -> build/default/deps/glob/glob_1.o
[2/8] cxx: deps/fnmatch/fnmatch.c -> build/default/deps/fnmatch/fnmatch_2.o
[3/8] cxx: src/glob.cc -> build/default/src/glob_3.o
[4/8] cxx: deps/glob/glob.c -> build/default/deps/glob/glob_4.o
[5/8] cxx: deps/fnmatch/fnmatch.c -> build/default/deps/fnmatch/fnmatch_5.o
[6/8] cxx: src/glob.cc -> build/default/src/glob_6.o
../src/glob.cc: In function ‘int EIO_Glob(eio_req*)’:
../src/glob.cc:87: warning: format ‘%i’ expects type ‘int’, but argument 3 has type ‘const char*’
../src/glob.cc: In function ‘int EIO_GlobAfter(eio_req*)’:
../src/glob.cc:101: warning: format ‘%i’ expects type ‘int’, but argument 3 has type ‘const char*’
../src/glob.cc:120: warning: format ‘%i’ expects type ‘int’, but argument 3 has type ‘glob_t*’
../src/glob.cc:120: warning: format ‘%i’ expects type ‘int’, but argument 3 has type ‘const char*’
../src/glob.cc:122: warning: format ‘%i’ expects type ‘int’, but argument 3 has type ‘glob_request*’
../src/glob.cc:122: warning: format ‘%i’ expects type ‘int’, but argument 3 has type ‘const char*’
../src/glob.cc: In function ‘v8::Handle<v8::Value> GlobAsync(const v8::Arguments&)’:
../src/glob.cc:142: warning: format ‘%i’ expects type ‘int’, but argument 3 has type ‘glob_request*’
../src/glob.cc:142: warning: format ‘%i’ expects type ‘int’, but argument 3 has type ‘const char*’
../src/glob.cc:151: warning: format ‘%i’ expects type ‘int’, but argument 3 has type ‘eio_req*’
../src/glob.cc:151: warning: format ‘%i’ expects type ‘int’, but argument 3 has type ‘const char*’
../src/glob.cc: In function ‘v8::Handle<v8::Value> GlobSync(const v8::Arguments&)’:
../src/glob.cc:170: warning: format ‘%i’ expects type ‘int’, but argument 3 has type ‘const char*’
../src/glob.cc:175: warning: format ‘%i’ expects type ‘int’, but argument 3 has type ‘const char*’
[7/8] cxx_link: build/default/src/glob_3.o build/default/deps/glob/glob_1.o build/default/deps/fnmatch/fnmatch_2.o -> build/default/glob.node
[8/8] cxx_link: build/default/src/glob_6.o build/default/deps/glob/glob_4.o build/default/deps/fnmatch/fnmatch_5.o -> build/default/glob_g.node
Waf: Leaving directory `/Users/pyrotechnick/webclient/node_modules/glob/build'
'build' finished successfully (1.250s)
[email protected] ./node_modules/glob 

store Stats in statCache

I'd like to request that node-glob store Stats objects, instead / in addition to storing whether the path exists / is a directory. I'm building my own filesystem library, and stat the globbed paths all over again.

"nocase" option fails when * or ? is not present

When the glob pattern doesn't have any "fancy" characters in it, the nocase option doesn't appear to work.

The first two examples below behave as expected, but I expect the final example to return [ 'Foo.txt', 'foo.txt' ] instead of [ 'Foo.txt' ].

I tested this in a Mac OS Extended (Case-sensitive, Journaled) partition disk image.

$ npm install glob --silent
[email protected] node_modules/glob
├── [email protected]
├── [email protected]
└── [email protected] ([email protected], [email protected])

$ touch Foo.txt foo.txt

$ node -pe 'require("glob").sync("Foo.*")'
[ 'Foo.txt' ]

$ node -pe 'require("glob").sync("Foo.*", {nocase: true})'
[ 'Foo.txt', 'foo.txt' ]

$ node -pe 'require("glob").sync("Foo.txt", {nocase: true})'
[ 'Foo.txt' ]

Mark option not marking directories in windows in some edge cases

var glob = require('glob');

glob(__dirname, { mark: true }, function (err, matches) {
    console.log(matches); // Contains the __dirname with \ added to the end
});
var glob = require('glob');

glob(__dirname + '\\', { mark: true }, function (err, matches) {
    console.log(matches); // Contains the __dirname without the \ or / added to the end
});

I know that in this case, using glob is not necessary. Thing is that the pattern is dynamically assigned.
This only happens on windows. In my MacOSX, the example above works as expected.

error running when bundled with npm

if glob is bundled with npm I get the following error

Error: Cannot find module '../build/default/glob'
at Function._resolveFilename (module.js:299:11)
at Function._load (module.js:245:25)
at require (module.js:327:19)
at Object. (/Users/weepy/Projects/mewcal/node_modules/.npm/glob/2.0.5/package/lib/glob.js:3:15)

glob.sync() gives TypeError: object is not a function

I'm using glob 3.1.5, node 0.4.12, npm 1.0.103

I can't use glob.sync. Here is node output:

$ node
> var glob = require("glob");
> glob.sync("/tmp/*")
TypeError: object is not a function
    at Object.CALL_NON_FUNCTION (native)
    at Glob._process (/usr/local/lib/node_modules/glob/glob.js:234:3)
    at Glob.iterator (/usr/local/lib/node_modules/glob/glob.js:157:10)
    at native
    at Array.forEach (native)
    at new Glob (/usr/local/lib/node_modules/glob/glob.js:155:22)
    at glob (/usr/local/lib/node_modules/glob/glob.js:57:11)
    at Function.globSync [as sync] (/usr/local/lib/node_modules/glob/glob.js:76:10)
    at [object Context]:1:6
    at Interface.<anonymous> (repl.js:179:22)

npm test glob also fails.

not building on solaris

../deps/fnmatch/fnmatch.c../deps/glob/glob.c:40:23::40:23 : sys/cdefs.hsys/cdefs.h: No such file or directory: 
No such file or directory
In file included from ../deps/fnmatch/fnmatch.c:68:
../deps/fnmatch/fnmatch.h:54: error: expected constructor, destructor, or type conversion before "int"
../deps/fnmatch/fnmatch.h:54: error: expected `,' or `;' before "int"
In file included from /usr/include/string.h:33,
                 from ../deps/fnmatch/fnmatch.c:69:
/usr/include/iso/string_iso.h:51: error: expected constructor, destructor, or type conversion before "extern"
/usr/include/iso/string_iso.h:51: error: expected `,' or `;' before "extern"
In file included from ../deps/fnmatch/fnmatch.c:69:
/usr/include/string.h:74: error: `size_t' has not been declared
/usr/include/string.h:87: error: `size_t' has not been declared
/usr/include/string.h:92: error: `size_t' has not been declared
/usr/include/string.h:93: error: `size_t' has not been declared
/usr/include/string.h:95: error: `size_t' does not name a type
/usr/include/string.h:98: error: `size_t' has not been declared
/usr/include/string.h:99: error: `size_t' does not name a type
/usr/include/string.h:100: error: `size_t' does not name a type
In file included from ../src/glob_constants.h:2,
                 from ../src/glob.cc:7:
../deps/glob/glob.h:40:23: sys/cdefs.h: No such file or directory
../deps/fnmatch/fnmatch.c:129: error: `size_t' has not been declared
../deps/fnmatch/fnmatch.c:130: error: ISO C++ forbids declaration of `recursion' with no type
../deps/fnmatch/fnmatch.c: In function `int fnmatchx(const char*, const char*, int, int)':
../deps/fnmatch/fnmatch.c:172: error: `strchr' undeclared (first use this function)
../deps/fnmatch/fnmatch.c:172: error: (Each undeclared identifier is reported only once for each function it appears in.)
In file included from ../src/glob_constants.h:2,
                 from ../src/glob.cc:7:
../deps/glob/glob.h:102: error: expected constructor, destructor, or type conversion before "int"
../deps/glob/glob.h:102: error: expected `,' or `;' before "int"
In file included from ../src/glob_constants.h:3,
                 from ../src/glob.cc:7:
../deps/glob/glob.h:109: error: `__END_DECLS' does not name a type
In file included from /opt/nodejs/v0.2.6/include/node/node.h:5,
                 from ../src/glob_constants.h:4,
                 from ../src/glob.cc:7:
/opt/nodejs/v0.2.6/include/node/ev.h:44: error: expected constructor, destructor, or type conversion before "extern"
/opt/nodejs/v0.2.6/include/node/ev.h:44: error: expected `,' or `;' before "extern"
In file included from ../deps/glob/glob.c:88:
../deps/glob/glob.h:102: error: expected constructor, destructor, or type conversion before "int"
../deps/glob/glob.h:102: error: expected `,' or `;' before "int"
In file included from /usr/sfw/lib/gcc/i386-pc-solaris2.11/3.4.3/include/stdio.h:14,
                 from /usr/include/pwd.h:41,
                 from ../deps/glob/glob.c:89:
/usr/sfw/lib/gcc/i386-pc-solaris2.11/3.4.3/include/stdarg.h:43: error: expected constructor, destructor, or type conversion before "typedef"
/usr/sfw/lib/gcc/i386-pc-solaris2.11/3.4.3/include/stdarg.h:43: error: expected `,' or `;' before "typedef"
In file included from /usr/include/stdlib.h:39,
                 from ../src/glob.cc:11:
/usr/include/sys/wait.h:104: error: `idtype_t' was not declared in this scope
/usr/include/sys/wait.h:104: error: expected primary-expression before ',' token
/usr/include/sys/wait.h:104: error: `siginfo_t' was not declared in this scope
/usr/include/sys/wait.h:104: error: expected primary-expression before ',' token
/usr/include/sys/wait.h:104: error: expected primary-expression before "int"
/usr/include/sys/wait.h:104: error: initializer expression list treated as compound expression
../deps/glob/glob.c: In function `int glob3(Char*, Char*, Char*, const Char*, const Char*, const Char*, glob_t*, size_t*)':
../deps/glob/glob.c:857: error: expected primary-expression before "struct"
../deps/glob/glob.c:857: error: expected `)' before "struct"
../src/glob.cc: In function `v8::Handle<v8::Value> FNMatch(const v8::Arguments&)':
../src/glob.cc:71: error: `fnmatch' undeclared (first use this function)
../src/glob.cc:71: error: (Each undeclared identifier is reported only once for each function it appears in.)
../src/glob.cc: In function `int EIO_Glob(eio_req*)':
../src/glob.cc:87: error: `glob' undeclared (first use this function)
../src/glob.cc: In function `int EIO_GlobAfter(eio_req*)':
../src/glob.cc:92: error: `ev_unref' undeclared (first use this function)
../src/glob.cc: In function `v8::Handle<v8::Value> GlobAsync(const v8::Arguments&)':
../src/glob.cc:140: error: `ev_ref' undeclared (first use this function)
../src/glob.cc: In function `v8::Handle<v8::Value> GlobSync(const v8::Arguments&)':
../src/glob.cc:158: error: `glob' undeclared (first use this function)

Readme

Would be nice to actually include what glob does, since looking for "glob" without knowing "glob" is not that easy (google has no way to direct here) and it's also nice to be assured at the beginning of the Readme what the package does :).

Windows absolute paths ignores root path

The fix from #53 causes paths prefixed with / to not mount the path on top of the passed in root path.

When I pass in { root: 'c:\foo' } or something like that, and try to match /bar i will end up with c:\bar instead of what I expect, which is c:\foo\bar. Or at least I've understood that's how it works from reading the documentation.

  • 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.)
path.resolve('/foo/bar', './baz')
// returns
'/foo/bar/baz'

path.resolve('/foo/bar', '/tmp/file/')
// returns
'/tmp/file'

path.resolve('wwwroot', 'static_files/png/', '../gif/image.gif')
// if currently in /home/myself/node, it returns
'/home/myself/node/wwwroot/static_files/gif/image.gif'

Passing in a leading / to any argument of path.resolve() seems to mount it at root. My suggestion is to not try and do too much to Windows absolute paths in prefix.

It all works for me when I uncomment these lines, but I understand it breaks the previous issue: https://github.com/isaacs/node-glob/blob/master/glob.js#L369-L370

fnmatch should be a separate thing

Dunno if maybe it should be require("glob/fnmatch") or maybe segregate the constants somehow, but the GLOB_ constants and the FNM_ constants should probably not live on the same object, or else it's a bit tricky to translate them back and forth.

Mark issue with single *

If the pattern is something like dir/*, matching directories will not have a trailing / with the mark option enabled.

However dir/*/** marks directories as expected.

Rake's FileList like matching against several patterns

Hi. I think it's useful to have general FileList like utility and it seems to be easy to extend current implementation to handle multiple include-exclude patterns. I am going to do it anyway, but it's much easier to do for creator.

In Windows, patterns ending in / match files, too

I'm seeing this bug in v3.1.14. Given this directory structure:

deep
├── deep.txt
└── deeper
    ├── deeper.txt
    └── deepest
        └── deepest.txt

In OS X I get this (which is expected)

$ node -pe "require('glob').sync('d**/**')"
[ 'deep',
  'deep/deep.txt',
  'deep/deeper',
  'deep/deeper/deeper.txt',
  'deep/deeper/deepest',
  'deep/deeper/deepest/deepest.txt' ]

$ node -pe "require('glob').sync('d**/**/')"
[ 'deep/', 'deep/deeper/', 'deep/deeper/deepest/' ]

But in Windows I get this (this is not expected)

C:\test>node -pe "require('glob').sync('d**/**')"
[ 'deep',
  'deep/deep.txt',
  'deep/deeper',
  'deep/deeper/deeper.txt',
  'deep/deeper/deepest',
  'deep/deeper/deepest/deepest.txt' ]

C:\test>node -pe "require('glob').sync('d**/**/')"
[ 'deep/',
  'deep/deep.txt/',
  'deep/deeper/',
  'deep/deeper/deeper.txt/',
  'deep/deeper/deepest/',
  'deep/deeper/deepest/deepest.txt/' ]

Fails to build on v0.5.9

npm install says:

> [email protected] preinstall /Users/maciej/Programowanie/Sources/node-glob
> node-waf clean || true; node-waf configure build

Nothing to clean (project not configured)
Checking for program g++ or c++          : /usr/bin/g++ 
Checking for program cpp                 : /usr/bin/cpp 
Checking for program ar                  : /usr/bin/ar 
Checking for program ranlib              : /usr/bin/ranlib 
Checking for g++                         : ok  
Checking for node path                   : not found 
Checking for node prefix                 : ok /usr/local 
'configure' finished successfully (0.045s)
Waf: Entering directory `/Users/maciej/Programowanie/Sources/node-glob/build'
[1/8] cxx: deps/glob/glob.c -> build/Release/deps/glob/glob_1.o
[2/8] cxx: deps/fnmatch/fnmatch.c -> build/Release/deps/fnmatch/fnmatch_2.o
[3/8] cxx: src/glob.cc -> build/Release/src/glob_3.o
[4/8] cxx: deps/glob/glob.c -> build/Release/deps/glob/glob_4.o
[5/8] cxx: deps/fnmatch/fnmatch.c -> build/Release/deps/fnmatch/fnmatch_5.o
../src/glob.cc: In function ‘v8::Handle<v8::Value> GlobAsync(const v8::Arguments&)’:
../src/glob.cc:150: error: invalid conversion from ‘int (*)(eio_req*)’ to ‘void (*)(eio_req*)’
../src/glob.cc:150: error:   initializing argument 1 of ‘eio_req* eio_custom(void (*)(eio_req*), int, int (*)(eio_req*), void*)’
Waf: Leaving directory `/Users/maciej/Programowanie/Sources/node-glob/build'
Build failed:  -> task failed (err #1): 
    {task: cxx glob.cc -> glob_3.o}
npm ERR! [email protected] preinstall: `node-waf clean || true; node-waf configure build`
npm ERR! `sh "-c" "node-waf clean || true; node-waf configure build"` failed with 1
npm ERR! 
npm ERR! Failed at the [email protected] preinstall script.
npm ERR! This is most likely a problem with the glob package,
npm ERR! not with npm itself.
npm ERR! Tell the author that this fails on your system:
npm ERR!     node-waf clean || true; node-waf configure build
npm ERR! You can get their info via:
npm ERR!     npm owner ls glob
npm ERR! There is likely additional logging output above.
npm ERR! 
npm ERR! System Darwin 11.2.0
npm ERR! command "node" "/usr/local/bin/npm" "install"
npm ERR! cwd /Users/maciej/Programowanie/Sources/node-glob
npm ERR! node -v v0.5.9
npm ERR! npm -v 1.0.99
npm ERR! code ELIFECYCLE
npm ERR! 
npm ERR! Additional logging details can be found in:
npm ERR!     /Users/maciej/Programowanie/Sources/node-glob/npm-debug.log
npm not ok

`cwd` option not used on second call

This works:

GLOB(path1 + "/**", {
  cwd: path1
}, function (err, files)
{
});

GLOB(path2 + "/**", {
  cwd: path2
}, function (err, files)
{
});

This does NOT work but should:

GLOB("**", {
  cwd: path1
}, function (err, files)
{
});

GLOB("**", {
  cwd: path2
}, function (err, files)
{
});

Glob filter

I need to be able to filter the result of a glob operation using gitignore rules.

Would it make sense to add a filter plugin API which I can use to implement the gitignore rule set?

Should I use glob through another lib that already does something like this?

Absolute paths on Windows do not work

Steps to repro:

  • Create a folder C:\test
  • Put some *.txt files in there
  • Try to get them using glob.sync('/test/*.txt')

Note:

  • All backslashes were correctly replaced by forward-slashes

Expected result:

  • An array with a list of files of the folder C:\test

Actual result:

  • [](empty array)

PS:

  • It works using relative paths, it's only absolute paths that seem to be broken

node-glob package installation failed

Hi, I'm quite new with node's stuff but I really want to understand why I failed,
I recently tried to install node-glob but I failed for something which seems to be a compilation error.

Here is my bug report, hoping someone can point me out what is wrong

○ npm install -g glob                                                                                                                      

> [email protected] preinstall /home/blackpanther/local/node/lib/node_modules/glob
> node-waf clean || true; node-waf configure build

Nothing to clean (project not configured)
Checking for program g++ or c++          : /usr/bin/g++ 
Checking for program cpp                 : /usr/bin/cpp 
Checking for program ar                  : /usr/bin/ar 
Checking for program ranlib              : /usr/bin/ranlib 
Checking for g++                         : ok  
Checking for node path                   : not found 
Checking for node prefix                 : ok /home/blackpanther/app/node 
'configure' finished successfully (0.032s)
Waf: Entering directory `/home/blackpanther/app/node/lib/node_modules/glob/build'
[1/8] cxx: deps/glob/glob.c -> build/default/deps/glob/glob_1.o
[2/8] cxx: deps/fnmatch/fnmatch.c -> build/default/deps/fnmatch/fnmatch_2.o
[3/8] cxx: src/glob.cc -> build/default/src/glob_3.o
[4/8] cxx: deps/glob/glob.c -> build/default/deps/glob/glob_4.o
../src/glob.cc: In function ‘v8::Handle<v8::Value> GlobAsync(const v8::Arguments&)’:
../src/glob.cc:150:76: error: invalid conversion from ‘int (*)(eio_req*)’ to ‘void (*)(eio_req*)’ [-fpermissive]
/home/blackpanther/app/node/include/node/eio.h:334:10: error:   initializing argument 1 of ‘eio_req* eio_custom(void (*)(eio_req*), int, eio_cb, void*)’ [-fpermissive]
Waf: Leaving directory `/home/blackpanther/app/node/lib/node_modules/glob/build'
Build failed:  -> task failed (err #1): 
{task: cxx glob.cc -> glob_3.o}

...

error installing [email protected] Error: [email protected] preinstall: `node-waf clean || true; node-waf configure build`
error installing [email protected] `sh "-c" "node-waf clean || true; node-waf configure build"` failed with 1
error installing [email protected]     at ChildProcess.<anonymous> (/home/blackpanther/app/node/lib/node_modules/npm/lib/utils/exec.js:49:20)
error installing [email protected]     at ChildProcess.emit (events.js:70:17)
error installing [email protected]     at ChildProcess.onexit (child_process_legacy.js:246:12)
[email protected] preinstall: `node-waf clean || true; node-waf configure build`
`sh "-c" "node-waf clean || true; node-waf configure build"` failed with 1

Failed at the [email protected] preinstall script.
This is most likely a problem with the glob package,
not with npm itself.
Tell the author that this fails on your system:
    node-waf clean || true; node-waf configure build
You can get their info via:
    npm owner ls glob
There is likely additional logging output above.

System Linux 3.0.0-1-amd64
command "node" "/home/blackpanther/local/node/bin/npm" "install" "-g" "glob"
cwd /home/blackpanther
node -v v0.5.5-pre
npm -v 1.0.26

Additional logging details can be found in:
    /home/blackpanther/npm-debug.log

**something doesn't dive into subdirs

andreas@ubuntu:~/foo$ npm install [email protected]
npm WARN [email protected] package.json: bugs['web'] should probably be bugs['url']
[email protected] ../node_modules/glob 
├── [email protected]
├── [email protected] ([email protected])
└── [email protected]
> andreas@ubuntu:~/foo$ mkdir subdir
andreas@ubuntu:~/foo$ touch subdir/bar.txt
andreas@ubuntu:~/foo$ node
> require('glob').sync('**.txt');
skip final stat
[]

In 2.0.9 this (correctly) returned ['subdir/bar.txt'].

It also looks like some debug console.error statements made it into 3.1.1, I'm seeing a lot of skip final stat and cb is function messages when I run my test suite.

using with require('graceful-fs') hangs writes, require('fs') works fine

I have a process which writes a large build package which I'm trying to convert to use node-glob rather than a homegrown glob routine. Unfortunately, while the sync() calls work great for read operations, it appears that graceful-fs is causing hanging write operations.

This is running on node 0.6.20 on Mac OSX 10.7.4. Changing glob.js to require('fs') fixes everything.

One question is would it be possible to change how glob operates such that you could choose the fs implementation you want? In my case I'd be happy with standard node.

Alternatively, can you suggest any reason why graceful-js would cause this behavior? I've tried adjusting the limits on open files etc. but that didnt' seem to have any effect.

UNC (\\?\, \\hostname\) paths don't work

I realize these are "evil" for a glob, but I would normally expect this:

"//?/C:/*"

To return results. Realistically, on win32, it's reasonable to check if the path starts with //?/ (or ?\ but that's havoc, I understand) and stop right there.

I think the "root cause" is actually in minimatch, because it turns the pattern into /?/.

This is probably a pretty low priority issue. It's honestly not uncommon for UNC paths not to work sometimes in programs, and except for remote resources it's easy enough to mount the volume to a path or use the drive letter.

-[Unknown]

nonull not working?

If nonull was set, shouldn't the returned array always contain at least something? I'm using [email protected].

var glob = require('glob');

glob.sync('xyz', {nonull: true}); // []

glob('xyz', {nonull: true}, function(err, files) {
  console.log(err, files); // logs: null []
});

3.0.1: CWD is stripped from glob results when pattern starts with /

The current working directory shouldn't be relevant when globbing an absolute path, but:

andreas@ubuntu:~$ npm install [email protected]
[...]
andreas@ubuntu:~$ node
> require('glob').sync('/home/andreas/foo');
[ 'foo' ]

Same thing happens when the pattern contains wildcards.

It worked fine in previous versions:

andreas@ubuntu:~$ npm install [email protected]
[...]
andreas@ubuntu:~$ node
> require('glob').sync('/home/andreas/foo');
[ '/home/andreas/foo/' ]

Problem with globbing on Windows

e0829f6 appears to open up a problem on Windows. When using Git bash I work on n:\. npm, however, installs modules into c:\. This leads to the following problem:

path:  c:/Users/e5867u/AppData/Roaming/npm/node_modules/testacular/adapter/lib/jasmine.js
[ 'n:/c:/Users/e5867u/AppData/Roaming/npm/node_modules/testacular/adapter/lib/jasmine.js' ]

The problem is this: https://github.com/isaacs/node-glob/blob/master/glob.js#L326-L328

I tried printing the prefix before and after this if:

prefix before:  c:/Users/e5867u/AppData/Roaming/npm/node_modules/testacular/adapter/lib/jasmine.js
root:  n:/
prefix after:  n:\c:\Users\e5867u\AppData\Roaming\npm\node_modules\testacular\adapter\lib\jasmine.js

When going back to the code as it was before e0829f6, ie if (prefix.charAt(0) === "/" && !this.nomount) { the code returns as expected:

path:  c:/Users/e5867u/AppData/Roaming/npm/node_modules/testacular/adapter/lib/jasmine.js
[ 'c:/Users/e5867u/AppData/Roaming/npm/node_modules/testacular/adapter/lib/jasmine.js' ]

Negation not working when pattern begins with !

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.

"mark" option broken when pattern ends with "/"

Using [email protected] and given this directory / file structure:

$ tree deep
deep
├── deep.txt
└── deeper
    ├── deeper.txt
    └── deepest
        └── deepest.txt

The mark option behavior appears to be inverted when the pattern ends with a /:

var glob = require('glob');

// Expected behavior:

glob.sync('deep/**')
[ 'deep',
  'deep/deep.txt',
  'deep/deeper',
  'deep/deeper/deeper.txt',
  'deep/deeper/deepest',
  'deep/deeper/deepest/deepest.txt' ]

glob.sync('deep/**', {mark: true})
[ 'deep/',
  'deep/deep.txt',
  'deep/deeper/',
  'deep/deeper/deeper.txt',
  'deep/deeper/deepest/',
  'deep/deeper/deepest/deepest.txt' ]

glob.sync('deep/**/')
[ 'deep/',
  'deep/deeper/',
  'deep/deeper/deepest/' ]

// Inverse behavior:

glob.sync('deep/**/', {mark: true})
[ 'deep',
  'deep/deeper',
  'deep/deeper/deepest' ]

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.