sdk icon indicating copy to clipboard operation
sdk copied to clipboard

3rd party XML documentation files in NuGet packages are not copied to my project's build folder

Open fizxmike opened this issue 8 years ago • 33 comments

I've dug around for a solution to this problem, but I only find issues related to dotnet publish not including a project's xml documentation. My issue is that XML documentation files in NuGet packages are not copied to my project's build folder. I hope the distinction is clear to a human (google couldn't get it).

Steps to reproduce

Add some_other_library nuget package which includes xml documentation to you project. Do dotnet build

Expected behavior

The some_other_library.xml is included in the build (output) folder like so: image (screenshot from output of VS 2017)

Actual behavior

some_other_library.xml file is not present in the build (output) folder: image (screenshot of output of dotnet cli)

Environment data

dotnet --info output:

.NET Command Line Tools (2.1.101)

Product Information:
 Version:            2.1.101
 Commit SHA-1 hash:  6c22303bf0

Runtime Environment:
 OS Name:     Windows
 OS Version:  10.0.14393
 OS Platform: Windows
 RID:         win10-x64
 Base Path:   C:\Program Files\dotnet\sdk\2.1.101\

Microsoft .NET Core Shared Framework Host

  Version  : 2.0.6
  Build    : 74b1c703813c8910df5b96f304b0f2b78cdf194d

fizxmike avatar Jun 13 '18 16:06 fizxmike

@wli3 you were looking at something right this. Can you comment?

livarcocc avatar Jun 13 '18 18:06 livarcocc

give me some time i need to find the related issue

wli3 avatar Jun 13 '18 18:06 wli3

Sorry for the delay. It has similar root cause with https://github.com/dotnet/sdk/issues/1458

wli3 avatar Jun 18 '18 17:06 wli3

@fizxmike there are some work around in dotnet/sdk#1458 a solution is still work in progress. cc @nguerrera

wli3 avatar Jun 18 '18 17:06 wli3

This limitation is really painfull when the xml documentation are the one describing the model packed with NuGET and used on the WebAPI contracts where something like swagger is used to self document the WebApi themselves, in such a case there's no possibility to auto-describe the contracts (since it is autodiscovered using the xml documentations)

MonDeveloper avatar May 17 '19 11:05 MonDeveloper

怎么解决?

wuchang avatar May 19 '19 11:05 wuchang

The following solution helped me. Add it in you .csproj file for project which is building:

<ItemGroup>
	<PackageReference Include="{PackageName}" Version="{PackageVersion}" GeneratePathProperty="true"/>
</ItemGroup>

<Target Name="CopyXMLFromPackages" AfterTargets="Build">
    <Copy SourceFiles="$(PkgPackage_Name)\lib\netcoreapp3.1\{PackageName}.xml" DestinationFolder="$(OutDir)" />
</Target>

sergiodank avatar Jan 22 '20 15:01 sergiodank

Any timeframe when this is getting fixed? Its already been 2 years ago this issue was opened and is a breaking change compared to the old system.

I really want my csproj to just copy all documentation xml files and pdb files to the output directory just like it used to do.

The workarounds that were mentioned do not work. Tried to do this with msbuild but failed since it only includes 1 of the xml files...:

<PackageReferenceFiles Include="$(NugetPackageRoot)\%(PackageReference.FileName)\%(PackageReference.Version)\**\*.xml" />

Rick-van-Dam avatar Apr 15 '20 13:04 Rick-van-Dam

Any timeframe when this is getting fixed? Its already been 2 years ago this issue was opened and is a breaking change compared to the old system.

Try this:

