C#C
C#3mo ago
peppy

MSBuild: Excluding objects if %(Filename) matches any in ItemGroup

Given a target:
<Target Name="ExplicitRemoveFromFilesToBundle" BeforeTargets="GenerateSingleFileBundle" DependsOnTargets="PrepareForBundle">
    <ItemGroup>
        <FilesToRemoveFromBundle Include="@(FilesToBundle)" Condition="$([System.String]::new('%(Filename)').Contains('TerraFX.Interop.Windows')) AND ('%(Extension)' == '.dll')" />
    </ItemGroup>
    <ItemGroup>
        <FilesToBundle Remove="@(FilesToRemoveFromBundle)" />
    </ItemGroup>
</Target>

that receives FilesAsBundle as the following input (abridged for simplicity):
'C:\Users\f\.nuget\packages\terrafx.interop.windows\10.0.26100.2\lib\net9.0\TerraFX.Interop.Windows.dll;C:\Users\f\.nuget\packages\hexa.net.imgui\2.2.8.4\runtimes\win-x86\native\cimgui.dll;D:\fh\artifacts\obj\Fahrenheit.Tools.ModManager\Release\net9.0\win-x86\fhmodmgr.deps.json'

I can remove TerraFX.Interop.Windows.dll from the list. But I have a need to match any of several filenames. I naively define a ItemGroup of DepsToExclude as such:
'TerraFX.Interop.Windows;Hexa.NET.DirectXTex;Hexa.NET.ImGui.Backends;Hexa.NET.ImGui;HexaGen.Runtime.COM;HexaGen.Runtime;ImGuiImpl;cimgui;DirectXTex'

and then attempted to amend the comparison:
<ItemGroup>
    <FilesToRemoveFromBundle Include="@(FilesToBundle)" Condition="$([System.String]::Copy('%(Filename)').Contains('@(DepsToExclude)')) AND ('%(Extension)' == '.dll')" />
</ItemGroup>

and also attempted to directly Remove from FilesToBundle as follows:
<ItemGroup>
    <FilesToBundle Remove="**/*/TerraFX.Interop.Windows.dll" />
    <!-- others... -->
</ItemGroup>

but neither approach was successful. I'm struggling to follow how and when MSBuild expands
@
to automatically cover all elements in a group. What would be the correct way to express this?
Was this page helpful?