Skip to content

Configuration option to debug or run a script without dot sourcing it #4327

Closed
@metablaster

Description

@metablaster

Prerequisites

  • I have written a descriptive issue title.
  • I have searched all issues to ensure it has not already been reported.

Summary

There are 3 ways to debug (or run) a script In VSCode:

  1. Run -> Run Without Debugging
  2. Run -> Start Debugging
  3. By configuring launch.json and start debugging from action bar.

In all 3 cases a script will be dot sourced and I don't see a way to instruct debugger to call it rather than dot sourcing it.

I didn't notice this behavior until I stumbled upon a problem which you can see on SO:
"Run without debugging" or "Start debugging" giving different result than running script manually

Here is sample code:

$private:PSDefaultParameterValues = @{}
$private:PSDefaultParameterValues.Add("*:ErrorVariable", "+ErrorBuffer")

function Test-Variable
{
    [CmdletBinding()]
    param ()

    "Test-Variable private: '$($private:PSDefaultParameterValues | Out-String)'"
    "Test-Variable local: '$($local:PSDefaultParameterValues | Out-String)'"
    "Test-Variable script: '$($script:PSDefaultParameterValues | Out-String)'"
    "Test-Variable global: '$($global:PSDefaultParameterValues | Out-String)'"

    $ErrorBuffer = $PSCmdlet.GetVariableValue("ErrorBuffer")
    "Count of errors is $($ErrorBuffer.Count)"

    Write-Error -Message "sample error"

    "Count of errors is $($ErrorBuffer.Count)"
    $ErrorBuffer.Clear()
}

Test-Variable

Put it into a script and call it, the result is as expected, "one error", but when you run it with the help of PS Extension the result is unexpected because the script isn't meant to be dot sourced.

Currently it's impossible to debug or run it and have the result you want.
Debugger should not assume a user whishes to dot source a script.

Proposed Design

I propose an option to let user instruct debugger on how a script is to be executed, for example:

"powershell.debugging.executeMode = "call"

// OR

"powershell.debugging.executeMode = "dotsource"

Metadata

Metadata

Assignees

Type

No type

Projects

Status

Done

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions