11

Currently, when I run my project, it will execute my post-build commands that I have set up. However, this is only true if there was a change to the project. My ultimate goal here is to have my project run ng build each time I build it. However, what I have noticed is that if I were to change an HTML file in angular, the project does not detect any changes so it does not build again and thus it does not run my ng build command.

Is there a way to force it to always run post-build commands or maybe make it always rebuild, even if no changes are detected? Or maybe there is another way to accomplish this?

This is a .NET Core WebApp and the code to run my post build event is located inside my .csproj file

<Target Name="PostBuild" AfterTargets="PostBuildEvent">
    <Exec Command="echo Building Angular App..." />
    <Exec Command="cd ClientApp &amp;&amp; ng build" />
</Target>
Mark G
  • 2,848
  • 1
  • 24
  • 32
Bagzli
  • 6,254
  • 17
  • 80
  • 163
  • @LeoLiu-MSFT there was answer before yours, that got deleted for some reason, to use `Always true`. This did the trick for me so I kept it like this. I haven't had the chance to test your way, I'll try to do it soon and see if it works. – Bagzli Jul 12 '18 at 15:54
  • I'll be sticking with the way I have it now (see my last comment) as it works. – Bagzli Jul 20 '18 at 15:50

3 Answers3

11

Is there a way to force it to always run post-build commands or maybe make it always rebuild, even if no changes are detected?

The easiest way is set the property DisableFastUpToDateCheck to true in the project file to disable FastUpToDateCheck for Visual Studio build manager:

<PropertyGroup>
    <DisableFastUpToDateCheck>True</DisableFastUpToDateCheck>
</PropertyGroup>

Check MSDN about DisableFastUpToDateCheck:

A boolean value that applies to Visual Studio only. The Visual Studio build manager uses a process called FastUpToDateCheck to determine whether a project must be rebuilt to be up to date. This process is faster than using MSBuild to determine this. Setting the DisableFastUpToDateCheck property to true lets you bypass the Visual Studio build manager and force it to use MSBuild to determine whether the project is up to date.

Besides, if you want a way to separate build from post-build commands, you can use MSBuild command line build this project directly without adding above settings.

Hope this helps.

Leo Liu
  • 71,098
  • 10
  • 114
  • 135
  • Are you saying that MSBuild is going to detect changes in html/javascript files and will force a rebuild? – Bagzli Jul 09 '18 at 14:16
  • Yes, Visual Studio uses `FastUpToDateCheck` to improve compilation efficiency. If a file add as content (Not Compile) and we change the content in this file, Visual Studio will determine this file is up to date. So if we want to VS always run the post-build command, we just need disable the feature `FastUpToDateCheck`. – Leo Liu Jul 10 '18 at 01:28
  • This worked for me. Now my project always performs the post build event. – Nathanael Weiss Dec 10 '21 at 03:40
7

You could configure your post build event slightly differently in your .csproj file and set RunPostBuildEvent to Always as per the below:

  <PropertyGroup>
    <TargetFramework>netcoreapp2.0</TargetFramework>
    ...
    <PostBuildEvent>cd ClientApp &amp;&amp; ng build</PostBuildEvent>
    <RunPostBuildEvent>Always</RunPostBuildEvent>
  </PropertyGroup>

EDIT: As I discovered after a bit more testing, the RunPostBuildEvent does not behave as I expected it to. Therefore, a 'workaround' is to add DisableFastUpToDateCheck as per the below:

<PropertyGroup>
  <DisableFastUpToDateCheck>true</DisableFastUpToDateCheck>
  <PostBuildEvent>cd ClientApp &amp;&amp; ng build</PostBuildEvent>
</PropertyGroup>

From MSDN:

A boolean value that applies to Visual Studio only. The Visual Studio build manager uses a process called FastUpToDateCheck to determine whether a project must be rebuilt to be up to date. This process is faster than using MSBuild to determine this. Setting the DisableFastUpToDateCheck property to true lets you bypass the Visual Studio build manager and force it to use MSBuild to determine whether the project is up to date.

Clearly the downside to this is that the project will always be rebuilt, so this is not an ideal solution.

SpruceMoose
  • 9,737
  • 4
  • 39
  • 53
  • So the problem with this approach is that the starting location is different. The starting path is now inside bin\Debug\netcoreapp2.1. As such, I have to change my cd command. The new command now is `cd ../../../ClientApp && ng build` – Bagzli Jul 08 '18 at 15:59
  • However, based on my quick test, it doesn't force it to build every time. – Bagzli Jul 08 '18 at 16:01
  • It will just run the post build event each time. Build behaviour remains the same (i.e. compile if changes have been made) – SpruceMoose Jul 08 '18 at 16:04
  • I'm not sure I understand how this solves my problem? I added your command, I built it to test and it works. Then I went and changed my html in the angular and built visual studio again, and ng build was never called because visual studio did not detect any changes inside the mvc application. The ClientApp folder, where angular resides, is in the same directory as the application, yet it doesn't detect changes on front end, only if a c# class changes. – Bagzli Jul 08 '18 at 16:29
  • Sorry, you are correct, that approach doesn't work. The only solution I can see is adding `true` to the above, but that causes the project to always be built (not ideal). – SpruceMoose Jul 08 '18 at 17:30
  • 1
    Yeah, that one does work, it forces it to run ng build each time. If only there was a way to separate build from post-build commands. Can you add this as part of your answer? – Bagzli Jul 08 '18 at 21:05
  • Yeah, it isn't ideal. I have updated to reflect this. – SpruceMoose Jul 09 '18 at 08:55
4

I have solved this by adding the following properties to my project file

<RunPostBuildEvent>Always</RunPostBuildEvent> 
<DisableFastUpToDateCheck>true</DisableFastUpToDateCheck>

Somebody else had this posted as an answer but deleted for some reason so I am re-posting it so anyone that comes along in future can see how I solved it.

Bagzli
  • 6,254
  • 17
  • 80
  • 163