PowerShell / PowerShell

PowerShell for every system!
https://microsoft.com/PowerShell
MIT License
43.56k stars 7.06k forks source link

`$PSScriptRoot` unpopulated when script missing the `.ps1` file extension #21554

Closed Hashbrown777 closed 4 days ago

Hashbrown777 commented 2 weeks ago

Prerequisites

Steps to reproduce

Create a script utilising $PSScriptRoot, running via crunchbang instead of file association.

Expected behavior

PS> $pwd.Path
/home/hashbrown
PS> gc testScript
#!/bin/pwsh
$PSScriptRoot
PS> ./testScript
/home/hashbrown
PS>

Actual behavior

PS> $pwd.Path
/home/hashbrown
PS> gc testScript
#!/bin/pwsh
$PSScriptRoot
PS> ./testScript

PS>

Error details

No response

Environment data

Name                           Value
----                           -----
PSVersion                      7.4.2
PSEdition                      Core
GitCommitId                    7.4.2
OS                             Fedora Remix for WSL
Platform                       Unix
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

Visuals

image

rhubarb-geek-nz commented 2 weeks ago

Is this a duplicate of #21402 ?

Hashbrown777 commented 2 weeks ago

Is this a duplicate of #21402 ? image

Looks like it.

image They need to update the title with the real cause of the issue; the calling name ($args[0] in other languages, eg $0 in bash) is missing the .ps1 is the cause. Seems it's not so much the name of the actual script itself. Keeping it "Symbolic links can change..." is impossible to search if you're closer to the real problem à la "...script missing .ps1 extension".

To add; I think what's really responsible is that $MyInvocation has .MyCommand listed as the whole script as a raw string, instead of the script as a file, when the extension is missing. If MyCommand isn't a file it cannot have a path. image

iSazonov commented 2 weeks ago

Dup #4217

Hashbrown777 commented 2 weeks ago

Dup #4217

Can't dupe a closed-due-to-inactivity issue.

"if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you"

rhubarb-geek-nz commented 2 weeks ago

21402 did have a work around, creating a shell script without the extension adjacent to the powershell script with ps1

#!/bin/sh -e
exec pwsh -File "$0.ps1" "$@"
Hashbrown777 commented 2 weeks ago

I've just resorted to keeping the .ps1 on the actual script instead of pushing args and input&output streams through an intermediary, and hopefully this'll get fixed one day. It took forever for something huge like 1908 to get done, but it did get done!

rhubarb-geek-nz commented 2 weeks ago

instead of pushing input&output streams through an intermediary

Just so you know, the exec means no fork occurs and pwsh replaces sh with the same process id, environment, file descriptors etc. There is no intermediate.

Hashbrown777 commented 2 weeks ago

!/bin/sh -e

It's more bourne itself, I don't want to have to look up the intricacies of $@ et cetera and any caveats involved with explicitly using pwsh's -File, even if it turns out there are none for either. It's much simpler to just rename the script for now.

microsoft-github-policy-service[bot] commented 4 days ago

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

microsoft-github-policy-service[bot] commented 4 days ago

📣 Hey @Hashbrown777, how did we do? We would love to hear your feedback with the link below! 🗣️

🔗 https://aka.ms/PSRepoFeedback

Microsoft Forms