Skip to content

DAP support C#

Zeioth edited this page Aug 30, 2023 · 55 revisions

In order to debug your programs, you have to compile them in debug mode as described next

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 the necessary runtimeconfig.json file. So the only thing you have to do is to insert a breakpoint on neovim, open DAP, and select the .dll file that has been generated along with your .exe.

For more info read DAP wiki.

Alternative: For regular C# projects

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

[HelloWorld]
entry_point = "/path/to/my/entry_point_file/main.cs"
output = "/path/where/the/program/will/be/written/hello_world"
arguments = "/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 non dotnet 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 with Build solution, 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#.

Important for you to know

  • Note that mason installs the debugger for you. You don't need to install any system dependency.
  • If you find any issue while configuring DAP, run :DapSetLogLevel trace and :DapShowLog to find the reason.
  • Update 11/08/2023: Programs compile in debug mode by default now. But if you are defining your own arguments in the .solution.toml file of your project, you will still have to set the /debug argument manually, as defining your own arguments will overwrite the default ones.