Include pdb files into my nuget (nupkg) files

C#Visual StudioMsbuildNuget

C# Problem Overview


I am using MSBuild to generate my nuget packages.

Is there any command I need to set, to allow it to include my .pdb files, for stepping into the source while debugging?

I do not want the source files to be included into the project that is pulling in the nuget package.

C# Solutions


Solution 1 - C#

If you are using VS2017 15.4 or later, you can define a MSBuild property in your project file

<AllowedOutputExtensionsInPackageBuildOutputFolder>$(AllowedOutputExtensionsInPackageBuildOutputFolder);.pdb</AllowedOutputExtensionsInPackageBuildOutputFolder>

This is discussed in NuGet #4142

However, there is still an issue as the new project system does not copy the pdbs from packages to the bin/publish folder for .NET Core 3.0+, a good summary is also at sourcelink/#628

Currently this is not planned to be fixed until .NET 6 :-(

Solution 2 - C#

While it may not help for debugging, it's definitely useful to include .pdb files so that stack traces have line numbers.

In the nuspec file, include a <files> element (child of <package>, sibling of <metadata>). This is what I have in one of my class libraries:

<files>
    <file src="bin\$configuration$\$id$.pdb" target="lib\net452\" />
</files>

Make sure the target is the same folder as where your .dll file is put in the package.

Solution 3 - C#

With the .csproj format of .NET Core NuGet creation is much easier, since MSBuild does most of the work.

In order to include your pdb files you just have to enter the tag <IncludeSymbols>true</IncludeSymbols> in a PropertyGroup in your project's .csproj file.

This will create an additional .symbols.nupkg package, which you can release to your [debug] feed.

Old .NET framework files can be easily mapped to the new csproj with open source libs, like hvanbakel's repo

Solution 4 - C#

This can also be accomplished with the dotnet CLI.

By packaging with

dotnet pack --include-symbols --include-source [path-to-project-here]

I get full debugging on the packages I generated

Solution 5 - C#

The approach that worked for me was adding the PDB as content which had the benefit it will be copied alongside the DLL. (PackageCopyToOutput was required)

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFramework>netstandard2.0</TargetFramework>
    <AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath>
    <GenerateDocumentationFile>true</GenerateDocumentationFile>
    <EmbedAllSources>true</EmbedAllSources>
  </PropertyGroup>

  <ItemGroup>
    <Content Include="bin\$(Configuration)\$(AssemblyName).pdb" PackagePath="contentFiles\any\netstandard2.0\$(AssemblyName).pdb">
       <PackageCopyToOutput>true</PackageCopyToOutput>
    </Content>
  </ItemGroup>
</Project>

EmbedAllSources - Will include source code in the PDB for easier debugging.

You may also want to consider setting "Optimize" to false for improving debugging experience in release configuration.

Solution 6 - C#

Refer to this link. Actually you should add -Symbols to the end of command to create a symbols package. You shouldn't add pdb files to main nuget package.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionmonstertjie_zaView Question on Stackoverflow
Solution 1 - C#Paul HatcherView Answer on Stackoverflow
Solution 2 - C#Jeff SheplerView Answer on Stackoverflow
Solution 3 - C#Josef GinermanView Answer on Stackoverflow
Solution 4 - C#farlee2121View Answer on Stackoverflow
Solution 5 - C#Tal AloniView Answer on Stackoverflow
Solution 6 - C#MiladView Answer on Stackoverflow