Skip to content

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

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

Closed
2 tasks done
metablaster opened this issue Dec 13, 2022 · 13 comments
Closed
2 tasks done
Assignees
Labels
Area-Debugging Issue-Enhancement A feature request (enhancement). Needs: Fix Verification Please verify the fix for us! Resolution-Fixed Will close automatically. Up for Grabs Will shepherd PRs.

Comments

@metablaster
Copy link

metablaster commented Dec 13, 2022

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"
@metablaster metablaster added the Issue-Enhancement A feature request (enhancement). label Dec 13, 2022
@ghost ghost added the Needs: Triage Maintainer attention needed! label Dec 13, 2022
@andyleejordan andyleejordan added Area-Debugging and removed Needs: Triage Maintainer attention needed! labels Jan 31, 2023
@andyleejordan
Copy link
Member

It's not a bad idea at all, thanks for the suggestion!

@ThioJoe
Copy link

ThioJoe commented Jun 12, 2023

I also would like this. I use AppLocker so Powershell by default runs in Constrained Language mode. But I have certain scripts, and even certain directories set to allowed in AppLocker. So when actually running those scripts, they are actually running in Full Language mode. However, the Powershell extension only runs by dot sourcing, which won't even let it run in the first place, for example:

PS B:\DemoFolder> . 'B:\DemoFolder\test.ps1'
test.ps1: Cannot dot-source this command because it was defined in a different language mode. To invoke this command without importing its contents, omit the '.' operator.

But when running the script just through a powershell window:

Write-Host "Hello World!"
Write-Host $ExecutionContext.SessionState.LanguageMode

I get:

PS B:\DemoFolder> .\test.ps1
Hello World!
FullLanguage

So it should work, but it can't if it's trying to run by dot sourcing apparently. Without the option to change it, the extension is effectively useless on any device with AppLocker.

@drstonephd
Copy link

There is a lot of fuss about honoring preference variables in a module. For example, I use DBATools. I have to use the global scope to silence the module. However, without a script scope, I don't have an easy means to set the preferences for my personal use.

@pheeling
Copy link

@andyleejordan Is there an ETA for this feature?

@andyleejordan
Copy link
Member

Not currently, sorry. We are swamped with other work right now, but we will get to it eventually!

@andyleejordan
Copy link
Member

I'll add that I would be super happy to take a PR for it!

@andyleejordan
Copy link
Member

Working on getting this in 🙏

@JustinGrote JustinGrote added the Up for Grabs Will shepherd PRs. label Oct 8, 2024
@andyleejordan andyleejordan added Resolution-Fixed Will close automatically. and removed Needs: Maintainer Attention Maintainer attention needed! labels Oct 10, 2024
@andyleejordan andyleejordan moved this from Todo to In Progress in Flying Fox Oct 10, 2024
Copy link
Contributor

This issue has been labeled as resolved, please verify the provided fix (or other reason).

@github-actions github-actions bot added the Needs: Fix Verification Please verify the fix for us! label Oct 10, 2024
@metablaster
Copy link
Author

@andyleejordan
I appreciate the fix but I'm currently not in position to test it because I've switched to Linux almost a year ago and no longer do anything with PowerShell, I also no longer maintain my old PS project and don't even know where I left off.

I'm sure somebody else can test and confirm the fix.

@github-actions github-actions bot removed the Needs: Fix Verification Please verify the fix for us! label Oct 11, 2024
Copy link
Contributor

This issue has been labeled as resolved, please verify the provided fix (or other reason).

@github-actions github-actions bot added the Needs: Fix Verification Please verify the fix for us! label Oct 11, 2024
@JustinGrote
Copy link
Collaborator

@metablaster no worries! I'll be able to confirm it. Thanks so much for your contribution and sorry it took us so long to address it.

@JustinGrote JustinGrote self-assigned this Oct 11, 2024
@github-actions github-actions bot removed the Needs: Fix Verification Please verify the fix for us! label Oct 11, 2024
Copy link
Contributor

This issue has been labeled as resolved, please verify the provided fix (or other reason).

@github-actions github-actions bot added the Needs: Fix Verification Please verify the fix for us! label Oct 11, 2024
Copy link
Contributor

This issue has been labeled as needing fix verification and has not had any activity a week. It has been closed for housekeeping purposes.

@github-project-automation github-project-automation bot moved this from In Progress to Done in Flying Fox Oct 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-Debugging Issue-Enhancement A feature request (enhancement). Needs: Fix Verification Please verify the fix for us! Resolution-Fixed Will close automatically. Up for Grabs Will shepherd PRs.
Projects
Status: Done
Development

No branches or pull requests

6 participants