Post

How to Step into .NET Core Source Code in VS Code

You probably know how to use Visual Studio to step into .NET Core source code, as Microsoft documented it very well. However, what if you are using Visual Studio Code?

In fact, the steps are also simple, so let’s simply check them out.

Sample Project

First, let’s create a test project,

1
2
3
4
mkdir test-console
cd test-console
dotnet new console
code .

Preparation in VS Code

Then VS Code is kind enough to ask if you want to add required assets to build and debug. Of course, we give it a Yes.

Note that this creates a .vscode folder with launch.json and tasks.json.

We can debug this sample project right now to ensure everything works.

Stepping into .NET Core Source Code

Time to reveal the secrets you wanted to know, and we open launch.json in VS Code.

Here you can see what are the initial settings that control the debug experience under configurations generated by VS Code,

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
{
    "version": "0.2.0",
    "configurations": [
        {
            // Use IntelliSense to find out which attributes exist for C# debugging
            // Use hover for the description of the existing attributes
            // For further information visit https://github.com/OmniSharp/omnisharp-vscode/blob/master/debugger-launchjson.md
            "name": ".NET Core Launch (console)",
            "type": "coreclr",
            "request": "launch",
            "preLaunchTask": "build",
            // If you have changed target frameworks, make sure to update the program path.
            "program": "${workspaceFolder}/bin/Debug/net7.0/test-console.dll",
            "args": [],
            "cwd": "${workspaceFolder}",
            // For more information about the 'console' field, see https://aka.ms/VSCode-CS-LaunchJson-Console
            "console": "internalConsole",
            "stopAtEntry": false
        },
        {
            "name": ".NET Core Attach",
            "type": "coreclr",
            "request": "attach"
        }
    ]
}

All we need is to add the required debugger settings to mimic the ones mentioned in Microsoft’s article for Visual Studio for Windows,

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
{
    "version": "0.2.0",
    "configurations": [
        {
            // Use IntelliSense to find out which attributes exist for C# debugging
            // Use hover for the description of the existing attributes
            // For further information visit https://github.com/OmniSharp/omnisharp-vscode/blob/master/debugger-launchjson.md
            "name": ".NET Core Launch (console)",
            "type": "coreclr",
            "request": "launch",
            "preLaunchTask": "build",
            // If you have changed target frameworks, make sure to update the program path.
            "program": "${workspaceFolder}/bin/Debug/net7.0/testconsole-net7.dll",
            "args": [],
            "cwd": "${workspaceFolder}",
            // For more information about the 'console' field, see https://aka.ms/VSCode-CS-LaunchJson-Console
            "console": "internalConsole",
            "stopAtEntry": false,
            // Below are the required settings to step into .NET Core source code
            "justMyCode": false,
            "suppressJITOptimizations": true,
            "sourceLinkOptions": {
                "*": {
                    "enabled": true
                }
            },
            "symbolOptions": {
                "searchPaths": [],
                "searchMicrosoftSymbolServer": true,
                "searchNuGetOrgSymbolServer": true
            }
        },
        {
            "name": ".NET Core Attach",
            "type": "coreclr",
            "request": "attach"
        }
    ]
}

With such settings, if we set a breakpoint at Console.WriteLine("Hello, World!");, we can finally step into Console.WriteLine,

1
2
3
4
5
        [MethodImplAttribute(MethodImplOptions.NoInlining)]
        public static void WriteLine(string? value)
        {
            Out.WriteLine(value);
        }

Happy debugging ;)

© Lex Li. All rights reserved. The code included is licensed under CC BY 4.0 unless otherwise noted.
Advertisement

© - Lex Li. All rights reserved.

Using the Chirpy theme for Jekyll.

Last updated on April 25, 2024