runtime: Debugging not working with self-contained single file .NET6 assemblies

Description

When I publish a .NET assembly with the following command, there are no debug symbols available in VS2022. And trying to load the PDB manually leads to “image mismatch” errors or “can’t find matching debug symbols”.

This is the publish command:

dotnet publish -c Debug ./path/to/project.csproj -p:PublishSingleFile=true -p:IncludeAllContentForSelfExtract=true -r win-x64 --nologo --self-contained -o ./path/to/output

However if I set self-contained to false, it just works: --self-contained=false.

The problem occurs regardless if the debug symbols are embedded or stored as a PDB file.

I hope this is the right place. I know VS2022 is the one which can’t load the debug symbols but I am pretty sure it’s about the debug symbol creation/connection and not the loading. But I might be wrong.

Others seem to have the same problems. Look here: https://stackoverflow.com/questions/64897623/no-symbols-when-attaching-to-net-core-program-single-file/73796666#73796666.

Configuration

  • dotnet 6 (but seems to be a problem since netcore already)
  • tested on Win7 x64 but I guess the OS doesn’t matter here
  • x64 in this case
  • Debug config of course

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Comments: 22 (10 by maintainers)

Most upvoted comments

It should work with VS Community with no issues. Please try disabling Just My Code under Debug->Options.

Hope this helps!

Disabled Just My Code it’s working now as expect. Sorry and thank you very much for your great help and time.

Then hit F10 to start the program with the instruction pointer in the first line in the program:

When I hit F10 the app run and ends without give any chance to set a breakpoint. I also open the C# file in the EXE project and set the breakpoint in the first line in the program but the debugger don’t stop on it. It’s due I have the Microsoft Visual Studio Community 2022 (64-bit) - Current - Version 17.10.5?

It should work with VS Community with no issues. Please try disabling Just My Code under Debug->Options.

Hope this helps!

The problem here is some functionality doesn’t work with a NativeAOT published executable and I need to debug to identify them and using another alternatives, such as source generators. What the alternative to I could use to debug it?

You can use native AOT publish debug configuration:

image

Then similar to .NET core singlefile, drag the EXE into VS to create a VS project, then use the Native debug engine to debug (which I believe will be the default for this app type):

image

You should find that symbols load for the EXE and that you can set breakpoints in the program, view locals, and observe the callstack:

image

Note that the debug experience won’t be the same as .NET Core because the app has been compiled to native code, so things like C# expression evaluation and async stepping won’t work. Please see https://learn.microsoft.com/en-us/dotnet/core/deploying/native-aot/diagnostics#native-debugging for more information.

Hope this helps!

Do you mean System.Diagnostics.Debugger.IsAttached does not return true if a native debugger is attached to the app, or that you were not able to use the Managed (.NET Core, .NET 5+) debug engine with Native AOT?

I meant System.Diagnostics.Debugger.IsAttached does not return true if a native debugger is attached to the app.

Thank you very much for your excellent explanation.

I was using the System.Diagnostics.Debugger.IsAttached but that isn’t working on native Aot.

Do you mean System.Diagnostics.Debugger.IsAttached does not return true if a native debugger is attached to the app, or that you were not able to use the Managed (.NET Core, .NET 5+) debug engine with Native AOT? Note that Native AOT publish is off by default in VS and is only enabled if clicking the “Enable native AOT publish” option when creating the project:

image

When the publish for NativeAOT checkbox is set, the csproj will have the following line in it:

<PublishAot>true</PublishAot>

To use managed debugging on a published app, that line would need to be removed.

It seems that publishing from the VS2022 using a publish profile doesn’t have the same behavior as like using the dotnet publish command line, including the path which is different.

It’s possible to configure VS publishing to produce a single-file app:

image

Drag and drop the EXE into VS to create a VS EXE project: image

Configure the Debugger engine for Managed (.NET Core, .NET 5+) image

Then hit F10 to start the program with the instruction pointer in the first line in the program:

image

And for debugging the same way in WSL using linux-64, it’s also possible?

Yes:

The only way I could manage the debug it was put at start of the Program.cs file the instruction Console.Read();. After I click on Start the console is waiting for input but still the Program.cs file doesn’t open. I had to click on pause for the Program.cs file show up.

One way is to use File->Open and navigate to the proper C# file and open it, then set a breakpoint at the desired location.
Another other way is to simply start the program with F10 (step) instead of F5 (go). F10 will start the program in the first line of the program.
And yet another way is to set a function breakpoint to stop in the desired function, automatically loading the sources for it. For example:

using System;

public class Foo
{
    public static void Main()
    {
        Console.WriteLine("Hello, World!");
    }
}

image

image

It sounds like the original problem reported in this issue is resolved, so I will go ahead and close this issue.

Hope this helps!!

no. it is not new. there are a lot of topics for 1-2 years

@VSadov I assume this is the new Windows API problems showing up