Bug 18106 - xbuild evaluates Choose/When too late
Summary: xbuild evaluates Choose/When too late
Status: RESOLVED FIXED
Alias: None
Product: Tools
Classification: Mono
Component: xbuild ()
Version: unspecified
Hardware: PC Linux
: --- normal
Target Milestone: ---
Assignee: Marek Safar
URL:
Depends on:
Blocks:
 
Reported: 2014-03-01 02:35 UTC by Lex Li
Modified: 2014-05-13 16:21 UTC (History)
2 users (show)

Tags:
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:
Status:
RESOLVED FIXED

Description Lex Li 2014-03-01 02:35:26 UTC
MSBuild supports Property Functions, while xbuild is not. This can be accepted.

http://msdn.microsoft.com/en-us/library/dd633440.aspx#BKMK_String

Below is a test script called test.proj,

======================
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
    
    <PropertyGroup>
        <PackagesProjectConfig>packages.$(MSBuildProjectName.Replace(' ', '_')).config</PackagesProjectConfig>
    </PropertyGroup>
	
    <Choose>
        <When Condition="Exists('$(PackagesProjectConfig)')">
            <PropertyGroup>
                <PackagesConfig>$(PackagesProjectConfig)</PackagesConfig>
            </PropertyGroup>
        </When>
        <When Condition="Exists('packages.config')">
            <PropertyGroup>
                <PackagesConfig>packages.config</PackagesConfig>
            </PropertyGroup>
        </When>
    </Choose>
	
    <PropertyGroup>

        <!-- Commands -->
        <RestoreCommand>install "$(PackagesConfig)" </RestoreCommand>

    </PropertyGroup>

    <Target Name="Build">
        <Warning Text="$(RestoreCommand)"/>
    </Target>
</Project>
======================

Save it to disk and also create an empty text file called packages.config in the same folder.

Then run xbuild you will see 

======================
lextm@lextm-virtual-machine:~/src$ xbuild
XBuild Engine Version 12.0
Mono, Version 3.4.0.0
Copyright (C) 2005-2013 Various Mono authors

Build started 2/21/2014 12:00:58 AM.
__________________________________________________
Project "/home/lextm/src/test.proj" (default target(s)):
	Target Build:
/home/lextm/src/test.proj:  warning : install "" 
Done building project "/home/lextm/src/test.proj".

Build succeeded.

Warnings:

/home/lextm/src/test.proj (default targets) ->
(Build target) ->

	/home/lextm/src/test.proj:  warning : install "" 

	 1 Warning(s)
	 0 Error(s)

Time Elapsed 00:00:00.0891080
lextm@lextm-virtual-machine:~/src$ xbuild
XBuild Engine Version 12.0
Mono, Version 3.4.0.0
Copyright (C) 2005-2013 Various Mono authors

Build started 2/21/2014 12:01:26 AM.
__________________________________________________
Project "/home/lextm/src/test.proj" (default target(s)):
	Target Build:
/home/lextm/src/test.proj:  warning : install "" 
Done building project "/home/lextm/src/test.proj".

Build succeeded.

Warnings:

/home/lextm/src/test.proj (default targets) ->
(Build target) ->

	/home/lextm/src/test.proj:  warning : install "" 

	 1 Warning(s)
	 0 Error(s)

Time Elapsed 00:00:00.0886100

======================

This does not look good enough, as even if xbuild cannot evaluate the function call, it should only affect the single PropertyGroup, not the latter Choose IMHO.

Before fully supporting such functions, such exceptions might be better handled.

Note that this portion of script was taken from NuGet.targets. I will report to NuGet guys that they should better use OS testing to avoid showing such functions to Mono xbuild at this moment.
Comment 1 Lex Li 2014-03-01 02:52:19 UTC
Just found my above analysis was wrong, so I changed the bug title.

A revised script shows in fact this is caused by something else.

===========================
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
	
    <Choose>
        <When Condition="Exists('packages.config')">
            <PropertyGroup>
                <PackagesConfig>packages.config</PackagesConfig>
            </PropertyGroup>
        </When>
    </Choose>
	
    <PropertyGroup>

        <!-- Commands -->
        <RestoreCommand>install "$(PackagesConfig)" </RestoreCommand>

    </PropertyGroup>

    <Target Name="Build">
        <Warning Text="$(RestoreCommand)"/>
    </Target>
</Project>
===========================

MSBuild can resolve PackagesConfig correctly while xbuild fails.

It seems to me that Choose/When is not yet supported by xbuild.
Comment 2 Mikayla Hutchinson [MSFT] 2014-03-03 16:20:39 UTC
If you try to use $(PackagesConfig) from the target, it'll work. It looks like Choose/When is evaluated after the properties, instead of in order.
Comment 3 Marek Safar 2014-05-13 16:21:48 UTC
Fixed in master