Skip to content

Commit ac8be74

Browse files
committed
Remote mandatory param and update logic to exclude temp consoles
1 parent cd67c0e commit ac8be74

File tree

4 files changed

+27
-19
lines changed

4 files changed

+27
-19
lines changed

module/PowerShellEditorServices/Commands/Public/Start-DebugAttachSession.ps1

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ function Start-DebugAttachSession {
3030
[string]
3131
$RunspaceName,
3232

33-
[Parameter(Mandatory)]
33+
[Parameter()]
3434
[int]
3535
$RunspaceId,
3636

@@ -46,7 +46,7 @@ function Start-DebugAttachSession {
4646
$ErrorActionPreference = 'Stop'
4747

4848
try {
49-
if ($RunspaceId -and $RunspaceName) {
49+
if ($PSBoundParameters.ContainsKey('RunspaceId') -and $RunspaceName) {
5050
$err = [ErrorRecord]::new(
5151
[ArgumentException]::new("Cannot specify both RunspaceId and RunspaceName parameters"),
5252
"InvalidRunspaceParameters",
@@ -62,7 +62,7 @@ function Start-DebugAttachSession {
6262
$debugServer = Get-Variable -Name __psEditorServices_DebugServer -ValueOnly -ErrorAction Ignore
6363
if (-not $debugServer) {
6464
$err = [ErrorRecord]::new(
65-
[Exception]::new("Cannot start a new attach debug session unless running in an existing debug session"),
65+
[Exception]::new("Cannot start a new attach debug session unless running in an existing launch debug session not in a temporary console"),
6666
"NoDebugSession",
6767
[ErrorCategory]::InvalidOperation,
6868
$null)

module/docs/Start-DebugAttachSession.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
---
22
external help file: PowerShellEditorServices.Commands-help.xml
3+
Module Name: PowerShellEditorServices.Commands
34
online version: https://github.com/PowerShell/PowerShellEditorServices/tree/main/module/docs/Start-DebugAttachSession.md
45
schema: 2.0.0
56
---
@@ -14,19 +15,19 @@ Starts a new debug session attached to the specified PowerShell instance.
1415

1516
### ProcessId (Default)
1617
```
17-
Start-DebugAttachSession [-Name <String>] [-ProcessId <Int32>] [-RunspaceName <String>] -RunspaceId <Int32>
18+
Start-DebugAttachSession [-Name <String>] [-ProcessId <Int32>] [-RunspaceName <String>] [-RunspaceId <Int32>]
1819
[-ComputerName <String>] [-AsJob] [<CommonParameters>]
1920
```
2021

2122
### CustomPipeName
2223
```
2324
Start-DebugAttachSession [-Name <String>] [-CustomPipeName <String>] [-RunspaceName <String>]
24-
-RunspaceId <Int32> [-ComputerName <String>] [-AsJob] [<CommonParameters>]
25+
[-RunspaceId <Int32>] [-ComputerName <String>] [-AsJob] [<CommonParameters>]
2526
```
2627

2728
## DESCRIPTION
2829

29-
The Start-DebugAttachSession function can be used to start a new debug session that is attached to the specified PowerShell instance. The caller must be running in an existing launched debug session and the newly attached session will be treated as a child debug session in a new temporary console. If the callers script ends before the new debug session is completed, the debug session for the child will also end.
30+
The Start-DebugAttachSession function can be used to start a new debug session that is attached to the specified PowerShell instance. The caller must be running in an existing launched debug session, the launched session is not running in a temporary console, and the launched session is not entered into a remote PSSession. If the callers script ends before the new debug session is completed, the debug session for the child will also end.
3031

3132
The function will return once the attach response was received by the debug server. For an example, an attach request will return once PowerShell has attached to the process and has called `Debug-Runspace`. If you need to return early use the `-AsJob` parameter to return a `Job` object immediately that can be used to wait for the response at a later time.
3233

@@ -166,7 +167,7 @@ Type: Int32
166167
Parameter Sets: (All)
167168
Aliases:
168169
169-
Required: True
170+
Required: False
170171
Position: Named
171172
Default value: None
172173
Accept pipeline input: False

src/PowerShellEditorServices/Services/DebugAdapter/Handlers/DisconnectHandler.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,15 +56,16 @@ public async Task<DisconnectResponse> Handle(DisconnectArguments request, Cancel
5656
_debugStateService.ExecutionCompleted = true;
5757
_debugService.Abort();
5858

59-
if (!_debugStateService.IsAttachSession)
59+
if (!_debugStateService.IsAttachSession && !_debugStateService.IsUsingTempIntegratedConsole)
6060
{
6161
await _executionService.ExecutePSCommandAsync(
6262
new PSCommand().AddCommand("Remove-Variable")
6363
.AddParameter("Name", DebugService.PsesGlobalVariableDebugServerName)
6464
.AddParameter("Force", true),
6565
cancellationToken).ConfigureAwait(false);
6666
}
67-
else if (_debugStateService.IsInteractiveDebugSession)
67+
68+
if (_debugStateService.IsInteractiveDebugSession && _debugStateService.IsRemoteAttach)
6869
{
6970
// Pop the sessions
7071
if (_runspaceContext.CurrentRunspace.RunspaceOrigin == RunspaceOrigin.EnteredProcess)

src/PowerShellEditorServices/Services/DebugAdapter/Handlers/LaunchAndAttachHandler.cs

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -179,16 +179,22 @@ public async Task<LaunchResponse> Handle(PsesLaunchRequestArguments request, Can
179179

180180
_logger.LogTrace("Working dir " + (string.IsNullOrEmpty(workingDir) ? "not set." : $"set to '{workingDir}'"));
181181

182-
PSCommand setVariableCmd = new PSCommand().AddCommand("Set-Variable")
183-
.AddParameter("Name", DebugService.PsesGlobalVariableDebugServerName)
184-
.AddParameter("Value", _debugAdapterServer)
185-
.AddParameter("Description", "DO NOT USE: for internal use only.")
186-
.AddParameter("Scope", "Global")
187-
.AddParameter("Option", "ReadOnly");
188-
189-
await _executionService.ExecutePSCommandAsync(
190-
setVariableCmd,
191-
cancellationToken).ConfigureAwait(false);
182+
if (!request.CreateTemporaryIntegratedConsole)
183+
{
184+
// Start-DebugAttachSession attaches in a new temp console
185+
// so we cannot set this var if already running in that
186+
// console.
187+
PSCommand setVariableCmd = new PSCommand().AddCommand("Set-Variable")
188+
.AddParameter("Name", DebugService.PsesGlobalVariableDebugServerName)
189+
.AddParameter("Value", _debugAdapterServer)
190+
.AddParameter("Description", "DO NOT USE: for internal use only.")
191+
.AddParameter("Scope", "Global")
192+
.AddParameter("Option", "ReadOnly");
193+
194+
await _executionService.ExecutePSCommandAsync(
195+
setVariableCmd,
196+
cancellationToken).ConfigureAwait(false);
197+
}
192198
}
193199

194200
// Prepare arguments to the script - if specified

0 commit comments

Comments
 (0)