Search code examples
c#visual-studio-2015msbuildincremental-build

MSBuild not skipping target even though no changes were made to target inputs


I use MSBuild Version 14.0.

Following the documentation here, I defined my own Build task like this:

  <Target Name="Build" 
          Inputs="@(Compile)"
          Outputs="MyLibrary.dll">

          <Csc 
              Sources="@(Compile)"
              OutputAssembly="MyLibrary.dll"/>
  </Target>  

The idea is to reduce build time by only building incrementally -- the build task is supposed to run only when any of the files in the @(Compile) list (which is currently a collection of all .cs files in the project) is edited after the creation of the latest version of MyLibrary.dll.

Using MSBuild, I ran the following command:

msbuild MyProject.csproj /t:Build /p:Platform="AnyCPU" /fileLogger /flp:logfile=Output.log;verbosity:minimal

The first time I executed the command, everything was built from scratch, as expected.

However, on subsequent occasions when I ran the command again without having made any changes to any of my .cs files, the project was also built from scratch each time.

Why didn't MSBuild just skip the Build target even though there were no changes to any of the files included in the Inputs parameter to Target?


Solution

  • It was not working because I forgot to delete the following line from my .csproj file:

    <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
    

    So the Build task generated by Microsoft.CSharp.targets overrode the Build task that I had defined.

    Once that line was deleted, my custom Build task ran as expected.