Skip to content

fix: filter completion candidates to aliases and agent YAMLs only#3008

Open
hamza-jeddad wants to merge 5 commits into
mainfrom
fix/completion-filter-candidates
Open

fix: filter completion candidates to aliases and agent YAMLs only#3008
hamza-jeddad wants to merge 5 commits into
mainfrom
fix/completion-filter-candidates

Conversation

@hamza-jeddad
Copy link
Copy Markdown
Contributor

@hamza-jeddad hamza-jeddad commented Jun 5, 2026

Previously, docker agent run <TAB> included all directories from cwd in completions.

What this PR fixes:

  • Directories are excluded from plain-prefix (no path typed) completion
  • Dotfile YAMLs (e.g. .golangci.yml) are excluded

Known limitation / follow-up: Non-dotfile, non-agent YAMLs like Taskfile.yml and docker-compose.yaml are regular .yml files and still appear — filtering those is out of scope for this PR.

Implementation: completeAlias now calls a new completeAgentYAMLInCwd helper instead of completeAgentFilename for plain-prefix input. completeAgentFilename is unchanged — ./ and / path drill-down still works.

Also sorts alias names alphabetically.

- Replace completeAgentFilename fallthrough in completeAlias with a new
  completeAgentYAMLInCwd helper that only returns non-dotfile *.yaml/yml
  files from the current directory (no directories, no dotfiles)
- Sort alias names alphabetically using slices.Sorted(maps.Keys(...))
- Add tests: no directories in plain-prefix completion, no dotfile YAMLs,
  path drill-down still works with ./ prefix, aliases are sorted
@hamza-jeddad hamza-jeddad requested a review from a team as a code owner June 5, 2026 13:15
Copy link
Copy Markdown

@docker-agent docker-agent left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Assessment: 🟢 APPROVE

The fix correctly narrows tab-completion candidates to aliases and agent YAML files, excluding directories and dotfiles. The sorting via slices.Sorted(maps.Keys(...)) is a clean improvement. Test coverage is solid — four new tests cover the key scenarios. One minor edge case noted inline.

Comment thread cmd/root/completion.go Outdated
}
var out []string
for _, e := range entries {
if e.IsDir() {
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[LOW] Symlinks-to-directories with .yaml/.yml names pass the IsDir() guard

os.ReadDir on Linux returns DirEntry entries where IsDir() returns false for symlinks — even when the symlink target is a directory. This means a symlink like ln -s /some/dir my-agent.yaml in the working directory would pass all guards in completeAgentYAMLInCwd and appear as a valid YAML completion candidate, even though it is not a file.

A more robust check would be:

if !e.Type().IsRegular() {
    continue
}

This correctly excludes directories, symlinks, pipes, devices, and other non-regular entries.

The practical impact is low (unusual edge case for a completion helper), but using IsRegular() would make the intent explicit and correct.

…MLInCwd

Replace e.IsDir() with !e.Type().IsRegular() so that symlinks-to-directories
and other non-regular entries (pipes, devices) are also excluded.
@hamza-jeddad
Copy link
Copy Markdown
Contributor Author

Addressed in the latest commit: replaced e.IsDir() with !e.Type().IsRegular() so symlinks-to-directories and other non-regular entries are also excluded.

…in completion

Add TestCompleteAlias_PlainNonAgentYAMLStillAppears to make the current
contract explicit: regular non-dotfile .yml/.yaml files (e.g. Taskfile.yml)
are included in plain-prefix completions. Filtering those is a known
follow-up, not covered by this PR.
…ker CLI

When Docker CLI delegates tab-completion to the plugin it calls:
  docker-agent __complete agent <subcommand> <args...>

RunningStandalone() returns true here (os.Args[1] is '__complete', not
the metadata subcommand), so the cobra root command received 'agent' as
the first argument to __complete and could not resolve the subcommand
tree. The result was zsh falling back to default filesystem completion,
showing every file and directory in cwd.

Fix: strip the redundant 'agent' token in stripPluginNameFromCompletionArgs()
before passing args to cobra, so 'docker agent run <TAB>' correctly
delegates to completeRunExec and returns only aliases and agent YAMLs.
@aheritier aheritier added area/cli CLI commands, flags, output formatting kind/fix PR fixes a bug (maps to fix: commit prefix) labels Jun 5, 2026
- Reorder isManagementInvocation and stripPluginNameFromCompletionArgs
  so each function has its own doc comment (previously the merged block
  was attributed entirely to stripPluginNameFromCompletionArgs)
- Add test case for stripPluginNameFromCompletionArgs with len==2 input
  ([__complete, agent]) to document the no-trailing-args edge case
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/cli CLI commands, flags, output formatting kind/fix PR fixes a bug (maps to fix: commit prefix)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants