How to fail the build from project.json scripts

587 views Asked by At

I have a ASP.NET Core webapp, that builds frontend code using webpack. Webpack is called via npm, which in turn is called via the scripts section in my project.json:

{
  ...
  "scripts": {
    "postcompile": "npm run build"
  }
}

The "npm run build" call sets the process exit code to 1 if the build fails. Nevertheless running "dotnet build" either from the command line or from Visual Studio does not fail - it prints the npm errors, but it does not fail the build.

How can I configure an additional build step like calling "npm run build" in my project.json file that will actually fail the build if the step fails?

2

There are 2 answers

4
jontem On

Make sure that you set the bail property to true in webpack config if the errors is from the webpack build

From the docs:

bail
Report the first error as a hard error instead of tolerating it.

0
Mike Eshva On

I had exactly the same problem with my CI build on our build server. There were errors during build but CI build didn't fail. I couldn't find a solution for the problem using scripts in project.json (dotnet.exe just ignores exit code of script) but found a way to solve it in .xproj file.

ASP.NET Core project currently consists of 2 project files: project.json and .xproj per a project. Both are capable of running some kind of user defined scripts. So I've incorporated my scripts into .xproj file and deleted them from project.json. The main problem was to find a working way to call them before compile of .NET code. So here is my .xproj file. Notice the target BuildManagementConsole.

The main problem is I can't build project using 'dotnet build' I have to use 'msbuild .xproj' command line. In my opinion this solution is better than incorporating scripts into project.json because Microsoft will remove it in the next version of ASP.NET Core.

<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <PropertyGroup>
    <VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">14.0</VisualStudioVersion>
    <VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath>
    <TypeScriptCompileBlocked>true</TypeScriptCompileBlocked>
  </PropertyGroup>
  <Import Project="$(VSToolsPath)\DotNet\Microsoft.DotNet.Props" Condition="'$(VSToolsPath)' != ''" />
  <PropertyGroup Label="Globals">
    <ProjectGuid>d5feb23f-aaaa-4852-89c2-e1eef6ab52a8</ProjectGuid>
    <RootNamespace>ManagementConsole</RootNamespace>
    <BaseIntermediateOutputPath Condition="'$(BaseIntermediateOutputPath)'=='' ">.\obj\</BaseIntermediateOutputPath>
    <OutputPath Condition="'$(OutputPath)'=='' ">.\bin\</OutputPath>
    <TargetFrameworkVersion>v4.6.1</TargetFrameworkVersion>
    <SccProjectName>SAK</SccProjectName>
    <SccProvider>SAK</SccProvider>
    <SccAuxPath>SAK</SccAuxPath>
    <SccLocalPath>SAK</SccLocalPath>
  </PropertyGroup>
  <PropertyGroup>
    <SchemaVersion>2.0</SchemaVersion>
  </PropertyGroup>
  <ItemGroup>
    <DnxInvisibleContent Include=".bootstraprc" />
    <DnxInvisibleContent Include=".npmrc" />
    <DnxInvisibleContent Include="npm-debug.log" />
  </ItemGroup>
  <ItemGroup>
    <DnxInvisibleFolder Include=".nuget\" />
  </ItemGroup>
  <Target Name="BuildManagementConsole" BeforeTargets="Build">
    <Exec Command="dotnet restore" />
    <Exec Command="npm cache clean --force" />
    <Exec Command="npm install" />
    <Exec Command="npm run build:$(Configuration)" />
  </Target>
  <Import Project="$(VSToolsPath)\DotNet.Web\Microsoft.DotNet.Web.targets" Condition="'$(VSToolsPath)' != ''" />
</Project>