Item metadata not being evaluated when passed to build task assemblies
Steps to reproduce
I have a C++ XAML project with custom build logic that more or less is like this:
<ItemGroup>
<Dependency Include=”A”>
<IncludeDirs>C:\Path\A</IncludeDirs>
</Dependency>
< Dependency Include=”B”>
<IncludeDirs>C:\Path\B</IncludeDirs>
</Dependency >
</ItemGroup>
<PropertyGroup>
<ExtraIncludeDirs>@(Dependency->'%(IncludeDirs)')</ ExtraIncludeDirs >
</PropertyGroup>
<ItemDefinitionGroup>
<ClCompile>
<AdditionalIncludeDirectories>%(ClCompile.AdditionalIncludeDirectories);$(ExtraIncludeDirs)</AdditionalIncludeDirectories>
</ClCompile>
<ResourceCompile>
<AdditionalIncludeDirectories>%(ResourceCompile.AdditionalIncludeDirectories);$(ExtraIncludeDirs)</AdditionalIncludeDirectories>
</ResourceCompile>
</ItemDefinitionGroup>
Expected behavior
This should compile just fine with the extra include directories passed to the compiler.
Actual behavior
This worked fine for the most part in normal C++ projects, until I tried to use this in a C++ XAML project. Then it blew up trying to compile the XAML with an opaque “Path has invalid characters” error when invoking the XAML compiler MSBuild task. I debugged it and found out it was because the XamlCppIncludeDirectories property that gets passed to the task had a value of “@(Dependency->'%(IncludeDirs)')” rather than the actual evaluated value. For reference, here’s how that task is invoked (from C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\MSBuild\Microsoft\WindowsXaml\v15.0\8.1\Microsoft.Windows.UI.Xaml.Common.Targets):
<!-- In order for the XAML compiler to generated the correct relative paths for #includes in
the generated C++ code it needs to know the AdditionalIncludeDirectories of all the .cpp
files are DependentUpon XAML files.-->
<Target Name="GetXamlCppIncludeDirectories" Condition="'$(ManagedAssembly)'=='false' and '$(XamlCppIncludeDirectories)' == ''">
<GetXamlCppIncludeDirectories ClCompile="@(ClCompile)">
<Output PropertyName="XamlCppIncludeDirectories" TaskParameter="ComputedIncludeDirectories" />
</GetXamlCppIncludeDirectories>
</Target>
Now, I expect the property value to be non-evaluated during the evaluation phase of the build per the MSBuild documentation, but that same documentation indicates that these transforms and expressions should be evaluated during the execution phase when this XAML target is executing. It seems to happen properly for the normal C++ CL task itself, which is invoked like:
<CL Condition="'%(ClCompile.PrecompiledHeader)' != 'Create' and '%(ClCompile.ExcludedFromBuild)'!='true' and '%(ClCompile.CompilerIteration)' == '' and @(ClCompile) != ''"
BuildingInIDE ="$(BuildingInsideVisualStudio)"
Sources ="@(ClCompile)"
AdditionalIncludeDirectories ="%(ClCompile.AdditionalIncludeDirectories)"
…
Note the difference: CL is invoked by directly evaluating each metadata property and passing them each to the task. GetXamlCppIncludeDirectories is passed each whole item and the metadata is presumably processed programmatically within the task’s assembly.
I was able to work around the problem of the non-evaluation by forcing evaluation myself in a rather silly/hacky intermediate custom build target:
<Target Name="CreateMetadata"
BeforeTargets=”PrepareForBuild”>
<ItemGroup>
<ClCompile>
<AdditionalIncludeDirectories>%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
</ClCompile>
</ItemGroup>
</Target>
Talking to an MSBuild developer, it seems that the problem is that there is an issue with the items not having their default metadata evaluated by the MSBuild engine before being passed to the XAML task, so the raw property value never gets recursively evaluated before being passed in. This is due to an internal implementation detail of how ItemDefinitionGroup entries are referenced/evaluated.
Ideally properties like this would get evaluated in such cases before being passed to MSBuild task assemblies, this special condition could be more easily detectable by developers, or at minimum this limitation get documented.
Environment data
msbuild /version output:
Microsoft (R) Build Engine version 15.6.85.37198 for .NET Framework
Copyright (C) Microsoft Corporation. All rights reserved.
15.6.85.37198
OS info: Windows 10 Version 1709 (OS Build 16299.309)
If applicable, version of the tool that invokes MSBuild (Visual Studio, dotnet CLI, etc): Visual Studio 2017 15.6.6
I am facing this same issue. is there are resolution/workaround for this?