Skip to content

Commit 4dbd961

Browse files
Wait for attach complete (#6415)
This PR adds code to the Roslyn unit test debugging code to wait for attach complete.
1 parent a9fd50e commit 4dbd961

File tree

1 file changed

+52
-5
lines changed

1 file changed

+52
-5
lines changed

src/lsptoolshost/roslynLanguageServer.ts

Lines changed: 52 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ import { registerRazorCommands } from './razorCommands';
5454
import { registerOnAutoInsert } from './onAutoInsert';
5555
import { commonOptions, languageServerOptions, omnisharpOptions } from '../shared/options';
5656
import { NamedPipeInformation } from './roslynProtocol';
57+
import { IDisposable } from '../disposable';
5758

5859
let _channel: vscode.OutputChannel;
5960
let _traceChannel: vscode.OutputChannel;
@@ -615,23 +616,69 @@ export class RoslynLanguageServer {
615616
);
616617
}
617618

619+
// eslint-disable-next-line @typescript-eslint/promise-function-async
620+
private WaitForAttachCompleteAsync(attachRequestId: string): Promise<boolean> {
621+
return new Promise<boolean>((resolve) => {
622+
let didTerminateRegistation: IDisposable | null = null;
623+
let customEventReg: IDisposable | null = null;
624+
let isComplete = false;
625+
626+
const fire = (result: boolean) => {
627+
if (isComplete === false) {
628+
isComplete = true;
629+
didTerminateRegistation?.dispose();
630+
customEventReg?.dispose();
631+
resolve(result);
632+
}
633+
};
634+
635+
didTerminateRegistation = vscode.debug.onDidTerminateDebugSession((session: vscode.DebugSession) => {
636+
if (session.configuration.attachRequestId !== attachRequestId) {
637+
return;
638+
}
639+
640+
fire(false);
641+
});
642+
643+
customEventReg = vscode.debug.onDidReceiveDebugSessionCustomEvent(
644+
(event: vscode.DebugSessionCustomEvent) => {
645+
if (event.session.configuration.attachRequestId !== attachRequestId) {
646+
return;
647+
}
648+
649+
if (event.event !== 'attachComplete') {
650+
return;
651+
}
652+
653+
fire(true);
654+
}
655+
);
656+
});
657+
}
658+
618659
private registerDebuggerAttach() {
619660
this._languageClient.onRequest<RoslynProtocol.DebugAttachParams, RoslynProtocol.DebugAttachResult, void>(
620661
RoslynProtocol.DebugAttachRequest.type,
621662
async (request) => {
622663
const debugOptions = commonOptions.unitTestDebuggingOptions;
623664
const debugConfiguration: vscode.DebugConfiguration = {
624665
...debugOptions,
625-
name: '.NET Core Attach',
666+
name: '.NET Debug Unit test',
626667
type: 'coreclr',
627668
request: 'attach',
628669
processId: request.processId,
670+
attachRequestId: randomUUID(),
629671
};
630672

631-
const result = await vscode.debug.startDebugging(undefined, debugConfiguration, undefined);
632-
return {
633-
didAttach: result,
634-
};
673+
const waitCompletePromise = this.WaitForAttachCompleteAsync(debugConfiguration.attachRequestId);
674+
675+
let success = await vscode.debug.startDebugging(undefined, debugConfiguration, undefined);
676+
if (!success) {
677+
return { didAttach: false };
678+
}
679+
680+
success = await waitCompletePromise;
681+
return { didAttach: success };
635682
}
636683
);
637684
}

0 commit comments

Comments
 (0)