From 950c242d1de937a9a506b2c66330d0d1fc65de7c Mon Sep 17 00:00:00 2001 From: Scott Andrews Date: Thu, 19 Nov 2026 00:34:25 +0000 Subject: [PATCH 1/2] Install shell completions for devcontainers To register completions in current shell source "$(pwd)/build/scripts/completions.sh" To register completions in new shells. Add to the init.sh script: echo "source \"$(pwd)/build/scripts/completions.sh\"" | tee -a ~/.zshrc ~/.bashrc >/dev/null Signed-off-by: Scott Andrews --- .devcontainer/init.sh | 6 +++--- README.md | 11 ++++++++++- build/completions.jq | 15 +++++++++++++++ build/scripts/completions.sh | 35 +++++++++++++++++++++++++++++++++++ build/tools.mk | 12 ++++++++++++ build/tools/Cargo.toml | 9 +++++++++ tools/Cargo.toml | 4 ++++ 7 files changed, 88 insertions(+), 4 deletions(-) create mode 100644 build/completions.jq create mode 100644 build/scripts/completions.sh diff --git a/.devcontainer/init.sh b/.devcontainer/init.sh index e8eb4d8..cfe49cf 100755 --- a/.devcontainer/init.sh +++ b/.devcontainer/init.sh @@ -1,4 +1,4 @@ -!/usr/bin/env bash +#!/usr/bin/env bash # Setup script run once for new devcontainers to init the environment. @@ -6,8 +6,8 @@ set -euo pipefail curl -L --proto '=https' --tlsv1.2 -sSf https://raw.githubusercontent.com/cargo-bins/cargo-binstall/main/install-from-binstall-release.sh | bash -echo "PATH=\"\$(make -s tools-path):\${PATH}\"" >> ~/.zshrc -echo "PATH=\"\$(make -s tools-path):\${PATH}\"" >> ~/.bashrc +echo "PATH=\"\$(make -s tools-path):\${PATH}\"" | tee -a ~/.zshrc ~/.bashrc >/dev/null +echo "source \"$(pwd)/build/scripts/completions.sh\"" | tee -a ~/.zshrc ~/.bashrc >/dev/null make tools wit diff --git a/README.md b/README.md index 01da6a8..39775b2 100644 --- a/README.md +++ b/README.md @@ -12,7 +12,7 @@ build/ synced, never edited in the repository VERSION the release synced, e.g. v0.2.0 MANIFEST a checksum of each synced file, checked by `make check-build` build.mk the targets every repository has, includes the other make files - tools.mk wit.mk components.mk publish.mk sync.mk pinned.jq local.jq + tools.mk wit.mk components.mk publish.mk sync.mk pinned.jq local.jq completions.jq tools/Cargo.toml the tools the build provides, see Tools scripts/ publish.sh, bump-version.sh, update-build.sh, ... .github/workflows/ ci.yaml, bump-version.yaml, release.yaml, update-build.yaml: synced @@ -67,6 +67,15 @@ A repository changes a tool the build provides, or adds its own, without changin - **A binary built elsewhere**, to try a change: set its variable on the command line, e.g. `make WKG=../wkg/target/release/wkg`. The scripts the recipes run find the installed tools on the `PATH`. - **Its own tools**, e.g. `wasmtime-cli` for its tests: pin them in `tools/Cargo.toml` like an override. A repository with no tools of its own needs no `tools/Cargo.toml`, nor its dependabot entry. +The tools complete in bash and zsh once their completions are registered at the shell's startup, after the tools are on the `PATH`: + +```sh +PATH="$(make -s tools-path):${PATH}" +source /path/to/repository/build/scripts/completions.sh +``` + +A binary that prints its completions is listed in `[package.metadata.completions]` with the arguments that print them, the shell's name appended, e.g. `wasm-tools = ["completion"]` for `wasm-tools completion bash`. The build lists those of the tools it provides, cosign too, and a repository lists its own tools in its `tools/Cargo.toml`, or overrides one of the build's, `[]` for none. `make tools` generates `target/tools/completions` from them, read as each shell starts, a tool not installed yet is skipped. + ## Targets `make` runs `tools`, `generate`, `components` and `test`. Other targets: `wit`, `publish`, `tools-path`, `clean`, `clean-all`, `update-build`, `check-build`, `print-`. diff --git a/build/completions.jq b/build/completions.jq new file mode 100644 index 0000000..aaed8ac --- /dev/null +++ b/build/completions.jq @@ -0,0 +1,15 @@ +# generates target/tools/completions from `cargo metadata --no-deps` of build/tools/Cargo.toml, +# followed by that of the repository's tools/Cargo.toml when it has one, slurped, see build/tools.mk. +# Each line is a binary and the arguments that print its completions, quoted for the shell, e.g. +# `'wasm-tools' 'completion'`, read by build/scripts/completions.sh. A binary the repository lists +# overrides the build's, one listed with no arguments has none. +def completions($manifest): + (.packages[0].metadata.completions // {}) + | to_entries[] + | if (.value | type) == "array" and all(.value[]; type == "string") then . + else error("\(.key) in [package.metadata.completions] in \($manifest) is not a list of arguments") end; +([.[0] | completions("build/tools/Cargo.toml")] + [.[1] // empty | completions("tools/Cargo.toml")]) +| reduce .[] as $binary ({}; .[$binary.key] = $binary.value) +| to_entries | sort_by(.key)[] +| select(.value != []) +| [.key] + .value | @sh diff --git a/build/scripts/completions.sh b/build/scripts/completions.sh new file mode 100644 index 0000000..50118fe --- /dev/null +++ b/build/scripts/completions.sh @@ -0,0 +1,35 @@ +# Registers the shell completions of the installed tools, sourced by bash or zsh at startup once the +# tools are on the PATH, e.g. in ~/.bashrc: +# +# PATH="$(make -s tools-path):${PATH}" +# source /path/to/repository/build/scripts/completions.sh +# +# The tools that have completions, and how each prints them, are listed in +# [package.metadata.completions] of build/tools/Cargo.toml and the repository's tools/Cargo.toml, +# generated into target/tools/completions by `make tools`. Generated as each shell starts, so a tool +# installed or bumped since completes in new shells. A tool that isn't installed is skipped, as is +# one that fails to print its completions. + +__componentized_completions() { + local script completions shell line + if [ -n "${ZSH_VERSION:-}" ]; then + script="${(%):-%x}" + shell=zsh + else + script="${BASH_SOURCE[0]}" + shell=bash + fi + completions="$(cd "$(dirname "$script")/../.." && pwd)/target/tools/completions" + [ -f "$completions" ] || return 0 + while IFS= read -r line; do + # the binary and its arguments, quoted by build/completions.jq + eval "set -- $line" + command -v "$1" > /dev/null 2>&1 || continue + # only a script the tool printed, one that fails may print its usage to stdout, which isn't + # run, and none reads the terminal, which would hang the shell + line=$("$@" "$shell" < /dev/null 2> /dev/null) || continue + [ -n "$line" ] && eval "$line" + done < "$completions" +} +__componentized_completions +unset -f __componentized_completions diff --git a/build/tools.mk b/build/tools.mk index 921c14a..91818f5 100644 --- a/build/tools.mk +++ b/build/tools.mk @@ -55,6 +55,18 @@ $(TOOLS_MK): $(TOOLS_MANIFESTS) build/pinned.jq @echo "TOOLS_MANIFESTS_PINNED := $(TOOLS_MANIFESTS)" >> $@.tmp @mv $@.tmp $@ +# the binaries that print shell completions and their arguments, read by build/scripts/completions.sh +# as a shell starts, see [package.metadata.completions] in build/tools/Cargo.toml. Generated with the +# tools, the repository's tools/Cargo.toml is read last, overriding the build's. +TOOLS_COMPLETIONS := target/tools/completions + +tools: $(TOOLS_COMPLETIONS) + +$(TOOLS_COMPLETIONS): $(TOOLS_MANIFESTS) build/completions.jq + @mkdir -p $(@D) + @set -o pipefail; for manifest in $(TOOLS_MANIFESTS); do cargo metadata --manifest-path $$manifest --no-deps --format-version 1; done | jq -rs -f build/completions.jq > $@.tmp + @mv $@.tmp $@ + # the packages of the workspace, generated again when LOCAL_TOOLS changes. The workspace's members may # change without changing Cargo.toml or Cargo.lock, `make clean-tools` generates it again. $(LOCAL_TOOLS_MK): Cargo.toml Cargo.lock $(filter-out $(TOOLS_MK) $(LOCAL_TOOLS_MK),$(MAKEFILE_LIST)) build/local.jq diff --git a/build/tools/Cargo.toml b/build/tools/Cargo.toml index 3a47d6f..5040e5c 100644 --- a/build/tools/Cargo.toml +++ b/build/tools/Cargo.toml @@ -26,6 +26,15 @@ wasm-opt = ["wasm-opt"] wasm-tools = ["wasm-tools"] wkg = ["wkg"] +# the arguments of the binaries that print their shell completions, the shell's name is appended, +# e.g. `wasm-tools completion bash`, registered by build/scripts/completions.sh. A binary installed +# other than with cargo is listed too, e.g. cosign, see build/tools.mk. A repository adds its own +# in its tools/Cargo.toml, or overrides one with `[]`, e.g. for a tool it builds without them. +[package.metadata.completions] +componentized-build = ["completions"] +cosign = ["completion"] +wasm-tools = ["completion"] + [dependencies] cargo-cyclonedx = "=0.5.10" componentized-build-cli = "=0.1.6" diff --git a/tools/Cargo.toml b/tools/Cargo.toml index 4608866..d59e200 100644 --- a/tools/Cargo.toml +++ b/tools/Cargo.toml @@ -19,6 +19,10 @@ path = "lib.rs" [package.metadata.bins] wasmtime-cli = ["wasmtime"] +# the arguments of the binaries that print their shell completions, see build/tools/Cargo.toml +[package.metadata.completions] +wasmtime = ["completion"] + [dependencies] wasmtime-cli = "=50.0.1" From 80b4ca00f7c3c8cfc97f288d442f43efaeede558 Mon Sep 17 00:00:00 2001 From: Scott Andrews Date: Thu, 19 Nov 2026 00:42:17 +0000 Subject: [PATCH 2/2] fix shellcheck warnings Signed-off-by: Scott Andrews --- build/scripts/completions.sh | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/build/scripts/completions.sh b/build/scripts/completions.sh index 50118fe..ae31716 100644 --- a/build/scripts/completions.sh +++ b/build/scripts/completions.sh @@ -1,3 +1,4 @@ +# shellcheck shell=bash # Registers the shell completions of the installed tools, sourced by bash or zsh at startup once the # tools are on the PATH, e.g. in ~/.bashrc: # @@ -10,9 +11,13 @@ # installed or bumped since completes in new shells. A tool that isn't installed is skipped, as is # one that fails to print its completions. +# the arguments are the ones each line of completions sets +# shellcheck disable=SC2120 __componentized_completions() { local script completions shell line if [ -n "${ZSH_VERSION:-}" ]; then + # zsh's path of the sourced script, BASH_SOURCE in bash + # shellcheck disable=SC2296 script="${(%):-%x}" shell=zsh else