diff --git a/tests/configure-test-env.ps1 b/tests/configure-test-env.ps1 index eed9280e7df..18a434f14da 100644 --- a/tests/configure-test-env.ps1 +++ b/tests/configure-test-env.ps1 @@ -5,10 +5,19 @@ try { $null = gcm rig -ea stop; $rig=$true } catch { } If ($rig) { - Write-Host -ForegroundColor green " > Syncing R packages from rproj.lock" - rig proj sync + # In admin mode rig proj sync installs a missing R by re-running itself + # elevated, which leaves the project library owned by the admin account. + # Install R separately (rig add does nothing when it is already installed), + # then sync as this user. + $rLockVersion = (Select-String -Path rproj.lock -Pattern '^r_version = "(.+)"' | Select-Object -First 1).Matches[0].Groups[1].Value + Write-Host -ForegroundColor green " > Installing R $rLockVersion if missing" + rig add $rLockVersion + If ($LASTEXITCODE -eq 0) { + Write-Host -ForegroundColor green " > Syncing R packages from rproj.lock" + rig proj sync --no-install-r + } If ($LASTEXITCODE -ne 0) { - Write-Host -ForegroundColor red "rig proj sync failed - R tests will run without the locked packages" + Write-Host -ForegroundColor red "Installing R or rig proj sync failed - R tests will run without the locked packages" } Else { # Installs on top of the lock must target the project library and the # exact R the sync used; rig's default is the default R's user library. diff --git a/tests/configure-test-env.sh b/tests/configure-test-env.sh index a1e492acb4a..eb3040b521a 100755 --- a/tests/configure-test-env.sh +++ b/tests/configure-test-env.sh @@ -7,8 +7,13 @@ if [ -z "$rig_exists" ] then echo "No rig found in PATH - the R test environment is a rig project (rproj.toml). Install rig 0.11.1 or later: https://github.com/r-lib/rig" else + # In admin mode rig proj sync installs a missing R by re-running itself under + # sudo, which leaves the project library owned by root. Install R separately + # (rig add does nothing when it is already installed), then sync as this user. + r_lock_version=$(sed -n 's/^r_version = "\(.*\)"/\1/p' rproj.lock | head -n 1 | tr -d '\r') + echo " > Installing R $r_lock_version if missing" echo " > Syncing R packages from rproj.lock" - if rig proj sync + if rig add "$r_lock_version" && rig proj sync --no-install-r then # Installs on top of the lock must target the project library and the # exact R the sync used; rig's default is the default R's user library. @@ -20,7 +25,7 @@ else rig pkg install --r-version "$r_version" --library "$r_lib" --with-repos https://yihui.r-universe.dev knitr \ || echo "Dev knitr not installed - tests use the locked version" else - echo "rig proj sync failed - R tests will run without the locked packages" + echo "Installing R or rig proj sync failed - R tests will run without the locked packages" fi fi