Skip to content

Commit 5dcc6d7

Browse files
committed
WIP: Fix up debug config resolution stuff
Wow this was a mess.
1 parent 3ba25fe commit 5dcc6d7

File tree

3 files changed

+52
-48
lines changed

3 files changed

+52
-48
lines changed

package.json

+10-13
Original file line numberDiff line numberDiff line change
@@ -497,7 +497,7 @@
497497
"name": "PowerShell Attach Interactive Session Runspace",
498498
"type": "PowerShell",
499499
"request": "attach",
500-
"processId": "current"
500+
"processId": 0
501501
}
502502
},
503503
{
@@ -588,29 +588,26 @@
588588
"properties": {
589589
"computerName": {
590590
"type": "string",
591-
"description": "Optional: The computer name to which a remote session will be established. Works only on PowerShell 4 and above."
591+
"description": "Optional: The computer name to which a remote session will be established."
592592
},
593593
"processId": {
594-
"type": "string",
595-
"description": "The process id of the PowerShell host process to attach to. Works only on PowerShell 5 and above.",
596-
"default": null
594+
"type": "number",
595+
"description": "Optional: The ID of the PowerShell host process to attach to. A value of 0 (the default) means the Extension Terminal.",
596+
"default": 0
597597
},
598598
"runspaceId": {
599-
"type": [
600-
"string",
601-
"number"
602-
],
603-
"description": "Optional: The ID of the runspace to debug in the attached process. Defaults to 1. Works only on PowerShell 5 and above.",
604-
"default": null
599+
"type": "number",
600+
"description": "Optional: The ID of the runspace to debug in the attached process. Defaults to 1.",
601+
"default": 1
605602
},
606603
"runspaceName": {
607604
"type": "string",
608-
"description": "Optional: The Name of the runspace to debug in the attached process. Works only on PowerShell 5 and above.",
605+
"description": "Optional: The name of the runspace to debug in the attached process.",
609606
"default": null
610607
},
611608
"customPipeName": {
612609
"type": "string",
613-
"description": "The custom pipe name of the PowerShell host process to attach to. Works only on PowerShell 6.2 and above.",
610+
"description": "The custom pipe name of the PowerShell host process to attach to.",
614611
"default": null
615612
}
616613
}

src/features/DebugSession.ts

+35-35
Original file line numberDiff line numberDiff line change
@@ -426,6 +426,7 @@ export class DebugSessionFeature extends LanguageClientConsumer
426426
this.logger.writeVerbose(`Dotnet attach debug configuration: ${JSON.stringify(dotnetAttachConfig, undefined, 2)}`);
427427
this.logger.writeVerbose(`Attached dotnet debugger to process: ${pid}`);
428428
}
429+
429430
return this.tempSessionDetails;
430431
}
431432

@@ -452,7 +453,7 @@ export class DebugSessionFeature extends LanguageClientConsumer
452453
};
453454
}
454455

455-
/** Fetches all available vscode launch configurations. This is abstracted out for easier testing */
456+
/** Fetches all available vscode launch configurations. This is abstracted out for easier testing. */
456457
private getLaunchConfigurations(): DebugConfiguration[] {
457458
return workspace.getConfiguration("launch").get<DebugConfiguration[]>("configurations") ?? [];
458459
}
@@ -471,6 +472,10 @@ export class DebugSessionFeature extends LanguageClientConsumer
471472
return PREVENT_DEBUG_START;
472473
}
473474

475+
if (config.processId === 0 || config.processId === "current") {
476+
config.processId = await this.sessionManager.getLanguageServerPid();
477+
}
478+
474479
// If nothing is set, prompt for the processId.
475480
if (!config.customPipeName && !config.processId) {
476481
config.processId = await commands.executeCommand("PowerShell.PickPSHostProcess");
@@ -528,12 +533,13 @@ export class SpecifyScriptArgsFeature implements Disposable {
528533
if (text !== undefined) {
529534
await this.context.workspaceState.update(powerShellDbgScriptArgsKey, text);
530535
}
536+
531537
return text;
532538
}
533539
}
534540

535541
interface IProcessItem extends QuickPickItem {
536-
pid: string; // payload for the QuickPick UI
542+
processId: number; // payload for the QuickPick UI
537543
}
538544

