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.

Shay Rojansky
  • 15,357
  • 2
  • 40
  • 69
  • 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. – Shay Rojansky Mar 14 '17 at 23:38

2 Answers2

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.

Lukáš Lánský
  • 4,641
  • 3
  • 32
  • 48
  • 1
    This worked for us, thank you so much! Note that you need Visual Studio 15.5+ in order to debug such a nupkg. – chrishiestand Aug 23 '18 at 23:17
  • @chrishiestand 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. – user9057586 Jul 15 '22 at 20:45
  • @user9057586 sorry I haven't worked with this stack in quite a while so I don't know - perhaps it merits a new SO question? – chrishiestand Jul 19 '22 at 17:11
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.

m0sa
  • 10,712
  • 4
  • 44
  • 91
  • 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... – Shay Rojansky Mar 14 '17 at 23:39
  • 2
    It is exposed... You should be able to set it via [``](https://github.com/dotnet/roslyn/blob/version-2.0.0/src/Compilers/Core/MSBuildTask/ManagedCompiler.cs#L106) or [`your.json`](http://source.roslyn.io/#MSBuildFiles/C/Users/vslsnap/.nuget/packages/Microsoft.Net.Compilers/2.0.0-rc2-61102-09/tools/Microsoft.CSharp.Core.targets,141) – m0sa Mar 14 '17 at 23:49
  • 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? – Shay Rojansky Mar 15 '17 at 06:04
  • some [sourcelink example here](https://github.com/chuckries/SourceLinkTest), [sourcelink feature discussion in the roslyn repo](https://github.com/dotnet/roslyn/issues/12759) – m0sa Mar 15 '17 at 06:31
  • Thanks for the links, will try. Note: You're missing parentheses in @Compile in your sample. – Shay Rojansky Mar 15 '17 at 06:39
  • 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? – jdasilva Apr 08 '17 at 00:08
  • @jdasilva you probably mean `embedded`, which allows you to embed the pdb info and sourcelink into the dll – m0sa Apr 08 '17 at 06:29
  • 1
    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. – jdasilva Apr 10 '17 at 04:22
  • @jdasilva report the issue in the dotnet/roslyn repo, the original proposal thread is [here](https://github.com/dotnet/roslyn/issues/12625) – m0sa Apr 10 '17 at 06:34
  • 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 :) – jdasilva Apr 10 '17 at 16:49