Skip to content
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

Custom prompt function no longer sees PSDebugContext #3980

Closed
6 tasks done
jhoneill opened this issue May 13, 2022 · 4 comments · Fixed by PowerShell/PowerShellEditorServices#1803
Closed
6 tasks done
Assignees
Labels

Comments

@jhoneill
Copy link

jhoneill commented May 13, 2022

Prerequisites

  • I have written a descriptive issue title.
  • I have searched all issues to ensure it has not already been reported.
  • I have read the troubleshooting guide.
  • I am sure this issue is with the extension itself and does not reproduce in a standalone PowerShell instance.
  • I have verified that I am using the latest version of Visual Studio Code and the PowerShell extension.
  • If this is a security issue, I have read the security issue reporting guidance.

Summary

I have my own prompt function. For many years it has had

$debugging = (Test-Path -Path variable:/PSDebugContext)
...
 $(if ($debugging) { '[DBG]: ' } else { '' }) + "rest of prompt"

This no longer works ... And if I define a function

function debugging {Test-Path -Path variable:/PSDebugContext}

before running the code that hits the break point debugging returns false even though I can see $PSDebugContext interactively. But if I define the function while the debugger is active it returns true. It looks like a scopes thing but after spending a while trying to figure out how to see it , I've got nowhere. What is the correct way to detect the debugger now?

PowerShell Version

Name                           Value�[0m
----                           -----�[0m
PSVersion                      7.2.3
PSEdition                      Core
GitCommitId                    7.2.3
OS                             Microsoft Windows 10.0.19042
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

Visual Studio Code Version

1.67.1
da15b6fd3ef856477bf6f4fb29ba1b7af717770d
x64

Extension Version

Steps to Reproduce

function prompt         {
    <#
    .DESCRIPTION
    Display either [DBG] if debugging or [ADM] if we're Admin; then #HistoryID plus the PowerShell Version
    replace the middle of paths longer than the 1/3 of the screen width "..." AND it saves the history to $logfile
    #>
    if ($Global:shortPrompt) {return "`r`nPS>  "}
    $debugging = (Test-Path -Path variable:/PSDebugContext)
    $p         = $pwd.path.ToString()  -replace  [regex]::Escape( [System.Environment]::GetFolderPath([System.Environment+SpecialFolder]::MyDocuments)), "~\Documents"
    $hid       = $myinvocation.HistoryId
    if    ($logfile -and $hid -gt 1 -and -not $debugging) {Get-History -Id ($hid -1 ) | ConvertTo-Csv | Select-Object -last 1 | Add-Content -Encoding ascii -Path $logfile}
    if    ($p.Length -gt ($Host.UI.RawUI.BufferSize.Width/3)) {$p = $p -replace "(?<=^.{10,})\\.*\\(?=.{20,}$)","\...\"}
    if    ($env:isadmin) {Write-Host -ForegroundColor Yellow -NoNewline "[ADM]" }
     $(if ($debugging) { '[DBG]: ' } else { '' }) + "#$([math]::abs($hid)) PS$($PSVersionTable.PSVersion.Major) $p>" + $(if ($NestedPromptLevel -ge 1) { '>> ' } else {' '})
}

Paste the above at the prompt and run something with a breakpoint set and debugger in Interactive session mode.

Visuals

image

Logs

No response

@jhoneill jhoneill added the Issue-Bug A bug to squash. label May 13, 2022
@ghost ghost added the Needs: Triage Maintainer attention needed! label May 13, 2022
@JustinGrote
Copy link
Collaborator

Hmm this is how I've always done it too, I've noted my debug prompt has been wonkly lately. Good catch! I'll see if I can repro this consistently.

@JustinGrote JustinGrote added Area-Debugging and removed Needs: Triage Maintainer attention needed! labels May 13, 2022
@andyleejordan
Copy link
Member

I'm pretty sure this is because, as part of the effort to fix #873, we made an allowlist for which commands go through ProcessDebuggerCommand (and prompt is not currently in that list). Seems reasonable to add it given the behavior you're expecting. /cc @SeeminglyScience

@jhoneill
Copy link
Author

The first thing I noticed was the fix to 873, my history number isn't incrementing in steps of a dozen any more, which is a sizable win :-) If we can just get the prompt right that will be great.

@andyleejordan andyleejordan changed the title After May 2022 update custom prompt function no longer sees PSDebugContext (with debugger in interactive session mode) Custom prompt function no longer sees PSDebugContext May 17, 2022
@andyleejordan andyleejordan moved this from Todo to In Progress in American Pharoah May 17, 2022
Repository owner moved this from In Progress to Done in American Pharoah May 17, 2022
@jhoneill
Copy link
Author

Good stuff, thanks @andschwa

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
No open projects
Status: Done
Development

Successfully merging a pull request may close this issue.

3 participants