Bug 18761 - AfterTargets and AfterBulid ignored by xbuild
Summary: AfterTargets and AfterBulid ignored by xbuild
Alias: None
Product: Tools
Classification: Mono
Component: xbuild ()
Version: 3.4.0
Hardware: Macintosh Mac OS
: --- normal
Target Milestone: ---
Assignee: Bugzilla
Depends on:
Reported: 2014-04-02 15:05 UTC by Anton Tayanovskyy
Modified: 2014-06-27 15:37 UTC (History)
2 users (show)

Is this bug a regression?: ---
Last known good build:

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.

Please create a new report on GitHub or Developer Community with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:

Description Anton Tayanovskyy 2014-04-02 15:05:08 UTC
To reproduce: create an empty ASP.NET application, say:


Edit .csproj to add a new target:

<Target Name="Complain" AfterTargets="Build">
  <Warning Text="Complaining.." />


1. When doing `xbuild Test.sln` the target does NOT run either
2. When doing `xbuild Test.csproj` the target does run
3. In XamarinStudio/MonoDevelop, the target does NOT run (no warnings)

MonoDevelop: 4.2.2
Mono: 3.2.8
xbuild: 12.0

Behavior is similar for:

<Target Name="AfterBuild">
  <Warning Text="Complaining.." />

When using a simple C# Library project, the XS/MD starts running these targets, but `xbuild Test.sln` still does not.
Comment 1 Anton Tayanovskyy 2014-06-10 09:55:21 UTC
It got worse. I think the problem affects all projects now, not necessarily web applications. Current environment:

Mac OS X
Xamarin Studio: 5.0
Mono: 3.4.0
xbuild: 12.0


1. Create "C# library" project in Xamarin Studio

2. Edit .csproj to include:

<Target Name="Complain" AfterTargets="Build">
  <Warning Text="Complaining.." />

3. xbuild CSharpLib.fsproj ==> works (Warning: Complaining..)

4. xbuild CSharpLib.sln ==> does not work.

Building the solution obviously follows some different code path than building constituent projects. 

Please consider fixing. This breaks various CI scenarios.

For us this is currently is a big annoyance with WebSharper which relies on custom targets during build process.
Comment 2 Jonathan Pryor 2014-06-26 16:06:35 UTC
> Edit .csproj to add a new target:

Where are you adding the new target? Targets may be "overridden": http://stackoverflow.com/a/861321/83444

So what happens with the AfterBuild target is that e.g. Microsoft.Common.targets has:

	<Target Name="AfterBuild"/>

Regarding Comment #0, There are two ways this can go:

1. Your overriding target is before the <Import/>:

  <Target Name="AfterBuild">
     <!-- custom steps here -->
  <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />

In this case, due to overriding the AfterBuild declared in Microsoft.Common.targets will come "textually" _after_ the one declared above, and will be the "controlling" definition. Consequently, "custom steps here" will NOT be executed during the build.

2. Your overriding target is after the <Import/>:

  <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
  <Target Name="AfterBuild">
     <!-- custom steps here -->

In this case, the above target is overriding the AfterBuild declared in Microsoft.Common.targets, and comes "textually" after the one in Microsoft.Common.targets. Consequently, "custom steps here" SHOULD execute (barring some other bug).

In relation to all <Import/>s in your code, where is your AfterBuild target being declared?
Comment 3 Mikayla Hutchinson [MSFT] 2014-06-27 12:19:04 UTC
I can confirm that the AfterTargets is ignored when building a sln with xbuild.

Everything else works as expected - building a csproj with xbuild, building projects and solutions in Xamarin Studio, overriding the AfterBuild target, etc.

Note also that you can redefine the BuildDependsOn property to inject targets into the build, so this is easy to work around.
Comment 4 Mikayla Hutchinson [MSFT] 2014-06-27 13:27:16 UTC
Interestingly, if I generate a project file from the solution:

    XBUILD_EMIT_SOLUTION=1 xbuild Foo.sln

Then build that

    xbuild Foo.sln.csproj

it works fine.

Which is odd, because that's exactly what building the solution is supposed to be doing.
Comment 5 Mikayla Hutchinson [MSFT] 2014-06-27 14:04:57 UTC
Here is the difference between building the sln and building the .sln.proj (with some s/\.proj// naming fixup to simplify the diff):

Comment 6 Mikayla Hutchinson [MSFT] 2014-06-27 14:32:37 UTC
Fixed in https://github.com/mono/mono/commit/8afba772b2c2e919ca2a913388b8da916462ed15
Comment 7 Anton Tayanovskyy 2014-06-27 15:37:03 UTC