<ItemGroup>
	<PackageReference Include="MyNuget" Version="0.0.2526">
		<CopyToOutputDirectory>lib/netcoreapp3.1/*.xml</CopyToOutputDirectory>
	</PackageReference>
</ItemGroup>

<Target Name="CopyXMLFromPackagesForBuild" AfterTargets="Build">
	<ItemGroup>
		<PackageReferenceFiles Condition="%(PackageReference.CopyToOutputDirectory) != ''" Include="$(NugetPackageRoot)$([MSBuild]::Escape('%(PackageReference.Identity)').ToLower())/%(PackageReference.Version)/%(PackageReference.CopyToOutputDirectory)" />
	</ItemGroup>
	<Copy SourceFiles="@(PackageReferenceFiles)" DestinationFolder="$(OutDir)" />
</Target>

<Target Name="CopyXMLFromPackagesForPublish" BeforeTargets="PrepareForPublish">
	<ItemGroup>
		<PackageReferenceFiles Condition="%(PackageReference.CopyToOutputDirectory) != ''" Include="$(NugetPackageRoot)$([MSBuild]::Escape('%(PackageReference.Identity)').ToLower())/%(PackageReference.Version)/%(PackageReference.CopyToOutputDirectory)" />
	</ItemGroup>
	<Copy SourceFiles="@(PackageReferenceFiles)" DestinationFolder="$(PublishDir)" />
</Target>

blacksnake-rus avatar Apr 15 '20 13:04 blacksnake-rus

That means I have to do that for every nuget package and need to know if said nuget package has a netcoreapp3.1 folder... This means my customers (other developers) cannot use the package manager and have to manually edit the csproj for packages they add. Not a very good solution in my case.

To give a bit more context this is for a game engine, currently we have a integrated nuget (2.14) package manager that lets you install plugins (which are nuget packages) but in order to support the newer netstandard and netcore we want to upgrade nuget. Since the newer nuget contains many breaking changes that make this hard to do we are now looking how far we could get if we just used the default tooling (thus having all dependencies in a csproj).

Rick-van-Dam avatar Apr 15 '20 14:04 Rick-van-Dam

Also this approach doesn't work on Linux or docker containers.

Any news on this? Does anyone know how to transfer the csproj hack to support Linux or docker environments as well?

What we're currently doing is basically running a script during deployment to extract the NuGet XML content and put it next to the publish output. This solution is !!!terrible!!! but we don't have another one for now...

JoFrMueller avatar May 29 '20 13:05 JoFrMueller

@JoFrMueller you can do this to copy the xml and/or pdb files to the build output if it exists:

  <Target Name="CopyReferenceFiles" BeforeTargets="Build">
    <ItemGroup>
      <ReferenceFiles Include="%(Reference.RelativeDir)%(Reference.Filename).xml;%(Reference.RelativeDir)%(Reference.Filename).pdb" />
    </ItemGroup>
    
    <Message Text="Copying reference files to $(OutputPath)" Importance="High" />
    <Copy SourceFiles="@(ReferenceFiles)" DestinationFolder="$(OutputPath)" Condition="Exists('%(RootDir)%(Directory)%(Filename)%(Extension)')" />
  </Target>

Also see my blog about this https://the-photographing-programmer.com/packagereferences-and-xml-documentation/

Granted it would be nice if there was build in support for this common use case but this seems to work pretty well and takes into account transitive dependencies as well and doesn't lock your project into a specific targetframework.

We use this in our new project template for our game engine https://github.com/Barsonax/DualityProjectTemplate/blob/master/Source/Duality/Directory.Build.props#L12

Rick-van-Dam avatar May 29 '20 13:05 Rick-van-Dam

@Barsonax @JoFrMueller the solution can do the trick!

just for sharing infos, into build stage i have set: ENV NUGET_XMLDOC_MODE=none into linux image the dotnet restore wasn't able to pick up all xml files.

AntoCanza avatar May 29 '20 16:05 AntoCanza

Any timeframe when this is getting fixed? Its already been 2 years ago this issue was opened and is a breaking change compared to the old system.

Try this:

<ItemGroup>
	<PackageReference Include="MyNuget" Version="0.0.2526">
		<CopyToOutputDirectory>lib/netcoreapp3.1/*.xml</CopyToOutputDirectory>
	</PackageReference>
</ItemGroup>

<Target Name="CopyXMLFromPackagesForBuild" AfterTargets="Build">
	<ItemGroup>
		<PackageReferenceFiles Condition="%(PackageReference.CopyToOutputDirectory) != ''" Include="$(NugetPackageRoot)$([MSBuild]::Escape('%(PackageReference.Identity)').ToLower())/%(PackageReference.Version)/%(PackageReference.CopyToOutputDirectory)" />
	</ItemGroup>
	<Copy SourceFiles="@(PackageReferenceFiles)" DestinationFolder="$(OutDir)" />
</Target>

<Target Name="CopyXMLFromPackagesForPublish" BeforeTargets="PrepareForPublish">
	<ItemGroup>
		<PackageReferenceFiles Condition="%(PackageReference.CopyToOutputDirectory) != ''" Include="$(NugetPackageRoot)$([MSBuild]::Escape('%(PackageReference.Identity)').ToLower())/%(PackageReference.Version)/%(PackageReference.CopyToOutputDirectory)" />
	</ItemGroup>
	<Copy SourceFiles="@(PackageReferenceFiles)" DestinationFolder="$(PublishDir)" />
</Target>

it worked for me! ty ❤️

Logikoz avatar Jul 15 '21 05:07 Logikoz

@JoFrMueller you can do this to copy the xml and/or pdb files to the build output if it exists:

  <Target Name="CopyReferenceFiles" BeforeTargets="Build">
    <ItemGroup>
      <ReferenceFiles Include="%(Reference.RelativeDir)%(Reference.Filename).xml;%(Reference.RelativeDir)%(Reference.Filename).pdb" />
    </ItemGroup>
    
    <Message Text="Copying reference files to $(OutputPath)" Importance="High" />
    <Copy SourceFiles="@(ReferenceFiles)" DestinationFolder="$(OutputPath)" Condition="Exists('%(RootDir)%(Directory)%(Filename)%(Extension)')" />
  </Target>

Also see my blog about this https://the-photographing-programmer.com/packagereferences-and-xml-documentation/

Granted it would be nice if there was build in support for this common use case but this seems to work pretty well and takes into account transitive dependencies as well and doesn't lock your project into a specific targetframework.

We use this in our new project template for our game engine https://github.com/Barsonax/DualityProjectTemplate/blob/master/Source/Duality/Directory.Build.props#L12

how to filter xml? I just want to copy file start with xxx (xxx.*.xml), like xxx.Application.xml

LGinC avatar Oct 29 '21 07:10 LGinC

@LGinC like this, just replace the placeholders:

  <Target Name="CopyXmlDocs" BeforeTargets="Build">
    <ItemGroup>
      <XmlDocs Include="%(Reference.RelativeDir)your_filename_1.xml" />
      <XmlDocs Include="%(Reference.RelativeDir)*.your_partial_filename_2.xml" />
    </ItemGroup>
    <Message Text="Copying XML docs to $(OutputPath)" Importance="High" />
    <Copy SourceFiles="@(XmlDocs)" DestinationFolder="$(OutputPath)" Condition="Exists(%(FullPath))" />
  </Target>

nkoudelia avatar Dec 02 '21 09:12 nkoudelia

@LGinC like this, just replace the placeholders:

  <Target Name="CopyXmlDocs" BeforeTargets="Build">
    <ItemGroup>
      <XmlDocs Include="%(Reference.RelativeDir)your_filename_1.xml" />
      <XmlDocs Include="%(Reference.RelativeDir)*.your_partial_filename_2.xml" />
    </ItemGroup>
    <Message Text="Copying XML docs to $(OutputPath)" Importance="High" />
    <Copy SourceFiles="@(XmlDocs)" DestinationFolder="$(OutputPath)" Condition="Exists(%(FullPath))" />
  </Target>

dotnet publish -c Release not copy xml to /bin/Release/net5.0/publish folder, just copy to /bin/Release/net5.0 folder. change $(OutputPath) to $(PublishDir), and it work @nkoudelia thx

<Target Name="CopyXmlDocs" BeforeTargets="Build">
    <ItemGroup>
      <XmlDocs Include="%(Reference.RelativeDir)My.*.xml" />
    </ItemGroup>
    <Message Text="Copying XML docs to $(OutputPath)" Importance="High" />
    <Copy SourceFiles="@(XmlDocs)" DestinationFolder="$(PublishDir)" Condition="Exists(%(FullPath))" />
  </Target>

copy My.*.xml to publish folder

LGinC avatar Dec 17 '21 09:12 LGinC

In addition to already promoted workarounds: A generic copy for all xml files for all assemblies in build directory

  <Target Name="CopyReferenceFiles" BeforeTargets="Build">
    <ItemGroup>
      <XmlReferenceFiles Condition="Exists('$(OutputPath)%(Filename).dll')" Include="%(Reference.RelativeDir)%(Reference.Filename).xml" />
    </ItemGroup>

    <Message Text="Copying reference files to $(OutputPath)" Importance="High" />
    <Copy SourceFiles="@(XmlReferenceFiles)" DestinationFolder="$(OutputPath)" Condition="Exists('%(RootDir)%(Directory)%(Filename)%(Extension)')" />
  </Target>

lichti81 avatar Jun 14 '22 10:06 lichti81

More or less all workaround I've tried nothing worked in the Azure Pipeline. Is there any other workaround that works in the Azure Pipeline?

MofaggolHoshen avatar Aug 03 '22 07:08 MofaggolHoshen

More or less all workaround I've tried nothing worked in the Azure Pipeline. Is there any other workaround that works in the Azure Pipeline?

I've used CopyFile (CopyFiles@2) task

MofaggolHoshen avatar Aug 03 '22 11:08 MofaggolHoshen