Skip to content

Well-known metadata not available when dynamically creating items #2844

@nickwalkmsft

Description

@nickwalkmsft

Well-known item metadata is not available during item creation for dynamically defined items (items defined within a Target). I am not super familiar with msbuild but this was surprising to me.

Steps to reproduce

Project file

<Project>

    <!-- Regular ol' items containing metadata defined from well-known metadata. %(Filename) and other
         well-known metadata exist during creation, so ArbitraryMetadata is defined with the value of
         Filename as expected -->
    <ItemGroup>
        <MyFileItems Include="myfiles\*" ArbitraryMetadata="%(Filename)" />
    </ItemGroup>

    <Target Name="MyTarget">

        <!-- Works as expected -->
        <Message Importance="high" Text="%(MyFileItems.Identity) -> %(MyFileItems.ArbitraryMetadata)" />

        <!-- Now dynamically (within the Target) define another item list the same way -->
        <ItemGroup>
            <MyDynamicFileItems Include="myfiles\*" ArbitraryMetadata="%(Filename)" />
        </ItemGroup>

        <!-- Unexpected: ArbitraryMetdata is empty, because %(Filename) and all other well-known metadata
             was not defined during creation -->
        <Message Importance="high" Text="%(MyDynamicFileItems.Identity) -> %(MyDynamicFileItems.ArbitraryMetadata)" />

        <!-- Note that %(Filename) and other well-known metadata *is* available now that the item list has
             already been created -->
         <Message Importance="high" Text="%(MyDynamicFileItems.Identity) -> %(MyDynamicFileItems.Filename)" />
    </Target>

</Project>

Directory contents:

/
- repro.proj
- myfiles/
     - file1
     - file2
     - file3

Command line

msbuild repro.proj /t:MyTarget

Expected behavior

Prints:

  myfiles\file1 -> file1
  myfiles\file2 -> file2
  myfiles\file3 -> file3
  myfiles\file1 -> file1
  myfiles\file2 -> file2
  myfiles\file3 -> file3
  myfiles\file1 -> file1
  myfiles\file2 -> file2
  myfiles\file3 -> file3

Actual behavior

Prints:

  myfiles\file1 -> file1
  myfiles\file2 -> file2
  myfiles\file3 -> file3
  myfiles\file1 -> 
  myfiles\file2 -> 
  myfiles\file3 -> 
  myfiles\file1 -> file1
  myfiles\file2 -> file2
  myfiles\file3 -> file3

Environment data

msbuild /version output:

Microsoft (R) Build Engine version 15.4.8.50001 for .NET Framework
Copyright (C) Microsoft Corporation. All rights reserved.

15.4.8.50001

OS info:
Microsoft Windows [Version 10.0.16299.192]

If applicable, version of the tool that invokes MSBuild (Visual Studio, dotnet CLI, etc):

I'm seeing this behavior everywhere - VS, dotnet, running msbuild manually.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions