Fix sccache on windows (#48943)

Conrad Irwin created

Release Notes:

- N/A *or* Added/Fixed/Improved ...

Change summary

script/setup-sccache     | 22 +++++++++++++++++-----
script/setup-sccache.ps1 | 35 ++++++++++++++++++++++++++++++++---
2 files changed, 49 insertions(+), 8 deletions(-)

Detailed changes

script/setup-sccache 🔗

@@ -3,12 +3,13 @@
 set -euo pipefail
 
 SCCACHE_VERSION="v0.10.0"
-SCCACHE_DIR="./target/sccache"
+# Use absolute path to avoid issues with working directory changes between steps
+SCCACHE_DIR="$(pwd)/target/sccache"
 
 install_sccache() {
     mkdir -p "$SCCACHE_DIR"
 
-    if [[ -x "${SCCACHE_DIR}/sccache" ]]; then
+    if [[ -x "${SCCACHE_DIR}/sccache" ]] && "${SCCACHE_DIR}/sccache" --version &>/dev/null; then
         echo "sccache already cached: $("${SCCACHE_DIR}/sccache" --version)"
     else
         echo "Installing sccache ${SCCACHE_VERSION} from GitHub releases..."
@@ -43,10 +44,17 @@ install_sccache() {
         echo "Installed sccache: $("${SCCACHE_DIR}/sccache" --version)"
     fi
 
+    # Verify the binary works before adding to path
+    if ! "${SCCACHE_DIR}/sccache" --version &>/dev/null; then
+        echo "ERROR: sccache binary at ${SCCACHE_DIR}/sccache is not executable or corrupted"
+        rm -f "${SCCACHE_DIR}/sccache"
+        exit 1
+    fi
+
     if [[ -n "${GITHUB_PATH:-}" ]]; then
-        echo "$(pwd)/${SCCACHE_DIR}" >> "$GITHUB_PATH"
+        echo "${SCCACHE_DIR}" >> "$GITHUB_PATH"
     fi
-    export PATH="$(pwd)/${SCCACHE_DIR}:${PATH}"
+    export PATH="${SCCACHE_DIR}:${PATH}"
 }
 
 configure_sccache() {
@@ -61,6 +69,10 @@ configure_sccache() {
     local key_prefix="${SCCACHE_KEY_PREFIX:-sccache/}"
     local base_dir="${GITHUB_WORKSPACE:-$(pwd)}"
 
+    # Use the absolute path to sccache binary for RUSTC_WRAPPER to avoid
+    # any PATH race conditions between GITHUB_PATH and GITHUB_ENV
+    local sccache_bin="${SCCACHE_DIR}/sccache"
+
     # Set in current process
     export SCCACHE_ENDPOINT="https://${R2_ACCOUNT_ID}.r2.cloudflarestorage.com"
     export SCCACHE_BUCKET="${bucket}"
@@ -69,7 +81,7 @@ configure_sccache() {
     export SCCACHE_BASEDIR="${base_dir}"
     export AWS_ACCESS_KEY_ID="${R2_ACCESS_KEY_ID}"
     export AWS_SECRET_ACCESS_KEY="${R2_SECRET_ACCESS_KEY}"
-    export RUSTC_WRAPPER="sccache"
+    export RUSTC_WRAPPER="${sccache_bin}"
 
     # Also write to GITHUB_ENV for subsequent steps
     if [[ -n "${GITHUB_ENV:-}" ]]; then

script/setup-sccache.ps1 🔗

@@ -43,12 +43,36 @@ function Install-Sccache {
         $absolutePath | Out-File -FilePath $env:GITHUB_PATH -Append -Encoding utf8
     }
     $env:PATH = "$absolutePath;$env:PATH"
+
+    # Verify sccache is available in PATH - fail fast if not
+    $sccacheCmd = Get-Command sccache -ErrorAction SilentlyContinue
+    if (-not $sccacheCmd) {
+        Write-Host "::error::sccache was installed but is not found in PATH"
+        Write-Host "PATH: $env:PATH"
+        Write-Host "Expected location: $absolutePath"
+        if (Test-Path (Join-Path $absolutePath "sccache.exe")) {
+            Write-Host "sccache.exe exists at expected location but is not in PATH"
+            Write-Host "Directory contents:"
+            Get-ChildItem $absolutePath | ForEach-Object { Write-Host "  $_" }
+        } else {
+            Write-Host "sccache.exe NOT found at expected location"
+        }
+        exit 1
+    }
 }
 
 function Configure-Sccache {
     if (-not $env:R2_ACCOUNT_ID) {
-        Write-Host "R2_ACCOUNT_ID not set, skipping sccache configuration"
-        return
+        Write-Host "::error::R2_ACCOUNT_ID not set, cannot configure sccache"
+        exit 1
+    }
+
+    # Verify sccache is available before configuring
+    $sccacheCmd = Get-Command sccache -ErrorAction SilentlyContinue
+    if (-not $sccacheCmd) {
+        Write-Host "::error::sccache not found in PATH, cannot configure RUSTC_WRAPPER"
+        Write-Host "PATH: $env:PATH"
+        exit 1
     }
 
     Write-Host "Configuring sccache with Cloudflare R2..."
@@ -57,6 +81,10 @@ function Configure-Sccache {
     $keyPrefix = if ($env:SCCACHE_KEY_PREFIX) { $env:SCCACHE_KEY_PREFIX } else { "sccache/" }
     $baseDir = if ($env:GITHUB_WORKSPACE) { $env:GITHUB_WORKSPACE } else { (Get-Location).Path }
 
+    # Use the absolute path to sccache binary for RUSTC_WRAPPER to avoid
+    # any PATH race conditions between GITHUB_PATH and GITHUB_ENV
+    $sccacheBin = (Get-Command sccache).Source
+
     # Set in current process
     $env:SCCACHE_ENDPOINT = "https://$($env:R2_ACCOUNT_ID).r2.cloudflarestorage.com"
     $env:SCCACHE_BUCKET = $bucket
@@ -65,7 +93,7 @@ function Configure-Sccache {
     $env:SCCACHE_BASEDIR = $baseDir
     $env:AWS_ACCESS_KEY_ID = $env:R2_ACCESS_KEY_ID
     $env:AWS_SECRET_ACCESS_KEY = $env:R2_SECRET_ACCESS_KEY
-    $env:RUSTC_WRAPPER = "sccache"
+    $env:RUSTC_WRAPPER = $sccacheBin
 
     # Also write to GITHUB_ENV for subsequent steps
     if ($env:GITHUB_ENV) {
@@ -87,6 +115,7 @@ function Configure-Sccache {
 function Show-Config {
     Write-Host "=== sccache configuration ==="
     Write-Host "sccache version: $(sccache --version)"
+    Write-Host "sccache path: $((Get-Command sccache).Source)"
     Write-Host "RUSTC_WRAPPER: $($env:RUSTC_WRAPPER ?? '<not set>')"
     Write-Host "SCCACHE_BUCKET: $($env:SCCACHE_BUCKET ?? '<not set>')"
     Write-Host "SCCACHE_ENDPOINT: $($env:SCCACHE_ENDPOINT ?? '<not set>')"