From e8fc468a53aa727ff9e6d69a557b0f024f063e86 Mon Sep 17 00:00:00 2001 From: Victor Vogel Date: Mon, 9 Nov 2026 20:27:09 +0000 Subject: [PATCH] tools: --help finds its header by a relative path from another directory Run by a relative path from somewhere other than the repository root (cd ..; omarchy-mobile/tools/build-packages --help, or ./release --help from tools/), the eight tools that cd to the repository before they read their options failed --help: they passed "$0", which is relative to the directory they had just left, and awk couldn't open it (exit 2). Derek pointed this out on #559 and suggested usage_from_header as the place to fix it. usage_from_header now takes only FILE's name and reads that tool from the tools directory above tools/lib/usage.sh. The helper's own path is still valid when it runs, because every caller has just sourced it from its current directory, so this fixes all eight callers in one place with no call site changed. The tools that resolve their own path first (fetch-toolchain, kernel-pr-check, publish-kernel) or don't cd before --help (build-flame-probe, phone-smoke, phone-demo), and the bundle's inline-awk tools (install-phone, nv-backup, phone-provision), already worked; their files are unchanged. phone-smoke and phone-demo still load the helper only on the help path. tests/tools-help now runs every tool's --help from the repository's parent and from tools/ by a relative path and compares the output and exit code with a run from the root. On main it fails at tools/build-packages. --help from the root is byte-identical before and after for all 17 tools (16 exit 0, phone-mirror exit 2). Tested: tests/tools-help, tools/pr-check origin/main, tests/kernel-pr-check, tests/install-phone, tests/make-bundle, and tests/all, which passes except tests/release-upload. That suite fails the same way on main because the worktree path contains "/.". Co-Authored-By: Claude Opus 5.5 --- tests/tools-help | 13 +++++++++++++ tools/lib/usage.sh | 12 ++++++++++-- 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/tests/tools-help b/tests/tools-help index cb27c288f..cb19063bf 100755 --- a/tests/tools-help +++ b/tests/tools-help @@ -53,3 +53,16 @@ for f in "${tools[@]}"; do || fail "$f --help doesn't follow its header when it grows by a line" done echo "ok - ${#tools[@]} tools print their header comment as --help, however long it grows, and no code" + +# The same --help, output and exit code, run by a relative path from the +# repository's parent and from tools/: a tool that cds to the repository +# first must not look for its header at "$0" from the directory it left +# (#559). +help_from() { (cd -- "$1" && "$2" --help 2>&1; echo "exit $?"); } +repo=$PWD +for f in "${tools[@]}"; do + out=$(help_from "$repo" "$f") + [[ $(help_from "$repo/.." "${repo##*/}/$f") == "$out" ]] || fail "$f --help differs run from the repository's parent" + [[ $(help_from "$repo/tools" "./${f#tools/}") == "$out" ]] || fail "$f --help differs run from tools/" +done +echo "ok - ${#tools[@]} tools print the same --help by a relative path from another directory" diff --git a/tools/lib/usage.sh b/tools/lib/usage.sh index 63299f273..b5cbc6d9f 100644 --- a/tools/lib/usage.sh +++ b/tools/lib/usage.sh @@ -10,17 +10,25 @@ # # source tools/lib/usage.sh # usage_from_header [--raw] FILE [PARAGRAPHS] -# prints FILE's header with each line's "#" and one space after it taken +# prints the header of the tool named FILE, in the tools directory +# above this file, with each line's "#" and one space after it taken # off (--raw keeps them). With PARAGRAPHS, only that many paragraphs of # it (a bare "#" line ends one), for a tool whose help is the first part # of a longer header. +# +# Only FILE's name is used. A tool that cds to the repository first and +# passes "$0" passes a path relative to the directory it left +# (`cd ..; omarchy-mobile/tools/build-packages --help`, #559), while this +# file's own path is still good: the tool has just sourced it from where it +# is now. usage_from_header() { local raw=0 [[ ${1:-} == --raw ]] && { raw=1; shift; } + local name=${1:?usage_from_header FILE [PARAGRAPHS]} awk -v raw="$raw" -v paras="${2:-0}" ' NR <= 2 { next } !/^#/ { exit } /^#[ \t]*$/ && ++blank == paras { exit } - { if (!raw) sub(/^# ?/, ""); print }' "${1:?usage_from_header FILE [PARAGRAPHS]}" + { if (!raw) sub(/^# ?/, ""); print }' "$(dirname -- "${BASH_SOURCE[0]}")/../${name##*/}" }