539545
// eslint-disable-next-line @typescript-eslint/no-empty-interface
@@ -542,7 +548,7 @@ interface IGetPSHostProcessesArguments {
542548

543549
interface IPSHostProcessInfo {
544550
processName: string;
545-
processId: string;
551+
processId: number;
546552
appDomainName: string;
547553
mainWindowTitle: string;
548554
}
@@ -551,19 +557,16 @@ export const GetPSHostProcessesRequestType =
551557
new RequestType<IGetPSHostProcessesArguments, IPSHostProcessInfo[], string>("powerShell/getPSHostProcesses");
552558

553559
export class PickPSHostProcessFeature extends LanguageClientConsumer {
554-
555560
private command: Disposable;
556561
private waitingForClientToken?: CancellationTokenSource;
557562
private getLanguageClientResolve?: (value: LanguageClient) => void;
558563

559564
constructor(private logger: ILogger) {
560565
super();
561566

562-
this.command =
563-
commands.registerCommand("PowerShell.PickPSHostProcess", () => {
564-
return this.getLanguageClient()
565-
.then((_) => this.pickPSHostProcess(), (_) => undefined);
566-
});
567+
this.command = commands.registerCommand("PowerShell.PickPSHostProcess", async () => {
568+
return this.pickPSHostProcess();
569+
});
567570
}
568571

569572
public override setLanguageClient(languageClient: LanguageClient): void {
@@ -617,25 +620,24 @@ export class PickPSHostProcessFeature extends LanguageClientConsumer {
617620
}
618621
}
619622

620-
private async pickPSHostProcess(): Promise<string | undefined> {
623+
private async pickPSHostProcess(): Promise<number | undefined> {
624+
// We need the language client in order to send the request.
625+
await this.getLanguageClient();
626+
621627
// Start with the current PowerShell process in the list.
622-
const items: IProcessItem[] = [{
623-
label: "Current",
624-
description: "The current PowerShell Extension process.",
625-
pid: "current",
626-
}];
628+
const items: IProcessItem[] = [];
627629

628630
const response = await this.languageClient?.sendRequest(GetPSHostProcessesRequestType, {});
629631
for (const process of response ?? []) {
630632
let windowTitle = "";
631633
if (process.mainWindowTitle) {
632-
windowTitle = `, Title: ${process.mainWindowTitle}`;
634+
windowTitle = `, ${process.mainWindowTitle}`;
633635
}
634636

635637
items.push({
636638
label: process.processName,
637639
description: `PID: ${process.processId.toString()}${windowTitle}`,
638-
pid: process.processId,
640+
processId: process.processId,
639641
});
640642
}
641643

@@ -648,9 +650,10 @@ export class PickPSHostProcessFeature extends LanguageClientConsumer {
648650
matchOnDescription: true,
649651
matchOnDetail: true,
650652
};
653+
651654
const item = await window.showQuickPick(items, options);
652655

653-
return item ? item.pid : undefined;
656+
return item?.processId ?? undefined;
654657
}
655658

656659
private clearWaitingToken(): void {
@@ -660,7 +663,7 @@ export class PickPSHostProcessFeature extends LanguageClientConsumer {
660663
}
661664

662665
interface IRunspaceItem extends QuickPickItem {
663-
id: string; // payload for the QuickPick UI
666+
id: number; // payload for the QuickPick UI
664667
}
665668

666669
// eslint-disable-next-line @typescript-eslint/no-empty-interface
@@ -677,18 +680,16 @@ export const GetRunspaceRequestType =
677680
new RequestType<IGetRunspaceRequestArguments, IRunspace[], string>("powerShell/getRunspace");
678681

679682
export class PickRunspaceFeature extends LanguageClientConsumer {
680-
681683
private command: Disposable;
682684
private waitingForClientToken?: CancellationTokenSource;
683685
private getLanguageClientResolve?: (value: LanguageClient) => void;
684686

685687
constructor(private logger: ILogger) {
686688
super();
687-
this.command =
688-
commands.registerCommand("PowerShell.PickRunspace", (processId) => {
689-
return this.getLanguageClient()
690-
.then((_) => this.pickRunspace(processId), (_) => undefined);
691-
}, this);
689+
690+
this.command = commands.registerCommand("PowerShell.PickRunspace",
691+
async (processId) => { return this.pickRunspace(processId); },
692+
this);
692693
}
693694

694695
public override setLanguageClient(languageClient: LanguageClient): void {
@@ -734,28 +735,26 @@ export class PickRunspaceFeature extends LanguageClientConsumer {
734735
this.clearWaitingToken();
735736
reject();
736737

737-
void this.logger.writeAndShowError("Attach to PowerShell host process: PowerShell session took too long to start.");
738+
void this.logger.writeAndShowError(
739+
"Attach to PowerShell host process: PowerShell session took too long to start.");
738740
}
739741
}, 60000);
740742
},
741743
);
742744
}
743745
}
744746

745-
private async pickRunspace(processId: string): Promise<string | undefined> {
747+
private async pickRunspace(processId: number): Promise<number | undefined> {
748+
// We need the language client in order to send the request.
749+
await this.getLanguageClient();
750+
746751
const response = await this.languageClient?.sendRequest(GetRunspaceRequestType, { processId });
747752
const items: IRunspaceItem[] = [];
748753
for (const runspace of response ?? []) {
749-
// Skip default runspace
750-
if ((runspace.id === 1 || runspace.name === "PSAttachRunspace")
751-
&& processId === "current") {
752-
continue;
753-
}
754-
755754
items.push({
756755
label: runspace.name,
757756
description: `ID: ${runspace.id} - ${runspace.availability}`,
758-
id: runspace.id.toString(),
757+
id: runspace.id,
759758
});
760759
}
761760

@@ -764,9 +763,10 @@ export class PickRunspaceFeature extends LanguageClientConsumer {
764763
matchOnDescription: true,
765764
matchOnDetail: true,
766765
};
766+
767767
const item = await window.showQuickPick(items, options);
768768

769-
return item ? item.id : undefined;
769+
return item?.id ?? undefined;
770770
}
771771

772772
private clearWaitingToken(): void {

src/session.ts

+7
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,13 @@ export class SessionManager implements Middleware {
303303
return this.sessionDetails;
304304
}
305305

306+
public async getLanguageServerPid(): Promise<number | undefined> {
307+
if (this.languageServerProcess === undefined) {
308+
void this.logger.writeAndShowError("PowerShell Extension Terminal unavailable!");
309+
}
310+
return this.languageServerProcess?.getPid();
311+
}
312+
306313
public getPowerShellVersionDetails(): IPowerShellVersionDetails | undefined {
307314
return this.versionDetails;
308315
}

0 commit comments

Comments
 (0)