Bug 57853 - $(MSBuildThisFileDirectory) in a Choose element resolves to the wrong value when imported
Summary: $(MSBuildThisFileDirectory) in a Choose element resolves to the wrong value w...
Status: RESOLVED NOT_ON_ROADMAP
Alias: None
Product: Tools
Classification: Mono
Component: xbuild ()
Version: 5.2 (2017-04)
Hardware: PC Linux
: --- normal
Target Milestone: ---
Assignee: Bugzilla
URL:
Depends on:
Blocks:
 
Reported: 2017-06-29 00:05 UTC by Christopher Warrington
Modified: 2018-03-15 10:39 UTC (History)
3 users (show)

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


Attachments
Project that reproduces the problem. run xbuild leaf/leaf.proj (668 bytes, application/x-xz)
2017-06-29 00:05 UTC, Christopher Warrington
Details


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 NOT_ON_ROADMAP

Description Christopher Warrington 2017-06-29 00:05:42 UTC
Created attachment 23188 [details]
Project that reproduces the problem. run xbuild leaf/leaf.proj

xbuild is evaluating $(MSBuildThisFileDirectory) inside of a Choose element that is in an imported project to the path of the importing project, not the imported project. The makes it difficult the use the $(MSBuildThisFileDirectory)some-file-co-located-with-the-imported-project difficult.

Given common.props with the following:


<?xml version="1.0" encoding="utf-8"?>
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <PropertyGroup>
    <CommonTopLevelProp>$(MSBuildThisFileDirectory)</CommonTopLevelProp>
  </PropertyGroup>
  <ItemGroup>
    <Diag Include="common-top-level=$(MSBuildThisFileDirectory)" />
  </ItemGroup>

  <Choose>
    <When Condition="false" />
    <Otherwise>
      <PropertyGroup>
        <CommonChooseProp>$(MSBuildThisFileDirectory)</CommonChooseProp>
      </PropertyGroup>
      <ItemGroup>
        <Diag Include="common-choose=$(MSBuildThisFileDirectory)" />
      </ItemGroup>
    </Otherwise>
  </Choose>
</Project>

and leaf/leaf.proj with the following:

<?xml version="1.0" encoding="utf-8"?>
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003" DefaultTargets="Dump">
  <Import Project="..\common.props" />

  <PropertyGroup>
    <LeafTopLevelProp>$(MSBuildThisFileDirectory)</LeafTopLevelProp>
  </PropertyGroup>
  <ItemGroup>
    <Diag Include="leaf-top-level=$(MSBuildThisFileDirectory)" />
  </ItemGroup>

  <Choose>
    <When Condition="false" />
    <Otherwise>
      <PropertyGroup>
        <LeafChooseProp>$(MSBuildThisFileDirectory)</LeafChooseProp>
      </PropertyGroup>
      <ItemGroup>
        <Diag Include="leaf-choose=$(MSBuildThisFileDirectory)" />
      </ItemGroup>
    </Otherwise>
  </Choose>

  <Target Name="Dump">
    <Message Text="Diag: @(Diag)" />
    <Message Text="LeafTopLevelProp: $(LeafTopLevelProp)" />
    <Message Text="LeafChooseProp: $(LeafChooseProp)" />
    <Message Text="CommonTopLevelProp: $(CommonTopLevelProp)" />
    <Message Text="CommonChooseProp: $(CommonChooseProp)" />
  </Target>
</Project>

xbuild leaf.proj prints the following:

XBuild Engine Version 14.0
Mono, Version 5.0.1.1
Copyright (C) 2005-2013 Various Mono authors

