Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Jan 29, 2026

Context

RecursiveDir metadata is lost when glob patterns are expanded through MSBuild tasks in targets. This breaks scenarios like dotnet pack where users expect to construct target paths based on %(RecursiveDir) from globbed items.

RecursiveDir is computed by comparing IncludeBeforeWildcardExpansionEscaped (original glob pattern) with IncludeEscaped (expanded file path). When GatherTaskItemOutputs created new ProjectItemInstance objects from task outputs, it used a constructor that set both values to IncludeEscaped, making them identical and causing RecursiveDir to return empty.

Changes Made

  • TaskExecutionHost.cs: Use 5-parameter ProjectItemInstance constructor to preserve IncludeBeforeWildcardExpansionEscaped from source TaskItem
  • TaskBuilder_Tests.cs: Add RecursiveDirMetadataPreservedInTaskOutputs test
- newItem = new ProjectItemInstance(_projectInstance, outputTargetName, outputAsProjectItem.IncludeEscaped, parameterLocationEscaped);
+ newItem = new ProjectItemInstance(_projectInstance, outputTargetName, outputAsProjectItem.IncludeEscaped, outputAsProjectItem.IncludeBeforeWildcardExpansionEscaped, parameterLocationEscaped);

Testing

  • New test validates RecursiveDir is correctly computed for nested files from task outputs
  • All 84 TaskExecutionHost tests pass
  • All RecursiveDir-related tests pass

Notes

This only affects ProjectItemInstance.TaskItem outputs (the common case). Microsoft.Build.Utilities.TaskItem outputs don't have IncludeBeforeWildcardExpansionEscaped—those rely on explicitly set RecursiveDir metadata (as CreateItem task already does).

Original prompt

This section details on the original issue you should resolve

<issue_title>RecursiveDir metadata is lost if glob is expanded through a msbuild task in targets</issue_title>
<issue_description>Repro:

s:\work\repro_rohit_target_recursivedir>type test.proj
<Project DefaultTargets="Print">
<Target Name="CollectItems" Returns="@(I)">
  <ItemGroup>
    <Inner Include="$(MSBuildThisFileDirectory)\**\file.txt" />
  </ItemGroup>

  <Message Importance="high"
           Text="Inner: @(Inner), recdir: %(Inner.RecursiveDir)" />

</Target>

<Target Name="GetItemsFromInner">
  <Touch Files="folder\subfolder\file.txt" />
  <MSBuild Projects="$(MSBuildThisFileFullPath)"
           Targets="CollectItems">
    <Output TaskParameter="TargetOutputs"
           ItemName="Outer" />
  </MSBuild>
</Target>

<Target Name="Print" DependsOnTargets="CollectItems">
  <Message Importance="high"
           Text="Outer: @(Outer), recdir: %(Outer.RecursiveDir)" />
</Target>
</Project>
s:\work\repro_rohit_target_recursivedir>msbuild /v:m

The impacted scenario is dotnet.exe pack , which provides an extension to run a target in inner build to pack tfm specific content into a package. Due to this bug, users cannot provide globbing paths and expect pack to construct the target path in the package based on RecursiveDir value.

CC: @rainersigwald </issue_description>

Comments on the Issue (you are @copilot in this section)


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

@dotnet-policy-service
Copy link
Contributor

Hello @@copilot, I noticed that you’re changing an .swr file or any file under src/Package/MSBuild.VSSetup.. Please make sure to validate this change by an experimental VS insertion. This is accomplished by pushing to an exp/* branch, which requires write permissions to this repo.

Copilot AI and others added 2 commits January 29, 2026 12:22
Preserve IncludeBeforeWildcardExpansionEscaped when creating
ProjectItemInstance from TaskItem outputs, ensuring RecursiveDir
metadata is calculated correctly.

Co-authored-by: JanProvaznik <[email protected]>
Copilot AI changed the title [WIP] Fix RecursiveDir metadata loss in msbuild task Fix RecursiveDir metadata lost when gathering task item outputs Jan 29, 2026
Copilot AI requested a review from JanProvaznik January 29, 2026 12:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

RecursiveDir metadata is lost if glob is expanded through a msbuild task in targets

2 participants