Powershell: Tab-completion doesn't work properly when AllScope aliases are redefined.

Created on 20 Mar 2020  路  1Comment  路  Source: PowerShell/PowerShell

_Some_, but by no means all built-in aliases are defined with the AllScope option
(pwsh -noprofile { get-alias | ? options -like '*allscope*' }; as an aside: in Windows PowerShell it is _all but a few_).

Removing and redefining such aliases works for _invocation_, but makes _tab completion_ malfunction: The _original_ alias definition's parameters are still being completed.
(By contrast, tab-completion works fine for redefining a built-in alias _not_ defined with AllScope.)

Steps to reproduce

Note:

function foo { param($bar) "[$bar]" }  # Sample function

# Remove the built-in AllScopes `cd` alias and redefine it to execute `foo`.
Remove-Alias cd; Set-Alias cd foo

# Make sure that the redefinition worked:
cd -bar baz | Should -Be '[baz]'

Now tab-complete _interactively_:

# OK: tab-completion with `foo` directly.
PS> foo -b<tab> # -> '-bar'

# BROKEN: tab-completion with redefined `cd` alias
PS> cd -b<tab> # -> !! NO COMPLETION
# Still sees the original parameters:
PS> cd -lit<tab> # -> !! '-LiteralPath'

Note: I tried to use the following Pester test, but doing so actually makes the symptom _disappear_:

# !! Unexpectedly works, unlike in interactive use.
(TabExpansion2 'cd -b').CompletionMatches.CompletionText | Should -Be '-bar'

Expected behavior

All tests should succeed.

Actual behavior

Interactive tab-completion with the defined cd fails, as shown above.

Environment data

PowerShell Core 7.0
Issue-Bug WG-Interactive-IntelliSense

Most helpful comment

This is a duplicate of the PSReadLine issue https://github.com/PowerShell/PSReadLine/issues/453.
AllScope means the alias will be copied to every scope that gets created, and it's copied to PSReadLine's module scope.
When you remove the alias, you remove it from the global scope, but not from the scopes where that alias has already been copied, within those scopes, cd is still pointing to Set-Location, and calling TabExpansion2 within those scopes will of course operate on Set-Location.

See https://github.com/PowerShell/PSReadLine/issues/453#issuecomment-341629310 for details.
Note that this is not a problem only with PSReadLine, but with any modules that are already loaded before Remove-Alias cd.

>All comments

This is a duplicate of the PSReadLine issue https://github.com/PowerShell/PSReadLine/issues/453.
AllScope means the alias will be copied to every scope that gets created, and it's copied to PSReadLine's module scope.
When you remove the alias, you remove it from the global scope, but not from the scopes where that alias has already been copied, within those scopes, cd is still pointing to Set-Location, and calling TabExpansion2 within those scopes will of course operate on Set-Location.

See https://github.com/PowerShell/PSReadLine/issues/453#issuecomment-341629310 for details.
Note that this is not a problem only with PSReadLine, but with any modules that are already loaded before Remove-Alias cd.

Was this page helpful?
0 / 5 - 0 ratings