Skip to content

VScode debug powershell script (F5) does NOT populate $PSScriptRoot #4368

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
6 tasks done
basprins opened this issue Jan 10, 2023 · 2 comments
Closed
6 tasks done

VScode debug powershell script (F5) does NOT populate $PSScriptRoot #4368

basprins opened this issue Jan 10, 2023 · 2 comments
Labels
Issue-Bug A bug to squash. Resolution-Duplicate Will close automatically.

Comments

@basprins
Copy link

Prerequisites

  • I have written a descriptive issue title.
  • I have searched all open and closed 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

Very much similar to issue #1330 I am unable to use $PSScriptRoot as a default argument when I am running the script in the debugger with F5.

My simplified script:

`
param(
[parameter(Mandatory=$true)] [ValidateNotNullOrEmpty()] [string]$machineNumber,
[ValidateNotNullOrEmpty()] [string]$softwareFolder = "some path",
[ValidateNotNullOrEmpty()] [string]$target = "$PSScriptRoot....\Simulation",
[parameter(Mandatory=$true)] [ValidateNotNullOrEmpty()] [string]$version
)
Process
{
$copyOfPSScriptRoot = $PSScriptRoot

"target = $target"
"copyOfPSScriptRoot = $copyOfPSScriptRoot"
"PSscriptroot = $PSScriptRoot"

}
`

I also added the launch configuration as suggested in the linked issue:

{ // Use IntelliSense to learn about possible attributes. // Hover to view descriptions of existing attributes. // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 "version": "0.2.0", "configurations": [ { "type": "PowerShell", "request": "launch", "name": "PowerShell Launch Current File From Workspace Folder", "script": "${file}", "args": [], "cwd": "${workspaceFolder}" }, ] }

When I debug the script from VScode (F5) an fill in the two mandatory arguments with whatever, I can inspect the values for :

  • $target = "....\Simulation" which is wrong, it should consist of the rooted path where my script is stored.
  • $PSScriptRoot = "", I guess that was expected, given that $target contains the wrong path. Still weird that PSScriptRoot for some reason is not populated....
  • but then... surprisingly enough , the $copyOfScriptRoot DOES have the correct rooted path of my script location. Wait, WUT?! how??

I double checked the powershell extension, I am using latest.
When I run the script from a powershell command window, it DOES populate $PSScriptRoot as expected.

Am I doing something wrong? Or is there still something broken with the annoying special behavior of populating $PSScriptRoot?

PS: I'd love to join discord for asking questions first, but I can't seem to login there. Hope you can forgive me for "dropping a bug" which might still be a user error on my side...

PowerShell Version

PS C:\dev\bookmaster\BuildEnvironment> $PSVersionTable

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

Visual Studio Code Version

λ code --version
1.74.0
5235c6bb189b60b01b1f49062f4ffa42384f8c91
x64

Extension Version

Steps to Reproduce

  • Copy paste the simplified example to c:\dev\PrepareSimFromRemote.ps1
  • open C:\dev in vscode (cd dev; code .)
  • ctrl + shift + p,
  • edit launch.json to

{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "PowerShell",
"request": "launch",
"name": "PowerShell Launch Current File From Workspace Folder",
"script": "${file}",
"args": [],
"cwd": "${workspaceFolder}"
},
]
}

  • Open the PrepareSimFromRemote script
  • optionally: Put a breakpoint on any line in the script
  • Press F5
  • Observe that $PSScriptRoot is empty

Visuals

k

Logs

k

@basprins basprins added the Issue-Bug A bug to squash. label Jan 10, 2023
@ghost ghost added the Needs: Triage Maintainer attention needed! label Jan 10, 2023
@andyleejordan
Copy link
Member

Hey! You're far from the first to ask for this, while we agree it'd be great, we haven't identified a proper solution for it yet. Going to refer you to prior discussion in #633

@andyleejordan andyleejordan added Resolution-Duplicate Will close automatically. and removed Needs: Triage Maintainer attention needed! labels Jan 31, 2023
@ghost
Copy link

ghost commented Feb 1, 2023

This issue has been marked as duplicate and has not had any activity in a day. It has been closed for housekeeping purposes.

@ghost ghost closed this as completed Feb 1, 2023
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Issue-Bug A bug to squash. Resolution-Duplicate Will close automatically.
Projects
None yet
Development

No branches or pull requests

2 participants