Skip to content

Add Start-DebugAttachSession function #2249

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

jborean93
Copy link
Contributor

@jborean93 jborean93 commented Jul 25, 2025

PR Summary

Adds the Start-DebugAttachSession cmdlet which can be used to launch a new attach session debug request inside an existing launched debug session. This allows a script being debugged to launch a child debugging session using more dynamic environment information without requiring end users to manually setup the launch.json configuration.

PR Context

Fixes: #2244

Examples added with: PowerShell/vscode-powershell#5246

image

Adds the `Start-DebugAttachSession` cmdlet which can be used to launch a
new attach session debug request inside an existing launched debug
session. This allows a script being debugged to launch a child debugging
session using more dynamic environment information without requiring end
users to manually setup the launch.json configuration.
@Copilot Copilot AI review requested due to automatic review settings July 25, 2025 05:21
@jborean93 jborean93 requested a review from a team as a code owner July 25, 2025 05:21
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR adds the Start-DebugAttachSession cmdlet that enables launching a new attach session debug request from within an existing debug session. This allows scripts to dynamically start child debugging sessions without requiring manual launch.json configuration.

Key changes:

  • Added new Start-DebugAttachSession PowerShell function with comprehensive parameter support
  • Implemented debug server variable management for session lifecycle
  • Added comprehensive documentation and examples

Reviewed Changes

Copilot reviewed 7 out of 7 changed files in this pull request and generated 5 comments.

Show a summary per file
File Description
LaunchAndAttachHandler.cs Sets global debug server variable during launch for child session access
DisconnectHandler.cs Cleans up debug server variable on disconnect for non-attach sessions
DebugService.cs Defines constant for debug server variable name
Start-DebugAttachSession.ps1 Main cmdlet implementation with parameter validation and debug request handling
Start-DebugAttachSession.md Comprehensive documentation with examples and parameter descriptions
PowerShellEditorServices.Commands.md Updates module documentation to include new cmdlet
PowerShellEditorServices.Commands.psd1 Exports new function in module manifest

[ErrorCategory]::InvalidArgument,
$null)
$err.ErrorDetails = [ErrorDetails]::new("")
$err.ErrorDetails.RecommendedAction = 'Specify only one of RunspaceId or RunspaceName.'
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

chefs kiss on recommendedaction

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Gotta play to the crowd :) It has made me notice that it's awkward to set it in PowerShell outside of Write-Error. Might be something to look into some more.

// Start-DebugAttachSession attaches in a new temp console
// so we cannot set this var if already running in that
// console.
PSCommand setVariableCmd = new PSCommand().AddCommand("Set-Variable")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice way to pass this in.

Copy link
Collaborator

@JustinGrote JustinGrote left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM for the most part, this will be a very useful addtiion!

  • doing detach in the runspace creates an unhandled exception, we should probably handle this nicer if at all possible but it might not be.

  • having tests for this would be ideal, but scaffolding it may be difficult. It should be doable without needing to E2E it via the vscode-powershell repo. I'll think on it.

I wonder if there's any way we can hook into child pwsh process starts from an active debug session and do "smart" attach similar to what node does, this way you could debug child processes without having to make code changes. That would be a separate PR though.

In any case, this is more or less nonexistent/broken functionality so this isn't a risky thing to merge.

@jborean93
Copy link
Contributor Author

doing detach in the runspace creates an unhandled exception, we should probably handle this nicer if at all possible but it might not be.

This is somewhat of an existing problem and I definitely agree it would be nice to solve. Basically the underlying process ends which kills the transport and the debug server doesn't handle that properly. IIRC the error message also says if you see this at the end it can be ignored.

having tests for this would be ideal, but scaffolding it may be difficult. It should be doable without needing to E2E it via the vscode-powershell repo. I'll think on it.

Testing an attach scenario is hard, I've started to add some scaffolding to #2250 but agree more tests are really needed for attaching. I think the difficult bit will be that Omnisharp doesn't actually know about startDebugging so I'm unsure if there is going to be any way to "expect" the request on the test side.

@jborean93
Copy link
Contributor Author

I think the difficult bit will be that Omnisharp doesn't actually know about startDebugging so I'm unsure if there is going to be any way to "expect" the request on the test side.

Looks like there is a way to wait for any request by name in the test runner so it should be possible to test this out. I'll probably want to look at adding it in in a separate PR though as the tests in my various PRs are starting to rely on each other or repeat the same things.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support startDebugging DAP reverse request
2 participants