YorSubs
YorSubs

Reputation: 4080

PowerShell alias is not seeing parameters during tab completion

I am using a function that can do some change directory functionality and then redirect / re-alias cd to that function.

function go ($jumpto, [switch]$DisableAliasCD, [switch]$ResetGoHash, [switch]$ResetCDHistory) {
    <stuff>
}

Set-Alias cc go
if (Test-Path Alias:cd) { Remove-Item -Path Alias:cd }    # Remove the default:   cd -> Set-Location
# Remove-Alias was not added until PS v6 so have to use Remove-Item with the Alias PSProvider
Set-Alias cd go -Option AllScope

Checking the syntax of each of go, cc, cd

get-command go -syntax
go [[-jumpto] <Object>] [-DisableAliasCD] [-ResetGoHash] [-ResetCDHistory]
get-command cc -syntax
go
get-command cd -syntax
go

This seems to make sense. However, I find that IntelliSense works fine for cc (i.e. if I type cc - and then Tab the above 4 Parameters appear, but for cd nothing happens.

Is this a bug? Or does this have something to do with declaring the Set-Alias cd with the AllScope option (which I have to do, the Set-Alias does not work without it for the cd)

Upvotes: 2

Views: 349

Answers (1)

mklement0
mklement0

Reputation: 439747

You seem to have a run into a bug, still present as of PowerShell 7.0:

Most (Windows PowerShell) / a few (PowerShell [Core] 6+) built-in aliases are defined with the AllScope option.
(You can discover them with
powershell -NoProfile { Get-Alias | ? options -like '*AllScope*' }
/
pwsh -NoProfile { Get-Alias | ? options -like '*AllScope*' }).

Redefining any alias works correctly in terms of invocation.

However, in terms of tab completion, redefined AllScope ones malfunction, as you describe: The original alias definition's parameters are still being completed.

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

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

# Make sure that the redefinition works:
cd -bar baz # OK: prints '[baz]'

# Try to tab-complete:
cd -b<tab> # NO COMPLETION instead of the expected '-bar'

# Try a parameter from `cd`'s *original* definition, `Set-Location`:
cd -li<tab> # UNEXPECTEDLY EXPANDS TO '-LiteralPath'

This problematic behavior has been reported in this GitHub issue.

Upvotes: 1

Related Questions