gulpjs / glob-stream

Readable streamx interface over anymatch.
MIT License
178 stars 51 forks source link

About the root option #42

Closed ifduyue closed 9 years ago

ifduyue commented 9 years ago

Quoted from here

  if (opt.root && glob[0] === '/') {
    glob = path.resolve(opt.root, '.'+glob);
  } else {
    glob = path.resolve(opt.cwd, glob);
  }

It can be inferred that root takes effect only if glob starts with '/', and relative path are resolved against cwd. Why treating paths start with and not start with '/' differently? Is it better to treat them all the same? So that:

root: /a, glob: /b   =>   /a/b 
root: /a, glob: b    =>   /a/b    (not `cwd`/b)
ifduyue commented 9 years ago

Find this in https://github.com/isaacs/node-glob/blob/e3cdccc0e295c2e1d5f40cf74c73ea17a8319c5c/README.md#options

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

ifduyue commented 9 years ago

Here is a demo https://github.com/ifduyue/gulp-src-with-opts-cwd-and-root, and what I want to do:

$ tree this-is-root/
this-is-root/
├── a
├── b
└── c
    ├── d
    └── e

1 directory, 4 files

Set directory this-is-a-root as the root (like a chroot root), so that every glob, not matter whether it start with / or not, captures expected files under this-is-root.

But it doesn't work. Below is the output of running gulp:

$ gulp
[01:00:16] Using gulpfile ~/workspace/github/gulp-src-with-opts-cwd-and-root/gulpfile.js
[01:00:16] Starting 'default'...
[01:00:16] glob is [ 'a', '/b', 'c/d', '/c/e' ]
[01:00:16] Finished 'default' after 16 ms
[01:00:16] { cwd: 'this-is-root' } 'captures' [ 'a', 'd' ]
[01:00:16] { root: 'this-is-root' } 'captures' [ 'this-is-root/b',
  '../Users/d/workspace/github/gulp-src-with-opts-cwd-and-root/this-is-root/c/e' ]
[01:00:16] { cwd: 'this-is-root', root: 'this-is-root' } 'captures' [ 'this-is-root/b',
  '../Users/d/workspace/github/gulp-src-with-opts-cwd-and-root/this-is-root/c/e' ]

It is expected that all the files under this-is-root are captures by gulp.src. But the reality is no matter how I use a combination of the opts cwd and root, it only capture two files, not all four.

ifduyue commented 9 years ago

It seems both glob-stream and node-glob manipulate on globs and opt.cwd, and something happens.