Skip to content

zsh --show-completion function name breaks autoload convention #1864

Description

@HaleTom

Describe the bug

When --show-completion=zsh output is installed as an autoloaded file in $fpath (the standard packaging convention), zsh completion silently fails because the generated function name doesn't match the autoloaded filename.

Reproduction

hf --show-completion=zsh

Output:

#compdef hf

_hf_completion() {
  eval $(env _TYPER_COMPLETE_ARGS="${words[1,$CURRENT]}" _HF_COMPLETE=complete_zsh hf)
}

compdef _hf_completion hf

Install as packages do:

hf --show-completion=zsh > /usr/share/zsh/site-functions/_hf

Then in zsh:

% hf <Tab>  # only shows filenames, no command completions

Root cause

zsh's compinit reads #compdef hf and registers hf → autoload _hf. The autoloaded file /usr/share/zsh/site-functions/_hf must define a function named _hf. But Typer generates _hf_completion, so the autoload produces no matching function and completion fails silently.

Why this only affects packaging, not --install-completion

--install-completion writes compdef _hf_completion hf into .zshrc. When sourced, compdef registers the function by whatever name it has — the filename is irrelevant. But when installed as an autoloaded file in $fpath, the filename IS the function name.

Suggested fix

Generate _{prog_name} instead of _{prog_name}_completion. The _completion suffix serves no purpose — the #compdef directive already wires up the completion.

#compdef hf

_hf() {
  eval $(env _TYPER_COMPLETE_ARGS="${words[1,$CURRENT]}" _HF_COMPLETE=complete_zsh hf)
}

compdef _hf hf

This matches zsh autoload convention and keeps --install-completion working (since compdef can reference any name).

Workaround (for package maintainers)

hf --show-completion=zsh | sed 's/_hf_completion/_hf/g' > _hf

Environment

  • Typer 0.26.7 (latest)
  • zsh 5.9

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions