From 34573817b87fdae0b533ba070332882e9b6bd863 Mon Sep 17 00:00:00 2001 From: Bartosz Blizniak Date: Mon, 20 Jul 2026 17:03:52 +0100 Subject: [PATCH] fix: select arm64 binary under Rosetta --- CHANGELOG.md | 4 ++-- README.md | 2 +- install.sh | 10 +++++++++- tests/detect_target.bats | 41 ++++++++++++++++++++++++++++++++++++++++ 4 files changed, 53 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3514e45..94e9b11 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,8 +13,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Cross-platform `install.sh` and `install.ps1` installers for the standalone Cloudsmith CLI. -- Automatic platform detection and support for selecting a specific CLI - version or target. +- Automatic platform detection, including native Arm64 selection under + Rosetta 2, and support for selecting a specific CLI version or target. - SHA-256 verification, archive safety checks, atomic installation, and concurrent-install locking. - Stable machine-readable output for CI/CD consumers. diff --git a/README.md b/README.md index 7b00ce7..faa5643 100644 --- a/README.md +++ b/README.md @@ -131,7 +131,7 @@ executable=/home/runner/.local/share/cloudsmith-cli/1.19.0/linux-x86_64-gnu/clou | Linux | x86-64 | `linux-x86_64-musl` | Alpine and other musl distributions | | Linux | Arm64 | `linux-aarch64-musl` | Alpine and other musl distributions | | macOS | x86-64 | `macos-x86_64` | Intel | -| macOS | Arm64 | `macos-arm64` | Apple silicon | +| macOS | Arm64 | `macos-arm64` | Apple silicon, including shells running under Rosetta 2 | | Windows | x86-64 | `windows-x86_64` | Also used on Windows Arm64 through x86-64 emulation | ## Security diff --git a/install.sh b/install.sh index f0d6a4f..ca52526 100755 --- a/install.sh +++ b/install.sh @@ -157,7 +157,15 @@ detect_target() { Darwin) case "$machine" in arm64|aarch64) printf '%s\n' macos-arm64 ;; - x86_64|amd64) printf '%s\n' macos-x86_64 ;; + x86_64|amd64) + # A translated shell reports x86_64 even when the host is Apple + # silicon. Prefer the native CLI binary when Rosetta 2 is detected. + if [ "$(sysctl -n sysctl.proc_translated 2>/dev/null || true)" = 1 ]; then + printf '%s\n' macos-arm64 + else + printf '%s\n' macos-x86_64 + fi + ;; *) die "unsupported macOS architecture: $machine" ;; esac ;; diff --git a/tests/detect_target.bats b/tests/detect_target.bats index 865d0e1..84eba4c 100644 --- a/tests/detect_target.bats +++ b/tests/detect_target.bats @@ -37,6 +37,22 @@ EOF chmod +x "$STUB_BIN/uname" } +# write_sysctl_stub : fakes Apple's Rosetta probe. +write_sysctl_stub() { + local mode="$1" + cat > "$STUB_BIN/sysctl" <: "gnu" simulates glibc; "fail" forces the ldd fallback. write_getconf_stub() { local mode="$1" @@ -72,6 +88,31 @@ write_ldd_stub() { @test "darwin/x86_64 is detected as macos-x86_64" { write_uname_stub Darwin x86_64 + write_sysctl_stub native + + run --separate-stderr "$INSTALL_SH" \ + --install-root "$INSTALL_ROOT" \ + --manifest-url "$UNREACHABLE_MANIFEST_URL" + + [ "$status" -ne 0 ] + [[ "$stderr" == *"detected target macos-x86_64"* ]] +} + +@test "darwin/x86_64 under Rosetta is detected as macos-arm64" { + write_uname_stub Darwin x86_64 + write_sysctl_stub translated + + run --separate-stderr "$INSTALL_SH" \ + --install-root "$INSTALL_ROOT" \ + --manifest-url "$UNREACHABLE_MANIFEST_URL" + + [ "$status" -ne 0 ] + [[ "$stderr" == *"detected target macos-arm64"* ]] +} + +@test "darwin/x86_64 stays macos-x86_64 when the Rosetta probe is unavailable" { + write_uname_stub Darwin x86_64 + write_sysctl_stub unavailable run --separate-stderr "$INSTALL_SH" \ --install-root "$INSTALL_ROOT" \