Skip to content

PowerShell Completion should not impact global namespace #2438

Description

@jfishe

chezmoi PowerShell Completion missing Native parameter #5133 uses cobra to generate PowerShell completion.
Cobra exposes a function and filter to PowerShell global namespace.
This should be avoided because it prevents lazy loading of shell completion and
could create name collision.

Recommend moving the globals inside the completion [scriptblock].

#2437 is a related issue with the Native parameter.

Describe the bug

chezmoi completion powershell produces:

# powershell completion for chezmoi                              -*- shell-script -*-

function __chezmoi_debug {
    if ($env:BASH_COMP_DEBUG_FILE) {
        "$args" | Out-File -Append -FilePath "$env:BASH_COMP_DEBUG_FILE"
    }
}

filter __chezmoi_escapeStringWithSpecialChars {
    $_ -replace '\s|#|@|\$|;|,|''|\{|\}|\(|\)|"|`|\||<|>|&','`$&'
}

[scriptblock]${__chezmoiCompleterBlock} = {
    param(
            $WordToComplete,
            $CommandAst,
            $CursorPosition
        )

<snip>

}

Register-ArgumentCompleter -CommandName 'chezmoi' -ScriptBlock ${__chezmoiCompleterBlock}

## To reproduce

```powershell
pwsh --noprofile
chezmoi completion powershell

Expected behavior

# powershell completion for chezmoi                              -*- shell-script -*-

[scriptblock]${__chezmoiCompleterBlock} = {
    param(
            $WordToComplete,
            $CommandAst,
            $CursorPosition
        )

    function __chezmoi_debug {
        if ($env:BASH_COMP_DEBUG_FILE) {
            "$args" | Out-File -Append -FilePath "$env:BASH_COMP_DEBUG_FILE"
        }
    }

    filter __chezmoi_escapeStringWithSpecialChars {
        $_ -replace '\s|#|@|\$|;|,|''|\{|\}|\(|\)|"|`|\||<|>|&','`$&'
    }

<snip>
}

Register-ArgumentCompleter -CommandName 'chezmoi' -ScriptBlock ${__chezmoiCompleterBlock}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions