Skip to content

Commit fb64316

Browse files
committed
Add handlers to cleanup dotnet attach
1 parent 52f0793 commit fb64316

File tree

1 file changed

+27
-2
lines changed

1 file changed

+27
-2
lines changed

src/features/DebugSession.ts

+27-2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
DebugConfiguration,
1212
DebugConfigurationProvider,
1313
DebugSession,
14+
Disposable,
1415
ExtensionContext,
1516
WorkspaceFolder
1617
} from "vscode";
@@ -92,7 +93,7 @@ export class DebugSessionFeature extends LanguageClientConsumer
9293

9394
if (sessionDetails === undefined) {
9495
void this.logger.writeAndShowError(`PowerShell session details not available for ${session.name}`);
95-
return;
96+
return undefined;
9697
}
9798

9899
this.logger.writeVerbose(`Connecting to pipe: ${sessionDetails.debugServicePipeName}`);
@@ -149,6 +150,26 @@ export class DebugSessionFeature extends LanguageClientConsumer
149150
return undefined;
150151
}
151152

153+
// Wire up a handler to capture the dotnet session and stop it before PowerShell, otherwise it will emit an error that its process unexpectedly stopped.
154+
let dotnetAttachStartHandler: Disposable[] = []
155+
vscode.debug.onDidStartDebugSession((dotnetAttachSession) => {
156+
this.logger.write(`Debugger session detected: ${dotnetAttachSession.name} (${dotnetAttachSession.id})`);
157+
let dotnetAttachStopHandler: Disposable[] = [];
158+
if (dotnetAttachSession.configuration.name == dotnetDebuggerConfig.name) {
159+
vscode.debug.onDidTerminateDebugSession(async (terminatedDebugSession) => {
160+
this.logger.write(`Debugger session stopped: ${terminatedDebugSession.name} (${terminatedDebugSession.id})`);
161+
if (terminatedDebugSession === session) {
162+
this.logger.write("Terminating dotnet debugger session associated with PowerShell debug session");
163+
await vscode.debug.stopDebugging(dotnetAttachSession);
164+
}
165+
//This makes it a one time event
166+
dotnetAttachStopHandler.forEach((handler) => handler.dispose());
167+
}, undefined, dotnetAttachStopHandler);
168+
}
169+
//This makes it a one time event
170+
dotnetAttachStartHandler.forEach((handler) => handler.dispose());
171+
}, undefined, dotnetAttachStartHandler);
172+
152173
// Start a child debug session to attach the dotnet debugger
153174
// TODO: Accomodate multi-folder workspaces if the C# code is in a different workspace folder
154175
await vscode.debug.startDebugging(undefined, dotnetDebuggerConfig, session);
@@ -169,11 +190,15 @@ export class DebugSessionFeature extends LanguageClientConsumer
169190
}
170191

171192
// Default debugger config if none provided
193+
// TODO: Type this appropriately from the C# extension?
172194
return {
173195
name: "Dotnet Debugger: PSIC Temporary Console",
174196
type: "coreclr",
175197
request: "attach",
176-
processId: pid
198+
processId: pid,
199+
logging: {
200+
moduleLoad: false
201+
}
177202
};
178203
}
179204

0 commit comments

Comments
 (0)