Author Topic: MSBuild: Why the `Condition` attribute doesn't work for the `ItemGroup` element?  (Read 3478 times)

0 Members and 1 Guest are viewing this topic.

Andrey Bushman

  • Swamp Rat
  • Posts: 864
MS Visual Studio 2013 Update 4.

Both When and ItemGroup elements can to have the Condition attribute (as I see in MSDN). But I get different results.

When $(CAD_Year) is 2015, I expect the AcRibbon will be not referensed to my project:

Code: [Select]
<ItemGroup Condition= "'$(CAD_Year)' &lt; '2010'" >   
  <Reference Include="AcRibbon">
    <HintPath>$(CAD_SDK_Location)\$(Inc)\AcRibbon.dll</HintPath>
    <Private>False</Private>
  </Reference>
</ItemGroup>
But I get unresolved reference in the Solution Browser for the AcRibbon always.

But this variant works fine:

Code: [Select]
<Choose>
  <When Condition= "'$(CAD_Year)' &lt; '2010'">
    <ItemGroup>
      <Reference Include="AcRibbon">
        <HintPath>$(CAD_SDK_Location)\$(Inc)\AcRibbon.dll</HintPath>
        <Private>False</Private>
      </Reference>
    </ItemGroup>
  </When>
</Choose>
At this case the AcRibbon is referenced only when $(CAD_Year) less than 2010 (as I expected). Both variants are located in the global area.

Why I get the different results?

owenwengerd

  • Bull Frog
  • Posts: 451
I'm not familiar with this problem, but it sounds like Solution Explorer just has an incomplete xml parser that doesn't evaluate the condition correctly in the ItemGroup case. Presumably MSBuild parses it correctly.

Andrey Bushman

  • Swamp Rat
  • Posts: 864
Thank you for the answer, owenwengerd. But I am thinking you aren't right at this case... I already found the reason of this behaviour (in my opinion), but I didn't wrote about it, since I thought this is interesting for nobody. If it is interesting for you: I found the answer in MSDN here:
Quote from: MSDN
While conditional import statements work in command-line MSBuilds, they do not work with MSBuild in the Visual Studio integrated development environment (IDE). Conditional imports are evaluated by using the configuration and platform values that are set when the project is loaded. If changes are subsequently made that require a reevaluation of the conditionals in the project file, for example, changing the platform, Visual Studio reevaluates the conditions on properties and items, but not on imports. Because the import conditional is not reevaluated, the import is skipped. To work around this, put conditional imports in the .targets files or put code in a conditional block such as a Choose Element (MSBuild) block.
I think this is true for ItemGroup element also...

P.S. I don't like to create the themes for own "monologues". If nobody write answers and I found an answer myself, then I prefer delete that theme, when it is possible. Unfortunately, I don't know how to delete such own themes on this site. I get the message, when I try to do it:
Quote
You cannot delete your own topics in this board. Check to make sure this topic wasn't just moved to another board.
If you tell me how can I do it, then I will be grateful to you.
« Last Edit: March 06, 2015, 02:22:31 AM by Andrey Bushman »