Skip to content

Commit 1d69366

Browse files
authored
Add Helpers to aide Debugging MSBuild tasks (dotnet#20695)
One thing that is very useful is the ability to debug your Tasks while they are being run on a build process. This is possible thanks to the `MSBUILDDEBUGONSTART` environment variable. When set to `2` this will force MSBuild to wait for a debugger connection before continuing. You will see the following prompt. ```dotnetcli Waiting for debugger to attach (dotnet PID 13001). Press enter to continue... ``` You can then use VS or VSCode to attach to this process and debug you tasks. This commit adds code helper tasks.json and launch.json entires to help make this debugging process a bit easier. You can start your test app with the `dotnet-local` script (so it uses your maui build) ```dotnetcli MSBUILDDEBUGONSTART=2 ~/<some maui checkout>/dotnet-local.sh build -m:1 ``` ```dotnetcli MSBUILDDEBUGONSTART=2 ~/<some maui checkout>/dotnet-local.sh build -m:1 ``` ```dotnetcli set MSBUILDDEBUGONSTART=2 ~/<some maui checkout>/dotnet-local.cmd build -m:1 ``` --- Note: the `-m:1` is important as it restricts MSBuild to 1 node. Once MSBuild starts it will print the following ```dotnetcli Waiting for debugger to attach (dotnet PID xxxx). Press enter to continue... ``` You need to copy the PID value so we can use this in the IDE. For Visual Studio you can use the `Attach to Process` menu option, while you have the Microsoft.Maui.sln solution open. For VSCode open the workspace then use the `Attach to Process` Run and Debug option. You will be prompted for the PID and it will then connect. Once connected go back to your command prompt and press ENTER so that the MSBuild process can continue. You will be able to set breakpoints in Tasks (but not Targets) and step through code from this point on. If you want to test in-tree in VSCode the `Build Platform Sample` command will ask you if you want to debug MSBuild tasks and fill in the `MSBUILDDEBUGONSTART` for you. The PID text will appear in the `Terminal` window in VSCode. You can then use the `Attach to Process` Run and Debug option to attach to the process.
1 parent 0c02e4b commit 1d69366

File tree

3 files changed

+84
-1
lines changed

3 files changed

+84
-1
lines changed

.github/DEVELOPMENT.md

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,57 @@ dotnet build src\DotNet\DotNet.csproj
301301
dotnet cake --target=VS
302302
```
303303
304+
## Debugging MSBuild Tasks using VS/VSCode
305+
306+
One thing that is very useful is the ability to debug your Tasks while
307+
they are being run on a build process. This is possible thanks to the
308+
`MSBUILDDEBUGONSTART` environment variable. When set to `2` this will
309+
force MSBuild to wait for a debugger connection before continuing.
310+
You will see the following prompt.
311+
312+
```dotnetcli
313+
Waiting for debugger to attach (dotnet PID 13001). Press enter to continue...
314+
```
315+
316+
You can then use VS or VSCode to attach to this process and debug you tasks.
317+
You can start your test app with the `dotnet-local` script (so it uses your maui build)
318+
319+
### [MacOS](#tab/macos)
320+
321+
```dotnetcli
322+
MSBUILDDEBUGONSTART=2 ~/<some maui checkout>/dotnet-local.sh build -m:1
323+
```
324+
325+
### [Linux](#tab/linux)
326+
327+
```dotnetcli
328+
MSBUILDDEBUGONSTART=2 ~/<some maui checkout>/dotnet-local.sh build -m:1
329+
```
330+
331+
### [Windows](#tab/windows)
332+
333+
```dotnetcli
334+
set MSBUILDDEBUGONSTART=2
335+
~/<some maui checkout>/dotnet-local.cmd build -m:1
336+
```
337+
338+
---
339+
340+
Note: the `-m:1` is important as it restricts MSBuild to 1 node.
341+
342+
Once MSBuild starts it will print the following
343+
344+
```dotnetcli
345+
Waiting for debugger to attach (dotnet PID xxxx). Press enter to continue...
346+
```
347+
348+
You need to copy the PID value so we can use this in the IDE. For Visual Studio you can use the `Attach to Process` menu option, while you have the Microsoft.Maui.sln solution open. For VSCode open the workspace then use the `Attach to Process` Run and Debug option. You will be prompted for the PID and it will then connect.
349+
350+
Once connected go back to your command prompt and press ENTER so that the MSBuild process can continue.
351+
352+
You will be able to set breakpoints in Tasks (but not Targets) and step through code from this point on.
353+
354+
If you want to test in-tree in VSCode the `Build Platform Sample` command will ask you if you want to debug MSBuild tasks and fill in the `MSBUILDDEBUGONSTART` for you. The PID text will appear in the `Terminal` window in VSCode. You can then use the `Attach to Process` Run and Debug option to attach to the process.
304355
305356
## Stats
306357

.vscode/launch.json

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
// Use IntelliSense to learn about possible attributes.
3+
// Hover to view descriptions of existing attributes.
4+
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
5+
"version": "0.2.0",
6+
"configurations": [
7+
{
8+
"name": "Attach to Process",
9+
"type": "coreclr",
10+
"request": "attach",
11+
"processId": "${input:processid}"
12+
}
13+
],
14+
"inputs": [
15+
{
16+
"id": "processid",
17+
"type": "promptString",
18+
"default": "0",
19+
"description": "Enter dotnet build process id reported when setting the env var MSBUILDDEBUGONSTART=2",
20+
}
21+
]
22+
}

.vscode/tasks.json

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
{
1717
"label": "Build Platform Sample",
1818
"type": "shell",
19-
"command": "./bin/dotnet/dotnet build ${input:project} -c ${input:configuration}",
19+
"command": "${input:debugbuildtasks} ./bin/dotnet/dotnet build ${input:project} -c ${input:configuration}",
2020
"group": {
2121
"kind": "build",
2222
"isDefault": true
@@ -60,6 +60,16 @@
6060
"net7.0-ios",
6161
]
6262
},
63+
{
64+
"id": "debugbuildtasks",
65+
"type": "pickString",
66+
"default": "",
67+
"description": "Debug Build Tasks?",
68+
"options": [
69+
"",
70+
"MSBUILDDEBUGONSTART=2"
71+
]
72+
},
6373
{
6474
"id": "attach",
6575
"type": "pickString",

0 commit comments

Comments
 (0)