-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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
Add pwsh PowerShell interpreter #4073
Conversation
Thanks for the contribution!
Yes it will. |
@pchaigno, thanks! i have found https://github.com/PowerShell/PowerShellStandard/blob/3436bfc162d6804dd11d1d76c4faff486b4b405d/build.ps1. This repo is MIT Licensed https://github.com/PowerShell/PowerShellStandard/blob/master/LICENSE.txt |
That'll do. Could you add it under |
@pchaigno, added. I renamed the file to shabang.ps1, to clarify its purpose. |
lib/linguist/languages.yml
Outdated
@@ -3511,6 +3511,7 @@ PowerShell: | |||
codemirror_mime_type: application/x-powershell | |||
aliases: | |||
- posh | |||
- pwsh |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Uh, I almost missed that. To add a new shebang, you need to add pwsh
under the interpreters
key, no the aliases
one.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks. I have added it under both Shell and PowerShell. One of the Ruby 2.3.3 job is failing to clone:
Error updating vendor/grammars/xc.tmbundle
Cloning into 'vendor/grammars/xc.tmbundle'...
error: RPC failed; result=35, HTTP code = 0
fatal: The remote end hung up unexpectedly
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have added it under both Shell and PowerShell.
I though this was a PowerShell interpreter only??
One of the Ruby 2.3.3 job is failing to clone:
That happens sometimes. Probably a momentary issue with Travis CI. I relaunched the build.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we have pwsh installed on linux, and execute ps1 script from different shell it picks up the interpreter, just like ash/dash etc.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But pwsh
cannot interpret Bash scripts, right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes it works the other way around as well: executing an sh script with bash shebang in pwsh. :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Confirmed on IRC (#PowerShell @freenode
):
<kasper> with pwsh shebang, executing a ps1 script in sh or bash etc. invokes pwsh interpreter, but does the opposite works? invoking an sh script with bash shebang in pwsh?
<+jcotton> yes
<+jcotton> PS doesn't look at it, it just goes through the standard execution mechanisms
<+jcotton> shebangs are handled by the kernel, not by the shell
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@pchaigno, did it answer your question? I tested on Alpine Linux that using ash, you cannot execute bash script unless you have bash installed. Similarly from ash or bash to pwsh. The other way around also works, that from pwsh, you can execute ash or bash script. From that granularity, it is yet another scripting language with interpreter of its own (like all the rest of them).
@lildude, can this be selected for the next release? |
@kasper3 very cool! Thanks for this! |
Yup, once all the necessary reviews are in - I don't review until PRs have been reviewed by at least one of the other primary contributors. I plan to make a new release later this week so it'll hopefully be reviewed before then. |
Installed and confirmed a working #!/usr/bin/env pwsh
Write-Output "Hello, world." Couldn't find a package for PowerShell on Ubuntu, however. |
Thanks, for Ubuntu, we need to add repo and key. The instructions are given here for each version of Ubuntu: https://github.com/powershell/powershell#get-powershell. |
lib/linguist/languages.yml
Outdated
@@ -4306,6 +4308,7 @@ Shell: | |||
- ksh | |||
- mksh | |||
- pdksh | |||
- pwsh |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should be removed. The Shell
entry refers specifically to POSIX/Bourne-derived shells, rather than any generic shell.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Apparently, the PowerShell interpreter can also interpret Bash scripts. I was able to confirm under Ubuntu with a simple:
#!/usr/bin/env pwsh
echo "ok"
pwd
cd /
pwd
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure this is actually used as a shebang for Bash in-the-wild (on GitHub.com) though...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If so, users should be identifying it using #!/bin/sh
or a traditional interpreter.
There's already enough inconsistencies in the shell world...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
pwd
echo
cd /
, ls
, curl
all work on Windows PowerShell since Vista/2006 (without any Unix-y stuff installed). These are official registered aliases to actual powershell cmdlets. If you try bash-only syntax, that will fail.
@kasper3 Sorry, I just noticed Unless there's a completely unrelated interpreter called |
Adds support for hashbangs such as "#!/usr/bin/env pwsh". References: github-linguist/linguist#4073
@Alhadis, thanks, we can remove it for sure. However, PowerShell grammar is based on POSIX 1003.2 (aka POSIX.2) based on https://books.google.com/books?id=MYZQAAAAMAAJ. We can invoke sh,ash,csh,bash,dash etc. scripts from pwsh on Unix. We can conversely invoke pwsh from sh,ash,csh,bash,dash etc. on Unix. Is there any substantial missing feature that makes in ineligible to get enlisted under |
That would be usage, which, as far as I can see^Wsearch, is not there yet: |
No sensible shell-script author would ever write a POSIX shell-script and specify an interpreter using anything other than |
@Alhadis, done. In our build automation, we have started to use powershell DSC. Though our file extensions are |
Uh, no. It isn't POSIX-compatible whatsoever: a=2
if [ $a -eq 3 ]; then
printf '%s\n' Foo
elif [ $a -ne 3 ]; then
printf '%s\n' Bar
fi
Any POSIX-compliant shell should output There's also a side-by-side comparison here which clearly illustrates the differences in syntax between the various command-line interpreters. |
It says "based on", which is loosely subjective and denounced quite clearly in the form of a syntax error. I've no idea what they could have been referring to... |
Yup, that's what i said Perhaps POSIX |
The syntax if this; then that; fi In fact, Anyway, we've clearly established that PowerShell is by no means a "normal" interpreter for POSIX shell grammar. |
I meant maybe |
Utilities have nothing to do with shells†. There's GNU on Windows, which makes the standard Unix toolchain available to † - Unless they're implemented as built-ins (like Bash's |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for your patience @kasper3!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM. Thanks for the contribution and welcome to Linguist.
@lildude, was this change picked in the release?
We don't get the expected syntax highlighting: Get-ChildItem test as opposed to Get-ChildItem test |
Yup, it was. However looking back at this PR, Inline code blocks aren't affected by this so |
@lildude Perhaps interpreters should be included in aliases for a language though? |
A quick test reveled that is the case: https://github.com/kasper3/linguist/blob/master/test1 (extension less file with pwsh shebang get picked up for syntax highlighting). I agree with @pchaigno's idea of implicitly including interpreters as aliases. |
I agree, though some cases this may not be always desired. When I've got a free moment, I'll see if I can work out why this isn't the case. |
@kasper3 Could you please open a separate issue so we don't forget about this? |
And feel free to open a PR adding |
Thanks, I have opened a pull request #4117 :) |
PowerShell is using
#!/usr/bin/env pwsh
shebang on Unix these days andpwsh
is "the thing" that runs powershell :)I assume after this change, the following would show syntax highlighting:
```pwsh
Get-ChildItem .
```
as
Get-ChildItem .
Description
Checklist:
I am associating a language with a new file extension.
I am adding a new language.
I am fixing a misclassified language
I am changing the source of a syntax highlighting grammar
I am adding new or changing current functionality