From d3971e77fdcf80202ded5a548fa88458eaff701a Mon Sep 17 00:00:00 2001 From: jiarung Date: Sun, 15 Mar 2026 05:14:07 +0000 Subject: [PATCH] fix(git-hooks): replace GNU-only sort -V with portable zero-pad sort MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit sort -V is a GNU extension; BSD sort on macOS does not support it. When node is absent from PATH and the nvm fallback runs, set -euo pipefail causes the unsupported flag to abort the hook before lint/format can run, blocking commits on macOS. Replace the sort -V | tail -1 pipeline with a Bash for-loop that zero-pads each semver component to five digits and emits a tab-delimited key+path line. Plain sort + tail -1 + cut then selects the highest semantic version — no GNU-only flags required. Smoke-tested with v18 vs v22 paths; v22 is correctly selected on both GNU and BSD sort. --- scripts/pre-commit/resolve-node.sh | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/scripts/pre-commit/resolve-node.sh b/scripts/pre-commit/resolve-node.sh index a3f484c7029..f51226889fc 100644 --- a/scripts/pre-commit/resolve-node.sh +++ b/scripts/pre-commit/resolve-node.sh @@ -2,17 +2,21 @@ # Resolve the newest nvm-managed Node when it is not already in PATH. # Source this file; do not execute it directly. # -# Uses `sort -V` (version-aware sort) so that semantic version order is -# respected — e.g. v22.x is chosen over v18.x even though "v1" sorts -# before "v2" lexicographically. +# Cross-platform: avoids GNU-only `sort -V` (not supported by BSD sort on +# macOS). Instead, each semver component is zero-padded to five digits so +# that a plain lexicographic sort correctly selects the highest semantic +# version (e.g. v22.x wins over v18.x). if ! command -v node >/dev/null 2>&1; then _nvm_node=$( - ls -d "$HOME/.nvm/versions/node"/*/bin/node 2>/dev/null \ - | sort -V \ - | tail -1 + for _p in "$HOME/.nvm/versions/node"/*/bin/node; do + [[ -x "$_p" ]] || continue + _ver="${_p%/bin/node}"; _ver="${_ver##*/v}" + IFS=. read -r _ma _mi _pa <<< "$_ver" + printf '%05d%05d%05d\t%s\n' "${_ma:-0}" "${_mi:-0}" "${_pa:-0}" "$_p" + done | sort | tail -1 | cut -f2- ) if [[ -x "$_nvm_node" ]]; then export PATH="$(dirname "$_nvm_node"):$PATH" fi - unset _nvm_node + unset _nvm_node _p _ver _ma _mi _pa fi