From 33139dbaea1f0ac419e55eea381294f8eb01625a Mon Sep 17 00:00:00 2001 From: Ibrahim Lawal Date: Wed, 19 Aug 2026 08:51:10 +0100 Subject: [PATCH] Honor a cache-restored build when a versioned extension mismatches (#1108) * fix: honor a cache-restored build when a versioned extension mismatches When a PHP package install bundles its own default build of a versioned extension (e.g. redis ships with its own recent version pre-enabled), add_pecl_extension correctly detects the version mismatch against the pinned version and falls back to pecl_install - but it does this even when a matching build has already been restored from a CI cache into a separate directory, since shared_extension only checks that *some* file exists at ext_dir, not which version it is. This adds an opt-in CACHED_EXTENSIONS_DIR: when set and a mismatch is detected, look for extension.so there, and if found, copy it into ext_dir and re-check before falling back to a full pecl_install. When CACHED_EXTENSIONS_DIR is unset, behavior is unchanged. Related to shivammathur/cache-extensions#21 and #37, where a cached build of a versioned PECL extension is silently ignored and recompiled on every run regardless of caching. * fix: preserve versioned builds in ext_dir instead of a separate cache dir Per @shivammathur's review on the PR: drop the opt-in CACHED_EXTENSIONS_DIR env var. Instead, after installing a versioned extension, keep a second copy of the built .so in ext_dir itself under a name with no .so suffix (e.g. redis-5.3.7), so it can't be mistaken for the active extension and rides along in the same ext_dir that cache-extensions already caches wholesale - no separate cache directory, key, or workflow step required. On a version mismatch, restore that preserved build over the active binary and re-check before falling back to a full pecl_install. --- src/scripts/extensions/add_extensions.sh | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/src/scripts/extensions/add_extensions.sh b/src/scripts/extensions/add_extensions.sh index b4e27b78..bab5ce5e 100644 --- a/src/scripts/extensions/add_extensions.sh +++ b/src/scripts/extensions/add_extensions.sh @@ -214,12 +214,36 @@ add_pecl_extension() { pecl_version=$(get_pecl_version "$extension" "$pecl_version") fi ext_version=$(php -r "echo phpversion('$extension');") + # The PHP package install itself can bundle its own default build of a + # versioned extension (e.g. redis ships with its own recent version), + # which overwrites ext_dir/$extension.so before this function ever runs. + # shared_extension only checks that *a* file exists, not which version, so + # a build matching the pinned version - even one cache-extensions already + # restored into ext_dir - never gets a chance: this always fell straight + # through to pecl_install regardless of caching. Preserve built versions + # in ext_dir itself under a name with no .so suffix, so cache-extensions + # (which caches all of ext_dir) picks them up automatically, and the PHP + # package install's own file can't collide with or overwrite them. On a + # mismatch, restore the matching preserved build before falling back to a + # full pecl_install. + if [ -n "$pecl_version" ] && check_extension "$extension" && [ "${ext_version/-/}" != "$pecl_version" ]; then + preserved="${ext_dir:?}/$extension-$pecl_version" + if [ -e "$preserved" ]; then + disable_extension_helper "$extension" >/dev/null 2>&1 + sudo cp "$preserved" "${ext_dir:?}/$extension.so" + enable_extension "$extension" "$prefix" + ext_version=$(php -r "echo phpversion('$extension');") + fi + fi if check_extension "$extension" && [[ -z "$pecl_version" || (-n "$pecl_version" && "${ext_version/-/}" == "$pecl_version") ]]; then add_log "${tick:?}" "$extension" "Enabled" else [ -n "$pecl_version" ] && pecl_version="-$pecl_version" pecl_install "$extension$pecl_version" || ( [ "${fail_fast:?}" = "false" ] && add_extension "$extension" "$(get_extension_prefix "$extension")" >/dev/null 2>&1) extension_version="$(php -r "echo phpversion('$extension');")" + if check_extension "$extension" && [ -n "$extension_version" ]; then + sudo cp "${ext_dir:?}/$extension.so" "${ext_dir:?}/$extension-$extension_version" 2>/dev/null || true + fi [ -n "$extension_version" ] && extension_version="-$extension_version" add_extension_log "$extension$extension_version" "Installed and enabled" fi