Skip to content

Copy To Output

Kirill Osenkov edited this page May 5, 2024 · 1 revision

https://github.com/dotnet/msbuild/issues/10106

If we need to copy a file to output directory, the current pattern seems to be:

<ItemGroup>
  <None Include="myfile.txt">
    <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
  </None>
</ItemGroup>

It works by first assigning the target path in the AssignTargetPaths target: https://github.com/dotnet/msbuild/blob/dbf8d12deab2aee74f1bd574c1ecad2b39f552b4/src/Tasks/Microsoft.Common.CurrentVersion.targets#L3197-L3199

Then NoneWithTargetPath gets copied to _ThisProjectItemsToCopyToOutputDirectory: https://github.com/dotnet/msbuild/blob/dbf8d12deab2aee74f1bd574c1ecad2b39f552b4/src/Tasks/Microsoft.Common.CurrentVersion.targets#L5176-L5179

Then the target returns and the items go into _ThisProjectItemsToCopyToOutputDirectory: https://github.com/dotnet/msbuild/blob/dbf8d12deab2aee74f1bd574c1ecad2b39f552b4/src/Tasks/Microsoft.Common.CurrentVersion.targets#L5195-L5197

Then the items flow into _SourceItemsToCopyToOutputDirectory: https://github.com/dotnet/msbuild/blob/dbf8d12deab2aee74f1bd574c1ecad2b39f552b4/src/Tasks/Microsoft.Common.CurrentVersion.targets#L5204-L5214

Finally the copy happens in _CopyOutOfDateSourceItemsToOutputDirectory: https://github.com/dotnet/msbuild/blob/dbf8d12deab2aee74f1bd574c1ecad2b39f552b4/src/Tasks/Microsoft.Common.CurrentVersion.targets#L5264-L5282