3

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?

Schweder
  • 1,464
  • 2
  • 13
  • 19

2 Answers2

0

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>
Mike Eshva
  • 1,078
  • 15
  • 17
-1

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.

jontem
  • 4,101
  • 1
  • 21
  • 17
  • I am using the https://www.npmjs.com/package/webpack-fail-plugin - I thought this should be enough. Anyway I also added the --bail option, without success. As I said, the process exit code already is set to 1 after the webpack build, but still dotnet build does not fail. Did I set the bail option correctly? It looks like in my package.json: "build": "webpack --config builds/webpack.prod.config.js --bail", – Schweder Dec 21 '16 at 16:35
  • Yes that should be enough for webpack. But i don't know why `dotnet build` isn't picking it up – jontem Dec 21 '16 at 16:42
  • Me neither. That's why I was creating this question :-) Could it be the "npm run build" should no be called from "postcompile" but from some different script entry point? – Schweder Dec 21 '16 at 17:24
  • It's mentioned by the OP that problem isn't in npm exit code but in how dotnet.exe handle errors in it's scripts (precompile, postcompile etc.) – Mike Eshva Feb 01 '17 at 12:29