gulp icon indicating copy to clipboard operation
gulp copied to clipboard

Is gulp.src too strict about input globs? (gulp 4)

Open vbud opened this issue 11 years ago • 44 comments

In #712, a decision was made to allow an empty array to be passed into gulp.src based on the case made by @wesleytodd. That decision was reverted recently in gulp 4 in is-valid-glob, which is used by vinyl-fs.

Elsewhere, another vinyl-fs dependency, glob-stream, is now also enforcing that individual file references in glob arrays or in singular globs must exist, otherwise a 'File not found with singular glob' error is thrown.

Both of these changes make gulp.src stricter and harder to use. I will sometimes hand arrays to gulp.src that can be empty in some circumstances, or an array of globs, some of which do not match any files (but may in the future).

I started some discussions in a vinyl-fs issue stating the problems I have encountered with the new behavior: https://github.com/gulpjs/vinyl-fs/issues/40#issuecomment-144231884 https://github.com/gulpjs/vinyl-fs/issues/40#issuecomment-144470676 https://github.com/gulpjs/vinyl-fs/issues/40#issuecomment-144572864

It seems there are some anti-patterns in gulpfiles that were the cause of these changes, as @contra mentioned in: https://github.com/gulpjs/vinyl-fs/issues/40#issuecomment-144316357

I would like to make the case for returning to the more forgiving gulp.src. This would require behavior changes to is-valid-glob and glob-stream.

Here's some examples of the added complexity required now:

// this
function moveVendorCssFiles() {
    return gulp.src(mainBowerFiles({filter: '**/*.css'}), {base: paths.bowerComponents})
    ...
}
// becomes
function moveVendorCssFiles(cb) {
    var bowerCss = mainBowerFiles({filter: '**/*.css'});
    if(bowerCss.length === 0) {
        cb();
        return;
    }
    return gulp.src(bowerCss, {base: paths.bowerComponents})
    ...
}
// this
gulp.src([
    'app/*.module.js',
    'app/**/*.js',
    '.tmp/index.js'
])
...
// becomes
gulp.src(globby.sync([
    'app/*.module.js',
    'app/**/*.js',
    '.tmp/index.js'
]), {base: 'app'})
...

Commence discussion!

vbud avatar Oct 01 '15 00:10 vbud

I'm curious about your use case, why aren't you having gulp.src resolve the globs?

yocontra avatar Oct 01 '15 00:10 yocontra

Edit: wrong points removed

phated avatar Oct 01 '15 01:10 phated

@phated allowEmpty is for ignoring the error when a file is not found in the stream, not for ignoring the error on invalid glob

yocontra avatar Oct 01 '15 01:10 yocontra

Oops, I was thinking the wrong thing, removed.

phated avatar Oct 01 '15 02:10 phated

@contra somehow it became commonplace (I think through some tutorial series) for people to not specify their globs, but instead rely on that main-bower-files module to build their array of paths (it doesn't actually have any globs, just paths). I don't see the appeal to this, but I've done away with bower so very long ago.

phated avatar Oct 01 '15 02:10 phated

@contra if I allow gulp.src to resolve the globs, I get an error from glob-stream. In this particular case, .tmp/index.js does not always exist. If I could configure gulp.src to use glob-stream's allowEmpty (can I do that?) that may solve that part of the problem.

@phated regarding mainBowerFiles, that's just an example of a case where something else is used to resolve globs into an array of files, then the resulting array of files is handed to gulp. Don't get hung up on bower - it could just as easily be globby or some other tool handing gulp.src an array of files.

To shed more light on the globby use case - we have a lazy-loading setup that manages several individual modules. So lots of non-gulp code runs to handle determining which files make up each module, and then those files are handed to gulp. At that point, we no longer have globs; we have arrays of already-resolved files. In some cases, these arrays can be empty.

Keep in mind also the broader use case of application generators, where some operations are stubbed out for handling files that could exist in a project, but don't necessarily exist at the beginning of a project.

Hopefully that helps you guys understand my use cases a bit better.

vbud avatar Oct 01 '15 05:10 vbud

I thought about this more last night.

The 2 reasons this was added were:

  1. To avoid the gulp.src([]).pipe(exec()) usage of vinyl streams
  2. To help users debug why a pipeline wasn't processing anything (since it would throw telling them it is an invalid glob)

What if we just add a validateGlob option that defaults to true and use that option as part of the isValidGlob conditional?

phated avatar Oct 01 '15 20:10 phated

@phated too many options imo

yocontra avatar Oct 01 '15 21:10 yocontra

The validateGloboption would work.

For 2, I usually use gulp-debug to see what is in the pipeline. Is there a reason you guys don't recommend that?

vbud avatar Oct 01 '15 21:10 vbud

We should just revert that commit of invalidating the array glob since it had unintended consequences.

Solution will happen at https://github.com/jonschlinkert/is-valid-glob/issues/3 - thanks for clarifying!

yocontra avatar Oct 01 '15 22:10 yocontra

I think that we should still keep the logic of checking for an empty array in vinyl-fs but just log a warning. This is an important debugging tool

phated avatar Oct 01 '15 23:10 phated

I am going to reopen this because we've only had feedback from 2 people so far and I'd like to hear from others.

phated avatar Oct 01 '15 23:10 phated

Okey, just digging around of these issues and read all comments.

I can't realize what and why you (@vbud and the others against this change) expect something to happen when there's no such actual file on the file system? And actually, why even you would run gulp "on nothing". I can't accept their state. I understand the case, but can't accept it, it's pointless for me.

There's no logic in the https://github.com/vbud/gulp-testing and the examples on the first post of this issue. It's total overkill to pass nothing (empty thing-ish a.k.a empty array) to gulp and do nothing. And it's more overkill and more error prone than just writing simple if statement and executes the cb. There's no logic to run all this hard and slow mechanisms of checks, foreachs, globbing, concat/merge/combine streams, passthroughs and etc stuffs - i mean one word - gulp. It will take 100x more time and more possibilities of errors than just one "if" which only job will be to just check some case (e.g. checking is empty array, then call cb) It's also overkill to use globby (and even sync calling it.. nah) and then to pass the paths to gulp.src

I'm absolutely agreed with https://github.com/gulpjs/vinyl-fs/issues/40#issuecomment-144316357. There are a tons of tutorials and cases which use gulp for everything, instead of just use "normal js code to do it". They make their lifes harder.

If someone is lazy or don't know sometimes how to trick some things exactly, it isn't problem of gulp, or google or whatever. Talking generally. And as you can see there's always have some workaround for everything. It's not "harder, stricter, or trickier", it's better or at least - correct. It's a lot better to do it with if statement instead of running something which will do nothing, i mean.. absolute no logic. Most of the times it's better to be trickier instead of be "x, y,z -way", or "simple/easy".

That's why I love strictness and use it everywhere - allows me to do more than crazy things and to have a lot more success and lower issues.

Cheers.

tunnckoCore avatar Oct 02 '15 00:10 tunnckoCore

I'm not sure if this helps or hinders, but node-glob itself will throw an error if an empty string is passed.

jonschlinkert avatar Oct 02 '15 03:10 jonschlinkert

@phated logging a warning sounds like a reasonable balance to me - it helps debugging while allowing the behavior.

vbud avatar Oct 02 '15 07:10 vbud

To me, this situation is really similar to running a map operation on an empty array - if there is nothing in the source array, there is nothing in the output array. The map operation does not throw an error because the array is empty.

In the same way, if I hand gulp.src nothing, it should do nothing, but it shouldn't throw an error. Is this a reasonable way of looking at the situation or am I missing something?

@tunnckoCore It seems like we have a philosophical difference :). But more importantly I think you are completely mis-representing my position. The gulp-testing repo was for testing current gulp 4.0 behavior, but it is not an example of the kind of thing I want to do. I never want to hand a file to gulp that will never exist - the whole point of my examples is that the file could or will exist, but does not always. The examples I gave at the beginning of this issue are more representative of what I'm trying to do, so please react to those instead.

