4

I'm trying to include PDB files of project references into Nuget package, by using dotnet pack command.

I've found solution to include project referenced DLL files into nuget package. This needs adding some code to .csproj file. I've also tried to make it work with .pdb files, but this doesn't work.

This code copy only *.dll files to nuget.

<Target Name="CopyProjectReferencesToPackage" DependsOnTargets="ResolveReferences">
  <ItemGroup>
    <BuildOutputInPackage Include="@(ReferenceCopyLocalPaths->WithMetadataValue('ReferenceSourceTarget', 'ProjectReference'))"/>
  </ItemGroup>
</Target>

This is what i tried, but *.pdb files aren't visible in nuget.

<Target Name="CopyProjectReferencesToPackage" DependsOnTargets="ResolveReferences">
  <ItemGroup>
    <BuildOutputInPackage Include="@(ReferenceCopyLocalPaths->WithMetadataValue('ReferenceSourceTarget', 'ProjectReference')->Replace('.dll', '.pdb'))"/>
  </ItemGroup>
</Target>

SOLUTION

I've fund solution to copy referenced PDB's- just add this lines to your .csproj file:

<Target Name="CopyPdbToPackage" Inputs="@(ReferenceCopyLocalPaths-&gt;WithMetadataValue('ReferenceSourceTarget', 'ProjectReference'))" Outputs="%(ProjectReference.Identity)" AfterTargets="CopyProjectReferencesToPackage">
    <PropertyGroup>
        <CurrentReference>%(ProjectReference.Identity)</CurrentReference>
        <CurrentReferenceName>$([System.IO.Path]::GetFileNameWithoutExtension($(CurrentReference)))</CurrentReferenceName>
    </PropertyGroup>

    <Message Text="Copying PDB of $(CurrentReferenceName) to packages..." Importance="high" Condition="'%(ProjectReference.NugetIgnore)'!='true'" />

    <ItemGroup>
        <AllItems Include="@(ReferenceCopyLocalPaths-&gt;WithMetadataValue('OriginalProjectReferenceItemSpec', '$(CurrentReference)'))" />
        <PdbFiles Include="%(AllItems.Identity)" Condition="@(AllItems-&gt;EndsWith('.pdb'))=='true'" />
    </ItemGroup>
    <ItemGroup>
        <TfmSpecificPackageFile Include="@(PdbFiles)" Condition="'%(ProjectReference.NugetIgnore)'!='true'">
            <PackagePath>lib/$(TargetFramework)</PackagePath>
        </TfmSpecificPackageFile>
    </ItemGroup>
</Target>

But there is one issue, the PDB of "main" nuget project is not copied...

valiano
  • 16,433
  • 7
  • 64
  • 79
Kamil Zemczak
  • 75
  • 1
  • 6

2 Answers2

5

The configuration below is actually a fix for your initial idea to transform a DLL path to a PDB path. The reason why PDB files didn't appear in a generated nuget package is that the output files get filtered further down the road (see AllowedOutputExtensionsInPackageBuildOutputFolder). Fortunately, we can customize what extensions should be kept.

See the final configuration below. It generates a package with a DLL/PDB of the compiling project plus DLLs/PDBs of referenced projects.

<PropertyGroup>
  <!--Include Project References output-->
  <TargetsForTfmSpecificBuildOutput>$(TargetsForTfmSpecificBuildOutput);CopyProjectReferencesToPackage</TargetsForTfmSpecificBuildOutput>

  <!--Allow certain extensions-->
  <AllowedOutputExtensionsInPackageBuildOutputFolder>$(AllowedOutputExtensionsInPackageBuildOutputFolder);.pdb</AllowedOutputExtensionsInPackageBuildOutputFolder>
</PropertyGroup>

<Target DependsOnTargets="ResolveReferences" Name="CopyProjectReferencesToPackage">
  <ItemGroup>
    <!--Include DLLs of Project References-->
    <BuildOutputInPackage Include="@(ReferenceCopyLocalPaths->WithMetadataValue('ReferenceSourceTarget', 'ProjectReference'))"/>

    <!--Include PDBs of Project References-->
    <BuildOutputInPackage Include="@(ReferenceCopyLocalPaths->WithMetadataValue('ReferenceSourceTarget', 'ProjectReference')->Replace('.dll', '.pdb'))"/>
  </ItemGroup>
</Target>
AndreyCh
  • 1,298
  • 1
  • 14
  • 16
0

You can force the command to contains the symbols.

--include-symbols

Final command for example:

dotnet pack -c Debug --include-symbols

This includes your *.pdb file in the package. You can read more about include symbols Here

Edit: My answer doesn't fulfill the question requirement. Please read the comments to this message.

andre
  • 113
  • 1
  • 10
  • 2
    Nope, it creates new nuget package with name *.symbols.nupkg, and this package does not contain project referenced *.pdb files, but only "parent" project .pdb. You can read more about include symbols [Here](https://learn.microsoft.com/en-us/nuget/create-packages/symbol-packages-snupkg) – Kamil Zemczak Jan 10 '19 at 14:06
  • Understood. I will try to find something. – andre Jan 10 '19 at 14:11
  • 1
    Look [Here](https://github.com/NuGet/Home/issues/3891#issuecomment-377319939). This code which copy DLLs is workaround of open feature issue. I'm asking, is there possibility to edit this workaround to copy PDBs also? – Kamil Zemczak Jan 10 '19 at 14:19
  • Really nice workaround but as you said, doesn't work for you because you want to include the *.pdb from the referenced projects. Sorry for misunderstood. [This comment](https://github.com/dotnet/cli/issues/3959#issuecomment-410482428) says all. :) And thanks for the information. – andre Jan 10 '19 at 14:29