MSBuild Conditions and Visual Studio

Corrado Cavalli
Corrado Cavalli
Published in
2 min readAug 23, 2019

Don’t trust Visual Studio…

My goal was to include a different file inside my project depending on selected project configuration, something that MSBuild is able to do using an appropriate Condition applied at item level or, in case of multiple items involved, at ItemGroup level.
After adding the two files to my project and customizing their content, I’ve edited the .csproj file and added following statements:

<ItemGroup>
<AppxManifest Include="Package.debug.appxmanifest" Condition="'$(Configuration)'=='Debug'">
<SubType>Designer</SubType>
</AppxManifest>
<AppxManifest Include="Package.release.appxmanifest" Condition="'$(Configuration)'!='Debug'">
<SubType>Designer</SubType>
</AppxManifest>
</ItemGroup>

but the result was that inside Visual Studio both files were listed and since the app can’t have more than one .appxmanifest file the project couldn’t build.

Two files aren’t better than one

After several unsuccessful tentative I then tried with another cool way to define non mandatory content inside a .csproj via the Choose element.

<Choose>
<When Condition="'$(Configuration)'=='Debug'">
<ItemGroup>
<AppxManifest Include="package.debug.appxmanifest" >
<SubType>Designer</SubType>
</AppxManifest>
</ItemGroup>
</When>

<Otherwise>
<ItemGroup>
<AppxManifest Include="Package.release.appxmanifest" >
<SubType>Designer</SubType>
</AppxManifest>
</ItemGroup>
</Otherwise>
</Choose>

but also this alternative apparently was not working and i started getting lost until my friend Damiano hinted me at this old StackOverflow post where it is mentioned that Visual Studio (2010! at that time) doesn’t filter item out based on applied condition. 🙈

So in the end everything was working, just a little ”annoyance” that after several editions of Visual Studio is still there.
I hope in future Visual Studio will dim out project items not satisfying a condition so that it will be immediately evident that that file is not part of the build.

--

--

Corrado Cavalli
Corrado Cavalli

Senior Sofware Engineer at Microsoft, former Xamarin/Microsoft MVP mad about technology. MTB & Ski mountaineering addicted.