Skip to content

DAP support C#

Zeioth edited this page Aug 6, 2023 · 55 revisions

The easy way: Using dotnet

This is only available if your project has a .csproj file. Compiling with the options Build and run dotnet or Build dotnet will automatically generate a .exe in debug mode, and the necessary runtimeconfig.json file. So the only thing you have to do to open it with DAP is selecting the generated .dll file.

For more info on this step see DAP wiki.

Alternative: For regular C# projects

Create a .solution file in your working directory. Then pass the -debug parameter to the compiler like this

[HELLO WORLD]
entry_point="/path/to/my/entry_point_file/main.c"
output="/path/where/the/program/will/be/written/hello_world"
parameters="-debug"

After compiling your program you will see the file <your_exe_name>.pdb beside your executable. This proves you are compiling in debug mode.

How to setup DAP to debug with C#

Please note that this section has nothing to do with compiler.nvim. I'm documenting this to make your life easier. To debug C# with DAP you have to:

  • Install the package netcoredbg in your system
  • Setup DAP for C#
  • Create the file <your_exe_name>.runtimeconfig.json in your executable directory. For example if your program is Program.exe, name it Program.runtimeconfig.json.
{
    "runtimeOptions": {
        "configProperties": {
            "System.GC.Server": true,
            "System.GC.Concurrent": true,
            "System.Threading.ThreadPool.MinThreads": 1,
            "System.Threading.ThreadPool.MaxThreads": 1
        },
        "tfm": "netcoreapp2.1",
        "framework": {
            "name": "Microsoft.NETCore.App",
            "version": "7.0.9"
        },
        "applyPatches": true,
        "rollForwardOnNoCandidateFx": 1
    }
}

But instead of version 7.0.9, use the Microsoft.NETCore.App version you have installed in your system. On Arch Linux you can check the directory /usr/share/dotnet/shared/Microsoft.NETCore.App/ to discover it.

How to check if everything works

Compile your program, add a break point to your code, and then run DAP. You will see something like this.

screenshot_2023-08-01_00-12-29_277199356

Congratulations, now you can compile and debug C#.

Troubleshooting

  • If you find any issue while configuring DAP, run :DapSetLogLevel trace and :DapShowLog to find the reason.