Skip to content
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

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

Closed
5 tasks done
Hashbrown777 opened this issue Apr 30, 2024 · 10 comments
Closed
5 tasks done
Labels
Resolution-Duplicate The issue is a duplicate.

Comments

@Hashbrown777
Copy link

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

@Hashbrown777 Hashbrown777 added the Needs-Triage The issue is new and needs to be triaged by a work group. label Apr 30, 2024
@rhubarb-geek-nz
Copy link

Is this a duplicate of #21402 ?

@Hashbrown777
Copy link
Author

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
Copy link
Collaborator

Dup #4217

@Hashbrown777
Copy link
Author

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
Copy link

#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
Copy link
Author

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
Copy link

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
Copy link
Author

#!/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.

@StevenBucher98 StevenBucher98 added Resolution-Duplicate The issue is a duplicate. and removed Needs-Triage The issue is new and needs to be triaged by a work group. labels May 13, 2024
Copy link
Contributor

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

Copy link
Contributor

microsoft-github-policy-service bot commented May 14, 2024

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

🔗 https://aka.ms/PSRepoFeedback

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Resolution-Duplicate The issue is a duplicate.
Projects
None yet
Development

No branches or pull requests

4 participants