Skip to content

Custom prompt function no longer sees PSDebugContext #3980

Closed
PowerShell/PowerShellEditorServices
#1803
@jhoneill

Description

@jhoneill

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

ms-vscode.powershell@2022.5.1

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

Metadata

Metadata

Assignees

Type

No type

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions