fix(hooks): scope install traps to subshells to prevent RETURN trap leak
trap '...' RETURN inside a function is NOT local to that function in bash
— it persists in the calling scope and fires on every subsequent function
return. After install_shellcheck set the trap, it fired again when
ensure_tool returned with $tmp_dir unbound, causing nounset abort.
Fix: change install functions from {} to () (subshell bodies) and use
trap EXIT instead of RETURN. The trap is now scoped to the subshell and
cannot leak to callers.
Also fixes double _os() call in install_jq and removes redundant local
declarations (subshells don't need them).
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01TP4EGbBg3XMcyF28Lx78XJ
This commit is contained in:
@@ -39,49 +39,44 @@ _arch() {
|
|||||||
esac
|
esac
|
||||||
}
|
}
|
||||||
|
|
||||||
install_shellcheck() {
|
install_shellcheck() (
|
||||||
local os arch tarball url tmp_dir
|
|
||||||
os="$(_os)"
|
os="$(_os)"
|
||||||
arch="$(_arch)"
|
arch="$(_arch)"
|
||||||
tarball="shellcheck-v${SHELLCHECK_VERSION}.${os}.${arch}.tar.xz"
|
tarball="shellcheck-v${SHELLCHECK_VERSION}.${os}.${arch}.tar.xz"
|
||||||
url="https://github.com/koalaman/shellcheck/releases/download/v${SHELLCHECK_VERSION}/${tarball}"
|
url="https://github.com/koalaman/shellcheck/releases/download/v${SHELLCHECK_VERSION}/${tarball}"
|
||||||
tmp_dir="$(mktemp -d)"
|
tmp_dir="$(mktemp -d)"
|
||||||
trap 'rm -rf "$tmp_dir"' RETURN
|
trap 'rm -rf "$tmp_dir"' EXIT
|
||||||
echo "Installing shellcheck v${SHELLCHECK_VERSION}..."
|
echo "Installing shellcheck v${SHELLCHECK_VERSION}..."
|
||||||
curl -fsSL "$url" -o "$tmp_dir/$tarball"
|
curl -fsSL "$url" -o "$tmp_dir/$tarball"
|
||||||
tar -xJf "$tmp_dir/$tarball" -C "$tmp_dir" --strip-components=1
|
tar -xJf "$tmp_dir/$tarball" -C "$tmp_dir" --strip-components=1
|
||||||
install -m 755 "$tmp_dir/shellcheck" "$TOOL_INSTALL_DIR/shellcheck"
|
install -m 755 "$tmp_dir/shellcheck" "$TOOL_INSTALL_DIR/shellcheck"
|
||||||
echo "Installed: $TOOL_INSTALL_DIR/shellcheck"
|
echo "Installed: $TOOL_INSTALL_DIR/shellcheck"
|
||||||
}
|
)
|
||||||
|
|
||||||
install_jq() {
|
install_jq() (
|
||||||
local os arch binary url tmp_dir
|
|
||||||
os="$(_os)"
|
os="$(_os)"
|
||||||
|
[[ "$os" == "darwin" ]] && os="macos"
|
||||||
arch="$(_arch | sed 's/x86_64/amd64/; s/aarch64/arm64/')"
|
arch="$(_arch | sed 's/x86_64/amd64/; s/aarch64/arm64/')"
|
||||||
[[ "$(_os)" == "darwin" ]] && os="macos"
|
url="https://github.com/jqlang/jq/releases/download/jq-${JQ_VERSION}/jq-${os}-${arch}"
|
||||||
binary="jq-${os}-${arch}"
|
|
||||||
url="https://github.com/jqlang/jq/releases/download/jq-${JQ_VERSION}/${binary}"
|
|
||||||
tmp_dir="$(mktemp -d)"
|
tmp_dir="$(mktemp -d)"
|
||||||
trap 'rm -rf "$tmp_dir"' RETURN
|
trap 'rm -rf "$tmp_dir"' EXIT
|
||||||
echo "Installing jq v${JQ_VERSION}..."
|
echo "Installing jq v${JQ_VERSION}..."
|
||||||
curl -fsSL "$url" -o "$tmp_dir/jq"
|
curl -fsSL "$url" -o "$tmp_dir/jq"
|
||||||
install -m 755 "$tmp_dir/jq" "$TOOL_INSTALL_DIR/jq"
|
install -m 755 "$tmp_dir/jq" "$TOOL_INSTALL_DIR/jq"
|
||||||
echo "Installed: $TOOL_INSTALL_DIR/jq"
|
echo "Installed: $TOOL_INSTALL_DIR/jq"
|
||||||
}
|
)
|
||||||
|
|
||||||
install_yq() {
|
install_yq() (
|
||||||
local os arch binary url tmp_dir
|
|
||||||
os="$(_os)"
|
os="$(_os)"
|
||||||
arch="$(_arch | sed 's/x86_64/amd64/; s/aarch64/arm64/')"
|
arch="$(_arch | sed 's/x86_64/amd64/; s/aarch64/arm64/')"
|
||||||
binary="yq_${os}_${arch}"
|
url="https://github.com/mikefarah/yq/releases/download/v${YQ_VERSION}/yq_${os}_${arch}"
|
||||||
url="https://github.com/mikefarah/yq/releases/download/v${YQ_VERSION}/${binary}"
|
|
||||||
tmp_dir="$(mktemp -d)"
|
tmp_dir="$(mktemp -d)"
|
||||||
trap 'rm -rf "$tmp_dir"' RETURN
|
trap 'rm -rf "$tmp_dir"' EXIT
|
||||||
echo "Installing yq v${YQ_VERSION}..."
|
echo "Installing yq v${YQ_VERSION}..."
|
||||||
curl -fsSL "$url" -o "$tmp_dir/yq"
|
curl -fsSL "$url" -o "$tmp_dir/yq"
|
||||||
install -m 755 "$tmp_dir/yq" "$TOOL_INSTALL_DIR/yq"
|
install -m 755 "$tmp_dir/yq" "$TOOL_INSTALL_DIR/yq"
|
||||||
echo "Installed: $TOOL_INSTALL_DIR/yq"
|
echo "Installed: $TOOL_INSTALL_DIR/yq"
|
||||||
}
|
)
|
||||||
|
|
||||||
ensure_tool() {
|
ensure_tool() {
|
||||||
local tool="$1"
|
local tool="$1"
|
||||||
|
|||||||
Reference in New Issue
Block a user