From 615d3dd8811decb9dc6e01a974621f40e6c34612 Mon Sep 17 00:00:00 2001 From: Defame1297 Date: Wed, 2 Dec 2026 09:47:30 +0000 Subject: [PATCH] fix(scripts): parse the apm version from stdout and its banner only require_apm_version merged stderr into the --version output and took the first X.Y.Z. When `uv run --frozen` syncs first, uv prints `Using CPython 3.14.6` on stderr, so the check parsed 3.14.6 and passed whatever apm reported. It now reads stdout only, matches apm's `version X.Y.Z` banner, and shows the captured stderr only on failure. The fake uv in the seam B test now writes that line to stderr, so the regression stays pinned. Resolve the repo root with CDPATH cleared. Refs: #271 Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_014uGZTNugst7K9Bd2FTR4Mi --- scripts/lib/apm.sh | 23 ++++++++++++++++++----- tests/test-apm-gate-invocation.sh | 5 ++++- 2 files changed, 22 insertions(+), 6 deletions(-) diff --git a/scripts/lib/apm.sh b/scripts/lib/apm.sh index a9baa21..972a0cb 100644 --- a/scripts/lib/apm.sh +++ b/scripts/lib/apm.sh @@ -35,7 +35,7 @@ # shellcheck source=scripts/lib/clean-git-env.sh source "$(dirname "${BASH_SOURCE[0]}")/clean-git-env.sh" -APM_REPO_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd)" +APM_REPO_ROOT="$(CDPATH='' cd -- "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd)" # 0.39.2 is the lowest apm that reads and writes lockfile v4 in a form later # versions accept, and it is above 0.39.0, which keeps Claude command keys such # as disable-model-invocation (ADR-0034, "Minimum version"). @@ -54,24 +54,37 @@ run_apm() { # Fails, with a message naming the fix, when the apm this helper resolves is # missing, unreadable, or below APM_MIN_VERSION. Returns 0 when it is new enough. require_apm_version() { - local output version + local output version stderr_file if [[ -z "${APM:-}" ]] && ! command -v uv >/dev/null 2>&1; then echo "FAIL: uv is not on PATH, and this repo runs apm as 'uv run --frozen apm' (ADR-0034)." >&2 echo " Fix: install uv, then run 'uv sync --frozen' at the repo root." >&2 return 1 fi - if ! output="$(run_apm --version 2>&1)"; then + # stdout only. uv writes its own lines to stderr when `uv run` syncs first + # (`Using CPython 3.14.6`, `Installed 62 packages`), and a merged stream + # would hand the parser uv's numbers instead of apm's. stderr is kept aside + # and shown only when the check fails. + stderr_file="$(mktemp)" + if ! output="$(run_apm --version 2>"$stderr_file")"; then + cat "$stderr_file" >&2 printf '%s\n' "$output" >&2 + rm -f "$stderr_file" echo "FAIL: could not run '${APM_CMD[*]} --version', so the apm version is unknown." >&2 echo " Fix: run 'uv sync --frozen' at the repo root, then retry." >&2 return 1 fi - version="$(grep -oE '[0-9]+\.[0-9]+\.[0-9]+' <<<"$output" | head -n 1 || true)" + # Anchored to apm's banner, `... CLI version X.Y.Z`, so no other number on + # the line can be taken for the version. + version="$(grep -oE 'version [0-9]+\.[0-9]+\.[0-9]+' <<<"$output" | head -n 1 || true)" + version="${version#version }" if [[ ! "$version" =~ ^([0-9]+)\.([0-9]+)\.([0-9]+)$ ]]; then - echo "FAIL: '${APM_CMD[*]} --version' printed no version, so the apm version is unknown." >&2 + cat "$stderr_file" >&2 + rm -f "$stderr_file" + echo "FAIL: '${APM_CMD[*]} --version' printed no 'version X.Y.Z', so the apm version is unknown." >&2 echo " Fix: run 'uv sync --frozen' at the repo root, then retry." >&2 return 1 fi + rm -f "$stderr_file" local -a have=("${BASH_REMATCH[1]}" "${BASH_REMATCH[2]}" "${BASH_REMATCH[3]}") local -a need IFS=. read -r -a need <<<"$APM_MIN_VERSION" diff --git a/tests/test-apm-gate-invocation.sh b/tests/test-apm-gate-invocation.sh index 34ea7d3..df2e765 100755 --- a/tests/test-apm-gate-invocation.sh +++ b/tests/test-apm-gate-invocation.sh @@ -48,6 +48,9 @@ if [[ "\${1:-}" != run || "\${2:-}" != --project || "\${4:-}" != --frozen || "\$ exit 97 fi shift 5 +# What a real \`uv run --frozen\` prints on stderr when it syncs first. The +# version check must parse apm's stdout, never this (review round 1 of #271). +echo "Using CPython 3.14.6" >&2 exec "$BIN/apm" "\$@" EOF @@ -165,7 +168,7 @@ for pair in "apm-audit-ci:$AUDIT" "apm-pack-check-clean:$PACK"; do done reset_logs run_gate "$FIXTURE" "$script" STUB_APM_OUTPUT="apm, version unknown" - [[ $RC -ne 0 ]] && grep -qF "printed no version" <<< "$OUT" \ + [[ $RC -ne 0 ]] && grep -qF "printed no 'version X.Y.Z'" <<< "$OUT" \ && pass "$label: fails when the version cannot be read" \ || fail "$label: an unreadable version must fail clearly (rc=$RC): $OUT" done