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

Prefer powershell script over cmd scripts #1854

Open
wants to merge 5 commits into
base: master
Choose a base branch
from

Conversation

balthild
Copy link

@balthild balthild commented Nov 6, 2024

Closes #1853

@xiaq
Copy link
Member

xiaq commented Jan 3, 2025

Thanks, I like the idea in principle, but looking at the implementation, a problem is that there are actually two different PowerShell's:

  • The classical "Windows PowerShell" that comes pre-installed in modern Windows versions, with the binary name powershell.exe.
  • The modern "PowerShell Core" that needs to be installed by the user, with the binary name pwsh.exe.

As far as I know, the modern pwsh.exe is not completely backwards compatible with the classical powershell.exe. The current implementation picks powershell.exe, but I don't feel Elvish should be making the choice on the user's behalf.

We could in principle implement an option for choosing which PowerShell to use, but I feel this would be overcomplicating what's supposed to simplify things for users.

I noticed that PowerShell itself will happily execute a .ps1 file in PATH (both powershell.exe and pwsh.exe, although the former has an annoying policy thing that prevents it by default), but then PowerShell knows what its own path is, and if you're already using say pwsh.exe, this is arguably some signal that you prefer it. The corollary seems to be that Elvish is in a position to special-case .elv files on Windows...

All in all I'm leaning towards rejecting this PR. Let me know if I missed something.

@balthild
Copy link
Author

balthild commented Jan 15, 2025

Maybe a solution is to find out which program that .ps1 files are associated with, but it requires some extra work to implement. If you think this works, I may take some time to try figuring it out.

@balthild
Copy link
Author

balthild commented Jan 26, 2025

Hello! Recently, I found in an MS doc that pwsh.exe could be compatible with powershell.exe through .NET Standard 2.0:

With the advent of .NET Standard 2.0, PowerShell can load many traditional Windows PowerShell modules without modification. Additionally, PowerShell 7 includes a Windows PowerShell Compatibility feature that allows you to use Windows PowerShell modules that still require the full framework.

Maybe it's OK to just try pwsh.exe first, and fallback to powershell.exe if pwsh.exe doesn't exist?

Also, since PowerShell Core is cross-platform, their script usually contains a shebang (#!/usr/bin/env pwsh). Maybe a strategy is to only use pwsh.exe if a shebang exists?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Prefer .ps1 than .cmd when both file exists in path
2 participants