Search code examples
visual-studiogulptask-runner-explorer

Cancel Build if Task Runner Explorer Task Fails


I am using visual studio task runner (2015) to run a Gulp task bound to before build.

I have set it up so that when the gulp tasks fails it sends exit code 1 and at the end it says "Process terminated with code 1." however the build continues.

This will cancel the build in team city so seems an issue linked Task Runner inside visual studio.

How can I prevent the build from taking place if it exits with a code other than 0?


Solution

  • You are correct in that this seems to be a Task Runner issue. The task runner does not communicate with MSBuild to stop a build if a BeforeBuild task fails.

    To get around this, you can run your Gulp task via the project's pre-build event instead of via the Task Runner bindings.

    Set the pre-build event

    For class libraries, you can access Build Events by right-clicking your project and selecting Properties -> Compile -> Build Events....

    For web projects, they are located at Properties -> Build Events.

    Here is the command I used to call the Gulp task in pre-build event, which will prevent the MSBuild from running if it exits with a failure:

    gulp -b $(ProjectDir) --gulpfile $(ProjectDir)gulpfile.js my-task

    This command calls Gulp passing absolute paths for working directory and gulpfile.js.

    Notes:

    • I found all kinds of context and working directory issues trying to use a more straight up command like gulp my-task.
    • $(ProjectDir) is one of the Macros for Build Commands.
    • It is assumed that Gulp is installed globally: npm install -g gulp. See jonas.ninja's answer for how to build this install into the command (or for an alternative that does not require the global dependency).