-
Notifications
You must be signed in to change notification settings - Fork 130
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
crash the build process on error #295
Comments
Most users don't want this, since it would break their watch task. However, you can use something like this to exit with a specific code: ...
.pipe(ts(tsProject))
.on('error', function() { process.exit(1) }) |
I was hoping this would be a flag or something. This is a very common use case as it makes no sense to ignore errors in production builds/CI environment. |
I think that's more suitable to be implemented directly in gulp instead of gulp-typescript. Can you report it there? Other plugins could also benefit from this. |
This might be already implemented in gulp 4. |
AFAIK gulp (current version) stops the task and crashes when an error occurs. This is the default behaviour and it was very annoying in watch tasks, but it's what should happen in production builds. for some reason |
I added the following to my let failed = false;
...
.pipe(ts(tsProject))
.on("error", function () { failed = true; })
.on("finish", function () { failed && process.exit(1); }); In my opinion this should not be integrated directly into |
simplified .once("error", function () {
this.once("finish", () => process.exit(1));
}) |
@falsandtru does this simplified solution occur after ALL type errors have been listed? When I tried something similar it aborted on the very first type error. |
I got all errors before exit with errors.
|
@falsandtru awesome, thanks! |
The fix on package consumer side is really concise, which I'm happy about, but I will say that a lot of other linters and gulp plugins will come with an optional way to fail the task if "error conditions" were detected (like warnings / errors in a linter), which can then be opted into as the task implementer sees fit in their CI builds. |
+1 please add a way to integrate this directly in the plugin. I've made the fix suggested by @falsandtru in my projects but I would prefer to use a simple flag in the configuration of the gulp task. Thanks! |
gulp-jshint has a pretty good solution to this: a 'fail' reporter
|
I'm just adding that I think developers would agree that while "breaking watch" is annoying, breaking production deploys because broken build steps "succeed" is a worse default behavior! |
+1 |
@danielmoore please use Github's "thumbs up" reaction feature instead of "+1" comments. Reactions are designed to be a better alternative for expressing support without the visual clutter and additional email notifications that "+1" comments bring. |
Here is my solution. It works fine for me, and it don't break watch! Use async task because I want to end this task conditionally. I put error handler on tsResult to count the number of errors and finish handler after dest pipe to delete created files if there is any error and send a error to the callback to break gulp tasks, otherwise I call the callback without any argument to continue remaining tasks. const gulp = require("gulp");
const del = require("del");
const ts = require("gulp-typescript");
gulp.task('compile', function (cb) {
let errorCount = 0;
const tsProject = ts.createProject("./tsconfig.json");
const tsResult = gulp
.src("./src/**/*.ts")
.pipe(tsProject())
.on("error", () => {
errorCount += 1;
});
tsResult.js
.pipe(gulp.dest("./build"))
.on("finish", () => {
if (errorCount > 0) {
let error = new Error(`Failed to compile: ${errorCount} error(s).`);
error['showStack'] = false;
del("./build/**").then(() => {
cb(error);
});
} else {
cb();
}
});
return gulp.src('src/**/*.ts')
.pipe(ts({
noImplicitAny: true,
out: 'output.js'
}))
.pipe(gulp.dest('built/local'));
}); |
I'm using gulp-typescript in a node-server project, so every change runs a gulp-nodemon task. However an error in the typescript task still allows the nodemon task to run. The solution posted above by @tsctao is the only solution I could find that worked. It seems to me like the conversation above is focused on exiting or not-exiting with an error exit-code, but in the nodemon case I don't want the process to quit, just for it to wait until a non-error result from typescript. Is there a cleaner workaround? EDIT: never mind, I switched out nodemon for this solution and it solved all my problems. |
We recently had to deal with this issue as well. We chose to invoke a callback function of the task with an error message. This would look something like this:
|
Hi @ivogabe If you don't want to break the watch task, then use this package (gulp-plumber), but please don't do it by default. |
@SimonNodel-AI Thank you for that. It works with node-mon as well. |
I still don't want to break this, especially given that the philosophy of TypeScript is that all errors are warnings, and there are no fatal errors. Though to make easier to crash the build, I'd suggest to add a new reporter (see readme) which will crash in case of errors. That way there is less code needed to configure this. Do you agree on this solution? |
cc @phated in case he as any points to add on how best to support failing builds while working with watch tasks. |
@ivogabe I think you should reconsider breaking this for watch tasks. The default invocation of |
I believe gulp 4 no longer crashes if a watch task errors. We landed a fix for it at gulpjs/glob-watcher@ad96e3f (glob-watcher 5.0.1) so you should properly emit errors from your streams. |
@phated Great, I'll take a look at that! |
The crashing behavior will be part of our release 5 🎉! Thanks for everyones opinions, I tested the behavior of gulp 4 and the watch scenario works there. The changes with the current behavior are as follows:
To prevent crashing the build, even outside of watch mode, you can add You can install the new version with |
Expected behavior:
When a typescript error is detected, gulp shouldn't exit with code 0, an error should be reported instead.
Actual behavior:
errors are displayed but gulp exists with code 0.
Your gulpfile:
Include your gulpfile, or only the related task (with
ts.createProject
).tsconfig.json
Include your tsconfig, if related to this issue.
The text was updated successfully, but these errors were encountered: