-
Notifications
You must be signed in to change notification settings - Fork 510
Cannot get path of running script in $MyInvocation.InvocationName #3959
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
Comments
Thanks @vadhbka were you debugging when you hit this? |
I have always used $MyInvocation.InvocationName to create the temp folder. |
@vadhbka this variable is dynamically populated depending on the context, you can't just run it at the command line, if it's present in a script it will present what you want, and if you are stopped in the debugger it will do that too. This is a PowerShell thing, has nothing to do with the extension, unless I'm missing something. I don't see any issue here, this behavior is the same in the previous versions. Can you provide a specific reproduction? |
Hi |
@CarlosAndreuLlaneras |
@vadhbka |
@vadhbka thanks for the update! I agree with @CarlosAndreuLlaneras because this makes your script immune to differences in how it is invoked (dotsource vs direct run). This happens in PowerShell itself, not just vscode, we just changed how it gets invoked but this exposes a flaw in your script, not necessarily in vscode. Test.ps1 $myinvocation.InvocationName @andschwa this looks like a "regression" (more like behavior change) from changing how the debug invokes scripts (since now it dotsources rather than running directly), but I don't know if there's anything we can do other than call it out as a "breaking" change. |
Hm, that's odd. I thought we were always dot-sourcing scripts. In fact, there was a regression where we changed to using the ampersand call-operator and had to change it back to dot-sourcing: PowerShell/PowerShellEditorServices#1652 |
@JustinGrote
This is a good method. |
We were always dot sourcing, but when this worked previously it was done via It's like it just looks at the first token rather than the first pipeline element. That said, I also agree that this is not the right use case for |
Think we should use |
Sorry I meant |
I'll check the Git history and find out why. |
The change to dot-source when you hit F5 was a breaking change for my workflow. I routinely have module function implementations in .PS1 files and I dot-source them from .PSM1 files. While I'm developing the functions, I would open the .PS1 file in VS Code and then use F5 and the InvocationName to exercise the new functions under the debugger:
I don't want the test code to execute when I import the module that dot-sources the .PS1 file. |
@NeilBrenMS the extension has always dot-sourced, with the exception of maybe a month during a preview window. |
You may want to set a variable in your psm1 instead. |
When debugging a script $MyInvocation, $PSScriptRoot and $PSCommandPath are all empty. |
Currently, I have confirmed that $MyInvocation.InvocationName does not work Visual Studio Code Version:1.76.0 |
Prerequisites
Summary
In versions 2022.5.0 and 2022.5.1
To get the path of a running script.
i tried
$MyInvocation.InvocationName
but I could not get the path by this command.
Similarly, to retrieve the path of a script running on
$PSScriptRoot
This also did not retrieve the path.
I downgraded the version to 2021.12.0 and got the path correctly
Thank you
Translated with www.DeepL.com/Translator
PowerShell Version
Visual Studio Code Version
Extension Version
Steps to Reproduce
$MyInvocation.InvocationName
Visuals
No response
Logs
No response
The text was updated successfully, but these errors were encountered: