Bug 26802 - Before build command is not executed when using xbuild from the command line
Summary: Before build command is not executed when using xbuild from the command line
Alias: None
Product: Runtime
Classification: Mono
Component: Tools ()
Version: unspecified
Hardware: PC Mac OS
: --- normal
Target Milestone: ---
Assignee: Bugzilla
Depends on:
Reported: 2015-02-06 07:29 UTC by John Miller [MSFT]
Modified: 2018-02-11 04:59 UTC (History)
9 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 John Miller [MSFT] 2015-02-06 07:29:21 UTC

   A project configuration has a before build command to run a script which copies some files around. This does not appear to be executed anymore when building from the command line with xbuild (switch from mdtool because of Unified API). 

The command being ran is:

> xbuild /v:diagnostic '/property:Configuration=PROD Ad-Hoc' MyProject.iOS.sln

The logs show:

>CustomCommands = <CustomCommands xmlns="http://schemas.microsoft.com/developer/msbuild/2003"><Command type="Build" command="/bin/bash ./setup.sh Release PROD"/></CustomCommands>

That is the script that is supposed to run, but it does not seem to actually be execute. 

**Steps to Reproduce:**

   Add a before build command to a configuration to run a script (iOS)
   Use xbuild from the command line to build the project

**Actual Results:**

   The script is not executed

**Expected Results:**

   The script should be executed

**Additional Information:**

   Originally, mdtool was being used:

> mdtool' build '--configuration:PROD Ad-Hoc|iPhone’ MyProject.iOS.sln

   After switching to Unified, xbuild was needed, so this was tried

> xbuild /v:diagnostic '/property:Configuration=PROD Ad-Hoc|iPhone’ MyProject.iOS.sln

This failed and resulted in this error:

> /Volumes/DataDrive/username/MyProjectSolution.iOS/Apps/Projects/iOS/MyProject.iOS.sln: error : Invalid solution configuration and platform: "PROD Ad-Hoc|iPhone|iPhone".

It seems to add an additional "|iPhone"
Comment 1 Neil Pepper 2015-02-06 08:28:23 UTC
=== Xamarin Studio ===

Version 5.7 (build 661)
Installation UUID: 0d980f6f-57aa-44e5-8995-12c9e34764af
	Mono 3.12.0 ((detached/a813491)
	GTK+ 2.24.23 (Raleigh theme)

	Package version: 312000068

=== Apple Developer Tools ===

Xcode 6.1.1 (6611)
Build 6A2008a

=== Xamarin.iOS ===

Version: (Enterprise Edition)
Hash: dfb682f
Build date: 2015-01-08 13:39:32-0500

=== Xamarin.Android ===

Android SDK: Not found

=== Xamarin.Mac ===

Not Installed

=== Build Information ===

Release ID: 507000661
Git revision: b70bab61da996da29045ea8ee8aed1a6faedbe78
Build date: 2015-01-05 16:31:31-05
Xamarin addins: 82f6c71490562d6cd125a09287f441902fdac3d7

=== Operating System ===

Mac OS X 10.10.2
Darwin Neils-MBP.home 14.1.0 Darwin Kernel Version 14.1.0
    Mon Dec 22 23:10:38 PST 2014
    root:xnu-2782.10.72~2/RELEASE_X86_64 x86_64
Comment 2 Sebastien Pouliot 2015-02-06 09:45:24 UTC
That sounds more like `xbuild` itself (i.e. it ships with mono, not XI), c.c. the best people for this
Comment 3 John Miller [MSFT] 2015-02-06 10:05:49 UTC
Updated component. Please let me know if that is not the right place.
Comment 4 Rolf Bjarne Kvinge [MSFT] 2015-02-06 11:20:37 UTC
This might be invalid MSBuild syntax (but it works in mdtool/XS).

Jeff, can you comment if this is the case?
Comment 5 Jeffrey Stedfast 2015-02-06 12:10:01 UTC
it's a syntax that is monodevelop-specific (i.e. it's non-standard MSBuild XML stuff that MSBuild cannot interpret).

If you want to use those features, then you need to use mdtool (as Rolf discovered works fine).

Either that or you need to use the MSBuild XML syntax to hook into the build process in the .csproj file (or in your own targets that you import in the .csproj).

I'll /cc Michael Hutchinson to make sure I'm correct (he knows more about this than I do), but I'm pretty sure I'm right.
Comment 6 Mikayla Hutchinson [MSFT] 2015-02-06 13:59:08 UTC
#1 The MSBuild properties for the platform and configuration are separate: '/property:Configuration=PROD Ad-Hoc' '/property:Platform=iPhone'. mdtool parses the "combined" configuration, but MSBuild/xbuild does not.

#2 CustomCommands items are specific to the old MonoDevelop build engine, which will be deprecated soon.

If you build inside XS, or with mdtool, it will use a sort of hybrid model, i.e. it will build xbuild-compatible projects with the xbuild engine, but will still respect the CustomCommands from the old engine.

If you build with xbuild, MSBuild or Visual Studio, these CustomCommands will not work, since they do not understand the MD-specific commands.

However, you can manually edit the csproj file to add arbitrary MSBuild syntax which should work in all of these environments.

A simple example, overriding the BeforeBuild target:

<Target Name="BeforeBuild>
  <Exec Command="some shell command" />

For more info, see https://msdn.microsoft.com/en-us/library/ms366724.aspx
Comment 7 xamarin 2018-02-11 01:39:08 UTC
I have a Xamarin Studio (Mac) project that I recently modified so it would generate the Info.plist (which contains version info). The problem is that the 'Before Build' command does not execute before the Info.plist is checked. This sounds similar to the problem discussed here:


Have not tested in Visual Studio for Mac yet.

Will try the workaround listed above by Mikayla Hutchinson.
Comment 8 xamarin 2018-02-11 04:59:09 UTC
Sadly, that workaround does not work. I believe that's mentioned in the forum post linked. My workaround is to use a separate generic project to do this instead, and ensure it builds first.