|
| 1 | +--- |
| 2 | +RFC: 0007 |
| 3 | +Author: Jason Shirk |
| 4 | +Status: Draft |
| 5 | +Area: Command Resolution |
| 6 | +--- |
| 7 | + |
| 8 | +# Weak Aliases |
| 9 | + |
| 10 | +This RFC is an attempt to address the communities desire to [remove the aliases `curl` and `wget`]((https://github.com/PowerShell/PowerShell/pull/1901)) from Windows PowerShell. |
| 11 | + |
| 12 | +## Motivation |
| 13 | + |
| 14 | +The introduction of the aliases `curl` and `wget` happened in PowerShell V3 and was an unintentional breaking change. |
| 15 | +Regretfully, removing these aliases now would also be a breaking change. |
| 16 | + |
| 17 | +An analysis of scripts from various sources was performed to understand the usage patterns of `curl` in PowerShell scripts. |
| 18 | +The source of scripts included the Windows code base, GitHub and a corpus of scripts collected by the PowerShell team from various sources like poshcode.org. |
| 19 | + |
| 20 | +There is a [gist](https://gist.github.com/lzybkr/3cd091334355f381d1d6ee7acfad5a48) with the code I used to analyze the scripts from GitHub. |
| 21 | + |
| 22 | +My analysis concludes that `curl` calls `Invoke-WebRequest` approximately twice as often as `curl.exe`. |
| 23 | + |
| 24 | +In the Windows code base, usage of `curl` was not common, but was primarily used as an alias for `Invoke-WebRequest`. |
| 25 | + |
| 26 | +I analyzed 667 scripts on GitHub that contained the string `curl`. |
| 27 | + |
| 28 | +In 465 scripts, `curl` was not used as a command name. |
| 29 | + |
| 30 | +In 124 scripts, my analysis concluded that `curl.exe` was the intended command, but 3 specific scripts appeared many times in the data set. Eliminating those duplicates, we end up with 45 scripts using `curl.exe`. |
| 31 | + |
| 32 | +The remaining scripts are assumed to use `Invoke-WebRequest` - approximately 75 scripts. |
| 33 | + |
| 34 | +Removing the aliases breaks scripts in another unexpected way as well. |
| 35 | +Approximately 10 or so scripts remove the alias, presumably to call `curl.exe`, e.g. |
| 36 | +```powershell |
| 37 | +rm alias:curl |
| 38 | +``` |
| 39 | +If the alias does not exist, this will result in an error, possibly breaking the script in an unexpected way. |
| 40 | + |
| 41 | +There are other scenarios where removal of the aliases cause undue difficulties for our customers. |
| 42 | + |
| 43 | +* PowerShell usage in compiled code - more difficult to discover |
| 44 | +* Windows PowerShell updates as part of Windows, possibly breaking *users* of scripts that don't know how to fix the script |
| 45 | + |
| 46 | +The conclusion of this analysis is that removing the aliases would cause enough problems that we should prefer another solution. |
| 47 | + |
| 48 | +## Specification |
| 49 | + |
| 50 | +This proposal is inspired by the notion of a [weak symbol](https://en.wikipedia.org/wiki/Weak_symbol) used by C/C++ compilers in the ELF and COFF object formats. |
| 51 | + |
| 52 | +In PowerShell, an alias has the highest order of command precedence. |
| 53 | +This proposal introduces a weak alias which would have the lowest order of command precedence. |
| 54 | + |
| 55 | +A weak alias can be created in various ways - both in C# and PowerShell: |
| 56 | +```C# |
| 57 | + var alias = new SessionStateAliasEntry("curl", "Invoke-webRequest"); |
| 58 | + alias.Weak = true; |
| 59 | + // Add alias to InitialSessionState |
| 60 | +``` |
| 61 | + |
| 62 | +```powershell |
| 63 | +New-Alias -Name curl -Value Invoke-WebRequest -Weak |
| 64 | +``` |
| 65 | + |
| 66 | +Command resolution changes as follows: |
| 67 | + |
| 68 | +1. During the search for aliases, if a weak alias is found, it is saved and the search resumes with looking for functions, cmdlets, and external commands. |
| 69 | +2. If no other commands are found, the saved weak alias is used. |
| 70 | + |
| 71 | +Note that this lookup describes a long standing confusing feature where `Get-*` commands may be invoked without the `Get-` prefix. |
| 72 | +With the introduction of weak aliases, we can codify this quirk of command resolution by creating a weak alias for all `Get-*` cmdlets, e.g. |
| 73 | + |
| 74 | +```powershell |
| 75 | +New-Alias -Name Process -Value Get-Process -Weak |
| 76 | +New-Alias -Name ChildItem -Value Get-ChildItem -Weak |
| 77 | +# etc |
| 78 | +``` |
| 79 | + |
| 80 | +This also helps with discoverability when `Get-Command` returns these weak aliases. |
| 81 | + |
| 82 | +## Alternate Proposals and Considerations |
| 83 | + |
| 84 | +### Considerations |
| 85 | + |
| 86 | +Adding weak aliases for `curl` and `wget` could still be considered a breaking change. |
| 87 | +A script that means to use `Invoke-WebRequest` may stop working correctly if `curl.exe` is installed on a system. |
| 88 | + |
| 89 | +### Alternate Proposals |
| 90 | + |
| 91 | +Another way to implement a weak alias is to allow multiple definitions, e.g. |
| 92 | + |
| 93 | +```powershell |
| 94 | +New-Alias -Name curl -Definition 'curl.exe','Invoke-WebRequest' |
| 95 | +``` |
| 96 | + |
| 97 | +Resolution would try each definition in the order specified. |
| 98 | +This design suffers a small problem of knowing the exact extension for the external command, which might be undesirable if the external command is often wrapped in another script, e.g. `curl.cmd`. |
| 99 | + |
| 100 | +There are some suggestions to choose the appropiate target command based on parameters. |
| 101 | +This suggestion has some significant flaws as in some cases, it is impossible to distinguish which target command was desired. |
0 commit comments