panuhorsmalahti / gulp-tslint

TypeScript linter plugin for Gulp
MIT License
118 stars 44 forks source link

sample task results with: stream.js:94 throw er; // Unhandled stream error in pipe. #19

Closed omencat closed 9 years ago

omencat commented 9 years ago

I can run tslint on the same file OK, but using the sample gulp-tslint task results with: stream.js:94 throw er; // Unhandled stream error in pipe.

gulp.task('tslint2', () => {
    var linter = gulp.src(['gulpfile.ts'])
        .pipe(tslint())
        .pipe(tslint.report('prose'));
});

console:

C:\proj\GitHub\test>gulp tslint2
[21:44:07] Using gulpfile ~\GitHub\test\gulpfile.js
[21:44:07] Starting 'tslint2'...
[21:44:07] Finished 'tslint2' after 5.32 ms
[21:44:07] [gulp-tslint] error gulpfile.ts[42, 15]: comment must start with a sp
ace
[21:44:07] [gulp-tslint] error gulpfile.ts[48, 4]: file should end with a newlin
e
[21:44:07] [gulp-tslint] error gulpfile.ts[6, 8]: unused variable: 'concat'
[21:44:07] [gulp-tslint] error gulpfile.ts[16, 5]: unused variable: 'testReporte
r'
[21:44:07] [gulp-tslint] error gulpfile.ts[23, 9]: unused variable: 'linter'
[21:44:07] [gulp-tslint] error gulpfile.ts[4, 23]: ' should be "
[21:44:07] [gulp-tslint] error gulpfile.ts[5, 29]: ' should be "
[21:44:07] [gulp-tslint] error gulpfile.ts[6, 25]: ' should be "
[21:44:07] [gulp-tslint] error gulpfile.ts[7, 29]: ' should be "
[21:44:07] [gulp-tslint] error gulpfile.ts[8, 25]: ' should be "
[21:44:07] [gulp-tslint] error gulpfile.ts[22, 11]: ' should be "
[21:44:07] [gulp-tslint] error gulpfile.ts[23, 28]: ' should be "
[21:44:07] [gulp-tslint] error gulpfile.ts[25, 29]: ' should be "
[21:44:07] [gulp-tslint] error gulpfile.ts[29, 11]: ' should be "
[21:44:07] [gulp-tslint] error gulpfile.ts[30, 30]: ' should be "
[21:44:07] [gulp-tslint] error gulpfile.ts[37, 21]: ' should be "
[21:44:07] [gulp-tslint] error gulpfile.ts[38, 21]: ' should be "
[21:44:07] [gulp-tslint] error gulpfile.ts[43, 36]: ' should be "
[21:44:07] [gulp-tslint] error gulpfile.ts[44, 29]: ' should be "
[21:44:07] [gulp-tslint] error gulpfile.ts[16, 17]: expected variable-declaratio
n: 'testReporter' to have a typedef
[21:44:07] [gulp-tslint] error gulpfile.ts[16, 28]: expected parameter: 'output'
 to have a typedef
[21:44:07] [gulp-tslint] error gulpfile.ts[16, 34]: expected parameter: 'file' t
o have a typedef
[21:44:07] [gulp-tslint] error gulpfile.ts[16, 43]: expected parameter: 'options
' to have a typedef
[21:44:07] [gulp-tslint] error gulpfile.ts[23, 15]: expected variable-declaratio
n: 'linter' to have a typedef
[21:44:07] [gulp-tslint] error gulpfile.ts[30, 17]: expected variable-declaratio
n: 'tsResult' to have a typedef
[21:44:07] [gulp-tslint] error gulpfile.ts[12, 1]: missing 'use strict'

stream.js:94
      throw er; // Unhandled stream error in pipe.
            ^
Error: Failed to lint: gulpfile.ts[42, 15]: comment must start with a space, gul
pfile.ts[48, 4]: file should end with a newline, gulpfile.ts[6, 8]: unused varia
ble: 'concat', gulpfile.ts[16, 5]: unused variable: 'testReporter', gulpfile.ts[
23, 9]: unused variable: 'linter', gulpfile.ts[4, 23]: ' should be ", gulpfile.t
s[5, 29]: ' should be ", gulpfile.ts[6, 25]: ' should be ", gulpfile.ts[7, 29]:
' should be ", gulpfile.ts[8, 25]: ' should be ", gulpfile.ts[22, 11]: ' should
be ", gulpfile.ts[23, 28]: ' should be ", gulpfile.ts[25, 29]: ' should be ", gu
lpfile.ts[29, 11]: ' should be ", gulpfile.ts[30, 30]: ' should be ", gulpfile.t
s[37, 21]: ' should be ", gulpfile.ts[38, 21]: ' should be ", gulpfile.ts[43, 36
]: ' should be ", gulpfile.ts[44, 29]: ' should be ", gulpfile.ts[16, 17]: expec
ted variable-declaration: 'testReporter' to have a typedef, gulpfile.ts[16, 28]:
 expected parameter: 'output' to have a typedef, gulpfile.ts[16, 34]: expected p
arameter: 'file' to have a typedef, gulpfile.ts[16, 43]: expected parameter: 'op
tions' to have a typedef, gulpfile.ts[23, 15]: expected variable-declaration: 'l
inter' to have a typedef, gulpfile.ts[30, 17]: expected variable-declaration: 't
sResult' to have a typedef, gulpfile.ts[12, 1]: missing 'use strict'.
panuhorsmalahti commented 9 years ago

I believe that's expected behaviour? If you don't want to gulp-tslint to throw an error, you can set the emitError option to false.

omencat commented 9 years ago

The message in question is

stream.js:94
      throw er; // Unhandled stream error in pipe.
            ^

This doesn't display when I run tslint.

panuhorsmalahti commented 9 years ago

Yes, that is by design.

panuhorsmalahti commented 9 years ago

Closing as won't fix.

pixelshaded commented 9 years ago

The gulp-tslint docs say it will throw a plugin error (from https://github.com/gulpjs/gulp-util). An example from the docs:

[gulp] Error in plugin 'gulp-tslint': Failed to lint: invalid.ts

But the error is occuring in stream.js and the message doesn't even look like a plugin error (aka it does not include the module name etc).

pixelshaded commented 9 years ago

You can listen for the error event in the stream, but if you throw it you get the same message. So it seems intentional. It would just be nice if it could be more clear that its intentional.

omencat commented 9 years ago

Thanks for the clarification.

pixelshaded commented 9 years ago

And if you would like even more clarification, pipe errors should actually be passed up to gulp. If you return a stream in your task function, gulp should be able to handle the on error event. If you are using the callback parameter, you should pass any errors to it. If you are using a promise, make sure to reject it with your error. Then when you look in the console, you'll get a message more like this:

[13:52:44] Starting 'tslint'...
[13:52:44] [gulp-tslint] error (no-unused-variable) app.ts[21, 7]: unused variable: 'test'
[13:52:44] [gulp-tslint] error (typedef) app.ts[21, 11]: expected variable-declaration: 'test' to have a typedef
[13:52:44] 'tslint' errored after 168 ms
[13:52:44] Error in plugin 'gulp-tslint'
Message:
    Failed to lint: app.ts[21, 7]: unused variable: 'test', app.ts[21, 11]: expected variable-declaration: 'test' to have a typedef.

Notice in your example that gulp never terminated the task with an output something like: [13:52:44] 'tslint' errored after 168 ms

This means that you aren't passing any stream errors up to gulp, meaning that the task ends on the error being thrown rather than gulp handling it. You should be able to fix this by simply returning the stream in your function.

pixelshaded commented 9 years ago

There is nothing to fix in gulp-tslint. This can be closed. The issue was related to improper error handling in the user created task. They needed to return the stream in the task function.

panuhorsmalahti commented 9 years ago

Yeah, it seems to be a best practice to return the stream, as gulp-tslint is asynchronous ( from https://github.com/gulpjs/gulp/blob/master/docs/recipes/running-tasks-in-series.md ), as it's used as a hint for task completion.

If the gulp does returns the stream, the error message is "nice" and handled by gulp. I updated the readme and the test files.

panuhorsmalahti commented 9 years ago

Closed after updating the examples, thanks for your help :).

pixelshaded commented 9 years ago

Well best practice is to do one of three things: return a stream, return a promise, or use the callback. Returning a stream is the only one where gulp will handle errors (using the error event from the stream). The other two require that you listen for errors on your streams and pass the error to your promise rejection method or the task callback.

danpetitt commented 9 years ago

I am getting an error just using your example code in the readme:

gulp.task('tslint', function () {
    return gulp.src('scripts/app/main.ts')
      .pipe(tslint())
      .pipe(tslint.report('verbose', {
        emitError: false
      }));
});

I have tried with or without the emitError option, just nothing i do works. I dont get any lint errors just parsing error. This is with a file which has one comment in it, or indeed empty, it just always crashes out no matter what i do with the following:

stream.js:94
      throw er; // Unhandled stream error in pipe.
            ^
SyntaxError: Unexpected token 
    at Object.parse (native)
pixelshaded commented 9 years ago

I think we need more information. Can you show us the content of main.ts? Also, are you getting any type of stack trace?

panuhorsmalahti commented 9 years ago

Does tslint.json exist? Does it's contents pass validation ( http://jsonlint.com/ )?

danpetitt commented 9 years ago

Apologies for this, I tried going right back to trying tslint which was also failing. It transpires that tslint cannot cope with a tslint.json file which is encoded as UTF8 with a BOM. That's what the unexpected token was, it was the BOM characters.

Much apologies for posting this as an issue for you when it wasn't an issue for your project in the end.

panuhorsmalahti commented 9 years ago

I created a new feature task for reporting a better error for this case.

KerryRitter commented 8 years ago

I also ran into this problem. Using Visual Studio, I created tslint.json, which gave it UTF8 encoding. Changing to ANSI resolved the error.