AfterTargets failures don't fail msbuild run

Viewed 505

In my solution, some project have tasks that need to run at the end, such as copy files to various places. We implement that with AfterTargets="Build":

<Target Name="CopyStuff" AfterTargets="Build">
    <Copy SourceFiles="..." DestinationFolder="..." />
</Target>

If works. However, when building the solution (not the individual project!), if the copy fails, we get a red build warning, but msbuild (and therefore TFS build) succeeds:

> msbuild /t:clean;build my.sln
(...)
(in red...) error MSB3021: Unable to copy file (...)

> echo %errorlevel%
0     <<<<<<< This means succeeded

To my understanding, that's because msbuild thinks that as long as the major "Build" target passed, everything passed too.

Our workaround - Change target to BeforeTargets="AfterBuild", which puts my target inside the Build target. However, this requires knowledge of the content of "Build" target, and may not work for other project types.

Question:

  1. Is there a way to get AfterTargets="Build" failures to fail solution builds?
  2. If not, is there a way to automatically validates that people didn't add AfterTargets="Build" into their projects?
1 Answers
Related