Also, please don't get hung up on globby.sync or mainBowerFiles or whatever - they are just examples of some other code resolving files and then handing to gulp. And globby is what I am having to use after these changes were made to pre-resolve files since gulp's behavior is now stricter.

To @tunnckoCore's last point about strictness - I'm all for strictness when it is preventing dangerous outcomes. Do you have examples of where this particular strictness in gulp has helped you?

vbud avatar Oct 02 '15 07:10 vbud

What's the harm in just passing the allowEmpty option when you think the file might not exist?

callumacrae avatar Oct 02 '15 08:10 callumacrae

I have been thinking about this more and more and the way I see it (especially in light of @jonschlinkert's comment) is that an invalid glob is something that will never match a file; e.g. an empty string, an empty array, an array of empty strings, etc. I think we should continue to throw on invalid globs because there is no reason to take on the overhead of building a pipeline if there will never be any files in it. And to @vbud's point about map, you wouldn't give a map function a number, so why give a pipeline no files? That example is a little absurd but a JS map is much lighter weight than constructing an entire stream pipeline.

gulp 4.0 is all about function composition so I think we should defer this use case to userland. Something like the following:

var gulp = require('gulp');
var isValidGlob = require('is-valid-glob');
var mainBowerFiles = require('main-bower-files');

// this would become a module
function ifGlob(paths, task){
  return function(cb){
    if(isValidGlob(paths)){
      return task(paths, cb);
    } else {
      cb();
    }
  }
}

gulp.task('build', ifGlob(mainBowerFiles(), function(paths, cb){
  return gulp.src(paths)
    .pipe(gulp.dest('./out/');
});

Also, note that we are throwing inside a task, so with gulp 4.0, you can use the --continue flag to continue upon errors inside tasks.

phated avatar Oct 02 '15 18:10 phated

@vbud i can agree with your first paragraph, but also support the @phated question and state about it

you wouldn't give a map function a number, so why give a pipeline no files? That example is a little absurd but a JS map is much lighter weight than constructing an entire stream pipeline.

I understand that "the files could or will exist", okey, but so .. you know in what case (when) they exist, so it's enough also your code to know, so perfect case for ifs and workarounds. In js specially you can do one thing in ton of different ways, respect that.

It seems that these cases can be tricked and can be as @phated say "in userland", it's not so hard.

Do you have examples of where this particular strictness in gulp has helped you?

I don't use it and won't use it until we write better file globbing library than node-glob. But I actively following it and using ideas and libraries behind it. Okey, I have one-two big projects with gulp. But yea, core behind gulp4 is awesome and thanks to @phated.

@jonschlinkert: I'm not sure if this helps or hinders, but node-glob itself will throw an error if an empty string is passed.

:+1: cool, this is the way we should move. :) one more reason why this is the correct way.

tunnckoCore avatar Oct 02 '15 22:10 tunnckoCore

@phated when you say:

That example is a little absurd but a JS map is much lighter weight than constructing an entire stream pipeline.

Could you educate me a bit more here? What all happens behind the scenes when a stream pipeline is constructed, and what happens when it is run on an empty array?

vbud avatar Oct 05 '15 00:10 vbud