Skip to content

shell completions do not expand ~ or environment variables #1577

@twpayne

Description

@twpayne

When using fish completions with a cobra.Command.ValidArgsFunction to specify custom completions, leading ~s and environment variables are not expanded before the argument to complete is passed to the ValidArgsFunction.

For example, if the user types the following to trigger shell completion:

$ command arg ~/.z<tab>

ValidArgsFunction is called with the toComplete argument equal to ~/.z. The tilde expansion should be done before this, i.e. the toComplete argument should be /home/user/.z where /home/user is the user's home directory.

Similarly, if the user types:

$ command arg $SOME_VAR/.z

then ValidArgsFunction is called with the toComplete argument equal to $SOME_VAR/.z. Instead, toComplete should have the value with $SOME_VAR expanded.

This is different to other shell completions (e.g. zsh), where ~ and environment variables are expanded by the shell before being passed to ValidArgsFunction.

The expansion of ~ and environment variables must be done in the fish shell itself. It cannot be done by the ValidArgsFunction because:

  • The value of environment variables might not be known. For example, if toComplete contains an unexported environment variable then its value is not available to the process executing ValidArgsFunction.
  • The exact interpretation of ~ depends on the shell. Example common interpretations include ~ for the user's home directory, ~/file for a file in the user's home directory, ~username for a different user's home directory, and ~username/file for a file in a different user's home directory. It is unreasonable for the ValidArgsFunction to emulate fish's behavior exactly.
  • As the fish completions behave differently to other shell completions, implementing fish's logic in ValidArgsFunction would break other shell completions.

Refs twpayne/chezmoi#1796 (comment).

Metadata

Metadata

Assignees

No one assigned

    Labels

    area/shell-completionAll shell completionskind/bugA bug in cobra; unintended behaviorlifecycle/frozenPrevents GitHub actions from labeling issues / PRs with stale and rottentriage/needs-infoNeeds more investigation from maintainers or more info from the issue provider

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions