PowerShell Completion should not impact global namespace
Author: jfisheCreated Jul 19, 2026Updated Jul 26, 2026
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].
https://github.com/spf13/cobra/issues/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}
Source: spf13/cobra