6

I'm trying to create a filtered item group from another one, where the filtered item group will only contain items from the first group that have a specified file extension.

I'm getting the following error:

error MSB4190: The reference to the built-in metadata "Extension" at position 1 is not allowed in this condition "'%(Extension)'=='.sys'".

When I run this:

<?xml version="1.0" encoding="utf-8"?>
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003"
         DefaultTargets="Go"
         ToolsVersion="4.0">

  <ItemGroup>
    <Files Include="X.exe"/>
    <Files Include="Y.sys"/>
  </ItemGroup>

  <ItemGroup>
    <SysFiles Include="%(Files.Identity)" Condition="'%(Extension)'=='.sys'">
    </SysFiles>
  </ItemGroup>

  <Target Name="Go">
    <Message Text="SysFiles=@(SysFiles)"/>
  </Target>

</Project>

Firstly, I can't understand why the meta data isn't allowed at this position? Is it some artificial restriction or unimplemented part of msbuild?

Secondly, what's a concise way of achieving this kind of transform?

Thanks very much.

Scott Langham
  • 58,735
  • 39
  • 131
  • 204

1 Answers1

5

Ok, I've found if I specify the ItemGroup SysFiles inside target Go, it will work without error.

Scott Langham
  • 58,735
  • 39
  • 131
  • 204
  • Whoa, hang on a minute there buddy. This is not a trivial fix that will work the same way. Targets can only be executed during an actual build. If you're trying to filter an item group for things that affect the UI of your IDE, then it's pretty important that this is able to work **outside** of a target/task. See https://stackoverflow.com/questions/25177363/how-to-get-visual-studio-to-execute-msbuild-initialtargets-or-building-a-projec – marknuzz Oct 08 '17 at 02:40
  • 1
    @Nuzzolilo If that's the behaviour you need, you might need to raise a ticket with the team that maintains msbuild, because it doesn't appear to work that way. – Scott Langham Oct 09 '17 at 11:10
  • I would like to add one information to this. Take a look at this post: https://social.msdn.microsoft.com/Forums/en-US/109c2303-b4a5-4aa1-8f61-01663d358fa5/using-metadata-in-conditions, the person responding there mentioned that PropertyGroup outside Target is evaluated before ItemGroup. Now why ItemGroup outside Target also does not work, I don't know, but I also observe that putting ItemGroup inside a Target will make it work. – ohnezahn Mar 24 '22 at 17:39