Embedding C# sources in PDB with new csproj
Asked Answered
I

3

11

The recently-released .NET tooling seem to have support for embedding C# in PDBs, which should improve the experience of stepping into third-party, etc. Running csc /?, I can clearly see the /embed option: "Embed all source files in the PDB."

However, there doesn't seem to be any way to specify this in csproj. What's more, there doesn't seem to be any provisions for passing arbitrary switches to the compiler, which I would use to manually pass /embed.

Can anyone confirm that I haven't missed anything and that build support for /embed is currently lacking? Is there an issue for this (and if not where would it go)? Any suggested workaround would be appreciated too.

Igniter answered 10/3, 2017 at 9:7 Comment(1)
@HansPassant I think you're confusing things... The UI option you describe embeds the PDB in the assembly, and corresponds to /debug:embedded. I'm looking for embedding the sources in the PDB, which is what the /embed switch does. They're two different things.Igniter
A
15

There's now a proper MSBuild EmbedAllSources property:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>netstandard2.0</TargetFramework>
    <EmbedAllSources>true</EmbedAllSources>
    [...]

From what I observed locally, it behaves the same as the mentioned EmbedFiles target.

Alimony answered 29/5, 2018 at 9:25 Comment(3)
This worked for us, thank you so much! Note that you need Visual Studio 15.5+ in order to debug such a nupkg.Slip
@Slip Can you elaborate on any additional steps you needed to take (if any) to actually get VS to debug into such a nupkg? For VS 2022 it seems to be completely ignoring the fact that PDB and sources are embedded in a nuget package.Weathersby
@Weathersby sorry I haven't worked with this stack in quite a while so I don't know - perhaps it merits a new SO question?Slip
P
6

Looks like the roslyn task should support them via the EmbeddedFiles Item Group, by adding this to your .csproj:

<Target Name="EmbedSources" BeforeTargets="CoreCompile">
   <ItemGroup>
      <EmbeddedFiles Include="@(Compile) " />
   </ItemGroup>
</Target>

... which is basically what the /embed option does.

You probably need to also provide a SourceLink json file, to wire up the sources in the PDB, not sure that happens automatically.

Posthumous answered 13/3, 2017 at 8:44 Comment(10)
Aside from not wanting to hack this thing myself, I don't think the SourceLink compiler option is exposed to msbuild any more than /embed...Igniter
It is exposed... You should be able to set it via <Csc SourceLink="your.json" /> or <PropertyGroup><SourceLink>your.json</SourceLink></PropertyGroup>Posthumous
Ah, thanks for that, I wasn't aware... If you already have some experience with this feature, would you care to post a sample of the SourceLink JSON required for the case of embedded source files?Igniter
some sourcelink example here, sourcelink feature discussion in the roslyn repoPosthumous
Thanks for the links, will try. Note: You're missing parentheses in @Compile in your sample.Igniter
I saw something that indicated you don't need a SourceLink file when you embed the source files, but I have yet to see this work in a debugger. DotPeek seems to confirm my sources are there and will find them at least for some files. Anybody have this working or know if it won't work in either VS2017 or Code?Putandtake
@Putandtake you probably mean <DebugType>embedded</DebugType>, which allows you to embed the pdb info and sourcelink into the dllPosthumous
Well the combination of that and the above: embedding the portable pdb, and using EmbeddedFiles to embed the source in that PDB. I'm not seeing VS2017 or Code actually find the sources. I've looked everywhere, but I haven't seen any definitive statement that this is supported (on the debugger side) or that anybody has actually seen it work. I've been able to make SourceLink to github work in the debugger, but not embedding the source files.Putandtake
@Putandtake report the issue in the dotnet/roslyn repo, the original proposal thread is herePosthumous
Yeah, that was my next step, I was just hoping somebody would say - oh you just missed doing X then it'll work... And thanks for this answer, I was the thumbs up on the OP's comment there :)Putandtake
N
0

I faced this when run the Teamcity pipelines, I changed the DebugType tag with portable in related proj file. From that can ensure your project is configured to generate PDB files during compilation.

<PropertyGroup>
  <DebugType>portable</DebugType>
</PropertyGroup>
Newsmagazine answered 19/1, 2024 at 5:9 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.