Vscode-powershell: Debugging: $WhatIfPreference is always reported as $false

Created on 22 Nov 2017  路  2Comments  路  Source: PowerShell/vscode-powershell

System Details

  • Operating system name and version: Windows 10 Pro, Version 1709, Build 16299.64
  • VS Code version: Code 1.18.1 (929bacba01ef658b873545e26034d1a8067445e9, 2017-11-16T18:32:36.023Z)
  • PowerShell extension version: 1.5.1
  • Output from $PSVersionTable:

Name|Value
---|---
PSVersion|5.1.16299.64
PSEdition|Desktop
PSCompatibleVersions|{1.0, 2.0, 3.0, 4.0...}
BuildVersion|10.0.16299.64
CLRVersion|4.0.30319.42000
WSManStackVersion|3.0
PSRemotingProtocolVersion|2.3
SerializationVersion|1.1.0.1

Copy / paste the following commands into the PowerShell Integrated Console, and paste the output here:

PowerShell Integrated Console

PS > code -v
1.18.1
929bacba01ef658b873545e26034d1a8067445e9
PS > $pseditor.EditorServicesVersion

Major  Minor  Build  Revision
-----  -----  -----  --------
1      5      1      0


PS > code --list-extensions --show-versions
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
PS > $PSVersionTable

Name                           Value
----                           -----
PSVersion                      5.1.16299.64
PSEdition                      Desktop
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
BuildVersion                   10.0.16299.64
CLRVersion                     4.0.30319.42000
WSManStackVersion              3.0
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1

Note: I did search for similar issues, but I could not find any.

Issue Description

In a script I am writing, I have a check against $WhatIfPreference. To test this in a debugging session in VS Code, I pass the -WhatIf flag using the PowerShell Launch Current File w/Args Prompt debug option.

Test case:

[CmdletBinding(SupportsShouldProcess=$true)]
Param()

If ($WhatIfPreference) {
  Write-Output "WhatIf enabled"
} Else {
  Write-Output "WhatIf disabled"
}

A breakpoint has been set on the If line. Upon entering a debugging session and hitting the breakpoint, hovering the mouse over $WhatIfPreference will show $false, no matter if -WhatIf has been set. This is true for the Variables and Watch panels as well. However, the script will evaluate the variable correctly. If -WhatIf is set, the test evaluates as true.

Please let me know if there's any more information or context I should provide.

Area-Debugging Issue-Bug

Most helpful comment

I believe this has to do with the fact that when you debug a script, that script is dot sourced into the PowerShell Integrated Console's global session. This is not one of my favorite things about the debugger but we were trying to reach some parity with the ISE debugger. Ironically this scenario works as expected in ISE. Go figure. Anyway, thanks for reporting the issue.

All 2 comments

I believe this has to do with the fact that when you debug a script, that script is dot sourced into the PowerShell Integrated Console's global session. This is not one of my favorite things about the debugger but we were trying to reach some parity with the ISE debugger. Ironically this scenario works as expected in ISE. Go figure. Anyway, thanks for reporting the issue.

This is probably related to: https://github.com/PowerShell/PowerShell/issues/4568

If you set $whatifpreference prior to running the script, it will report $true.

This same behavior applies for $verbosepreference and $debugpreference as well
image

It happens also if you set a breakpoint and call with & vs dotsource, so I don't think https://github.com/PowerShell/vscode-powershell/issues/1109#issuecomment-346528932 is correct.

It may be related to watch commands not running within the script PSCmdlet scope or something? For instance, you can't "watch" things like $PSScriptRoot or $PSBoundParameters, maybe this preference falls into that category of things.
image

Was this page helpful?
0 / 5 - 0 ratings