Skip to content

Commit 8017c63

Browse files
committed
Use a customRequest rather than StopDebugger for dotnet detach
1 parent a9a38a8 commit 8017c63

File tree

1 file changed

+22
-19
lines changed

1 file changed

+22
-19
lines changed

src/features/DebugSession.ts

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -375,35 +375,38 @@ export class DebugSessionFeature extends LanguageClientConsumer
375375
dotnetAttachConfig.processId = pid;
376376

377377
// Ensure the .NET session stops before the PowerShell session so that the .NET debug session doesn't emit an error about the process unexpectedly terminating.
378-
const startDebugEvent = debug.onDidStartDebugSession((dotnetAttachSession) => {
378+
let tempConsoleDotnetAttachSession: DebugSession;
379+
const startDebugEvent = debug.onDidStartDebugSession(dotnetAttachSession => {
380+
if (dotnetAttachSession.configuration.name != dotnetAttachConfig.name) {return;}
381+
379382
// Makes the event one-time
380383
// HACK: This seems like you would be calling a method on a variable not assigned yet, but it does work in the flow.
381384
// The dispose shorthand demonry for making an event one-time courtesy of: https://github.com/OmniSharp/omnisharp-vscode/blob/b8b07bb12557b4400198895f82a94895cb90c461/test/integrationTests/launchConfiguration.integration.test.ts#L41-L45
382385
startDebugEvent.dispose();
386+
383387
this.logger.writeVerbose(`Debugger session detected: ${dotnetAttachSession.name} (${dotnetAttachSession.id})`);
384388

385-
// HACK: As of 2023-08-17, there is no vscode debug API to request the C# debugger to detach, so we send it a custom DAP request instead.
386-
if (dotnetAttachSession.configuration.name == dotnetAttachConfig.name) {
387-
const onTerminateAttachSession = debug.onDidTerminateDebugSession(async parentSession => {
388-
// HACK: Force the "this" on this since we are supplying a "this" to the function via an argument. There probably is a type-safe way to do this, but because we also need a self-reference to the function in order to make it a one-time action, I'm not sure how to do it.
389-
const dotnetAttachSession = this as unknown as DebugSession;
389+
tempConsoleDotnetAttachSession = dotnetAttachSession;
390390

391-
if (parentSession.parentSession?.id !== dotnetAttachSession.id) {return;}
391+
const stopDebugEvent = debug.onDidTerminateDebugSession( async tempConsoleSession => {
392+
if (tempConsoleDotnetAttachSession.parentSession?.id !== tempConsoleSession.id) {return;}
392393

393-
this.logger.writeVerbose(`Powershell Binary Debug Session Stop Detected: ${dotnetAttachSession.name} (${dotnetAttachSession.id}), detaching dotnet debugger.`);
394+
// Makes the event one-time
395+
stopDebugEvent.dispose();
394396

395-
await dotnetAttachSession.customRequest(
396-
"disconnect",
397-
{
398-
restart: false,
399-
terminateDebuggee: false,
400-
suspendDebuggee: false
401-
}
402-
);
397+
this.logger.writeVerbose(`Debugger session terminated: ${tempConsoleSession.name} (${tempConsoleSession.id})`);
403398

404-
onTerminateAttachSession.dispose();
405-
}, session);
406-
}});
399+
// HACK: As of 2023-08-17, there is no vscode debug API to request the C# debugger to detach, so we send it a custom DAP request instead.
400+
await dotnetAttachSession.customRequest(
401+
"disconnect",
402+
{
403+
restart: false,
404+
terminateDebuggee: false,
405+
suspendDebuggee: false
406+
}
407+
);
408+
});
409+
});
407410

408411
// Start a child debug session to attach the dotnet debugger
409412
// TODO: Accommodate multi-folder workspaces if the C# code is in a different workspace folder

0 commit comments

Comments
 (0)