1.1.12 Shell Tab Completion

extra

Shell completion is a small CLI upgrade that pays back every day. For Bazel, it covers two kinds of suggestions: the command surface itself and target labels in the current workspace. Bazelisk's generated Bash script supports both.1,2

Generate Completion From Bazelisk

Bazelisk exposes a completion command for Bash and Fish:1

source <(bazelisk completion bash)

bazelisk completion fish > ~/.config/fish/completions/bazel.fish

For Bash, source the generated script directly while you are inside a Bazel workspace, or save it and source that file from your shell startup configuration.1 Fish loads the generated file from its normal completion directory.

The generated script comes from the active Bazel version's installer.1 If you save it to a file, regenerate it after upgrading Bazel or changing the version pinned by the workspace.

Choose How Bash Finds Target Labels

By default, the Bash script looks through BUILD files for target candidates.2 This is fast and does not ask a running Bazel server to load the package, but unusual formatting or targets created by macros can make its suggestions incomplete.

For more accurate target completion, enable the script's query-backed mode before sourcing it:

export BAZEL_COMPLETION_USE_QUERY=true
source <(bazelisk completion bash)

Now the completer uses bazel query for the package being completed.2 This finds targets more accurately, but a completion can pause while Bazel answers and may fail when the package itself has loading errors. Start with the default mode. Enable query-backed completion if missing target suggestions are a real problem. 1.1.8 Basic Query explains the query being used underneath.

key takeaway

Bazelisk generates version-matched completion for commands, flags, and target labels. Regenerate a saved script when the pinned Bazel version changes. Bash uses fast BUILD-file inspection by default. Set BAZEL_COMPLETION_USE_QUERY=true when more accurate target discovery is worth the extra latency and dependence on successful package loading.

Check your understanding · 2 questions

1.Match each completion layer to what makes it stale:

Drag each answer onto the matching prompt, or click an answer and then click a prompt

Answers
Generated Bazel CLI completion
Query-backed target-label completion

2.What should provide candidates for a lightweight target-label completer?

Select one answer

0 of 2 answered

Footnotes

  1. Bazelisk — A user-friendly launcher for Bazelbazelisk completion bash|fish, sourcing or saving the generated script, and the note that the script is tied to the active Bazel version 1 2 3 4

  2. Bazel Bash completion template — package and target completion, the default BUILD-file heuristic, and the optional BAZEL_COMPLETION_USE_QUERY path 1 2 3