Skip to content

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

Closed
@basprins

Description

@basprins

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions