msbuild icon indicating copy to clipboard operation
msbuild copied to clipboard

AnyHaveMetadataValue returns non boolean value when item group is empty

Open pranavkm opened this issue 4 years ago • 3 comments

Consider:

    <Target Name="Foo">
        <ItemGroup>
            <Var Include="A" />
            <Var Include="B" />
        </ItemGroup>
        
        <PropertyGroup>
            <X>@(Var->AnyHaveMetadataValue('Identity', 'A'))</X>
        </PropertyGroup>
        
        <ItemGroup>
            <Var Remove="@(Var)" />
        </ItemGroup>
        
        <PropertyGroup>
            <Y>@(Var->AnyHaveMetadataValue('Identity', 'A'))</Y>
        </PropertyGroup>
        
        <Message Text="X=$(X) Y=$(Y)" Importance="High" />
    </Target>

Result:

X=true Y=

This makes using it much less elegant, as also the documentation a bit misleading.

pranavkm avatar Feb 07 '20 01:02 pranavkm

To change this I think we'd have to figure out why it's happening. Is it because we interpret @(Var), see that it's empty, and don't bother calling the item function? I suspect that's it. If so, we may not be able to change this since it's not clear whether the item functions can handle an empty-list input. But we'd need to debug in to figure out what's up.

rainersigwald avatar Feb 10 '20 21:02 rainersigwald

it should be able to work in the item functions if you special case for when the lists are empty.

AraHaan avatar Jun 28 '22 01:06 AraHaan

Yeah, I'm also hitting this in a similar fashion where I have a code like this to check for a default Program.{cs,vb,fs} file in the project.

<_ProgramSourceFileIsPresent>@(Compile->AnyHaveMetadataValue('Identity', 'Program$(DefaultLanguageSourceExtension)'))</_ProgramSourceFileIsPresent>

Nirmal4G avatar Jun 28 '22 14:06 Nirmal4G

Just hit something (probably) similar when trying to parse PackageVersion statements for a specific package. Is there any workaround for this?

Blackclaws avatar Sep 29 '22 13:09 Blackclaws

A working workaround that is rather inelegant but works for Conditions:

@(Var->WithMetadataValue('Identity', 'A')->Count()) >= 1

Blackclaws avatar Sep 29 '22 13:09 Blackclaws