Build started 6/28/2017 4:56:23 PM.
__________________________________________________
Project "/home/chwarr/import-mbtfd/leaf/leaf.proj" (default target(s)):
        Target Dump:
                Diag: common-choose=/home/chwarr/import-mbtfd/leaf/;leaf-choose=/home/chwarr/import-mbtfd/leaf/;common-top-level=/home/chwarr/import-mbtfd/;leaf-top-level=/home/chwarr/import-mbtfd/leaf/
                LeafTopLevelProp: /home/chwarr/import-mbtfd/leaf/
                LeafChooseProp: /home/chwarr/import-mbtfd/leaf/
                CommonTopLevelProp: /home/chwarr/import-mbtfd/
                CommonChooseProp: /home/chwarr/import-mbtfd/leaf/
Done building project "/home/chwarr/import-mbtfd/leaf/leaf.proj".

Build succeeded.
         0 Warning(s)
         0 Error(s)

Time Elapsed 00:00:00.1492800

while msbuild leaf.proj print the following:

Microsoft (R) Build Engine version 15.2.0.0 ( Thu May 11 17:28:41 UTC 2017)
Copyright (C) Microsoft Corporation. All rights reserved.

Build started 6/28/2017 4:56:36 PM.
Project "/home/chwarr/import-mbtfd/leaf/leaf.proj" on node 1 (default targets).
Dump:
  Diag: common-top-level=/home/chwarr/import-mbtfd/;common-choose=/home/chwarr/import-mbtfd/;leaf-top-level=/home/chwarr/import-mbtfd/leaf/;leaf-choose=/home/chwarr/import-mbtfd/leaf/
  LeafTopLevelProp: /home/chwarr/import-mbtfd/leaf/
  LeafChooseProp: /home/chwarr/import-mbtfd/leaf/
  CommonTopLevelProp: /home/chwarr/import-mbtfd/
  CommonChooseProp: /home/chwarr/import-mbtfd/
Done Building Project "/home/chwarr/import-mbtfd/leaf/leaf.proj" (default targets).

Build succeeded.
    0 Warning(s)
    0 Error(s)

Time Elapsed 00:00:00.47

Compare the values for the Diag item common-choose and the property CommonChooseProp. Under xbuild these end with /leaf/, but under msbuild they do not. The msbuild behavior is what I expected.

My workaround has been to introduce a property top-level and then refer to that property in the Choose.
Comment 1 Christopher Warrington 2017-06-29 00:13:57 UTC
I am seeing the same behavior with xbuild 5.2.0.179:

XBuild Engine Version 14.0
Mono, Version 5.2.0.179
Copyright (C) 2005-2013 Various Mono authors

Build started 6/28/2017 5:09:58 PM.
__________________________________________________
Project "/home/chwarr/import-mbtfd/leaf/leaf.proj" (default target(s)):
        Target Dump:
                Diag: common-choose=/home/chwarr/import-mbtfd/leaf/;leaf-choose=/home/chwarr/import-mbtfd/leaf/;common-top-level=/home/chwarr/import-mbtfd/;leaf-top-level=/home/chwarr/import-mbtfd/leaf/
                LeafTopLevelProp: /home/chwarr/import-mbtfd/leaf/
                LeafChooseProp: /home/chwarr/import-mbtfd/leaf/
                CommonTopLevelProp: /home/chwarr/import-mbtfd/
                CommonChooseProp: /home/chwarr/import-mbtfd/leaf/
Done building project "/home/chwarr/import-mbtfd/leaf/leaf.proj".

Build succeeded.
         0 Warning(s)
         0 Error(s)

Time Elapsed 00:00:00.1585890
Comment 2 Matt Ward 2017-06-29 15:29:11 UTC
Probably want to install the separate msbuild package on Linux and use that instead of xbuild. I suspect no fixes will be made to xbuild now that msbuild is available on other platforms.
Comment 3 Christopher Warrington 2017-06-29 20:51:55 UTC
Encouraging a switch to MSBuild seems reasonable to me. If you want to close this with some kind of "will never be fixed" resolution (NOT_ON_ROADMAP?), I'm fine with that.
Comment 4 Marek Safar 2018-03-15 10:39:54 UTC
We deprecated xbuild and recommend using msbuild instead. Switching to msbuild will also most likely resolve the issue you reported.