Skip to content

Tab completion runs the command since v3 #193

Closed

Description

@hanoii

Problem

Since v3 (the move to cobra), tab completion through the legacy --generate-bash-completion flag runs the command instead of completing it.

Completion scripts written for the urfave/cli era append the flag to the words typed so far. The bash script in the docs does this:

opts=$( ${COMP_WORDS[@]:0:$COMP_CWORD} --generate-bash-completion )

So ahoy deploy <TAB> executes ahoy deploy --generate-bash-completion. In v3 the flag pre-parser in flag.go stops at the command name, so the flag reaches deploy as a plain argument and deploy runs.

Reproduce

# .ahoy.yml
ahoyapi: v2
commands:
  foo:
    cmd: echo RAN foo "$@"
  bar:
    imports: [sub.yml]
# sub.yml
ahoyapi: v2
commands:
  baz:
    cmd: echo RAN baz
  qux:
    cmd: echo RAN qux
Command v2.5.0 v3.0.1 / master
ahoy foo --generate-bash-completion nothing RAN foo
ahoy foo dis --generate-bash-completion nothing RAN foo dis
ahoy bar --generate-bash-completion baz qux help help for bar
ahoy bar ba --generate-bash-completion baz qux help help for bar

The existing bats test "Bash completion continues to work" only covers the flag ahead of the command, which still works.

Related

The root command's ValidArgsFunction returns every command name, including hidden ones such as __complete. Cobra already offers the visible names, so with ahoy completion bash every command is listed twice.


This issue was AI generated (Claude Code) and human tested and validated.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      ⌂ 换网址