From c9f1f70451d7dee76deacc55632d1f30356050bf Mon Sep 17 00:00:00 2001 From: Christophe Dervieux Date: Mon, 9 Nov 2026 17:15:19 +0100 Subject: [PATCH] Install R before syncing in configure-test-env In rig's admin mode, rig proj sync installs a missing R by re-running the whole command elevated, so the project library under tests/.rvenv ends up owned by root (r-lib/rig#503, fixed in rig's development version but not in 0.11.1). Setting up the test environment on a machine without the locked R then leaves a library the later dev rmarkdown and knitr installs cannot write to, and they fall back to the locked versions. Install R with rig add first, which does nothing when that version is already installed, then run rig proj sync --no-install-r as the calling user. This is the sequence the CI workflow already uses. Both configure-test-env.sh and configure-test-env.ps1 get it, since rig elevates the same way on Windows. Verified the already-installed path on Windows with both scripts. The missing-R path was not exercised. --- tests/configure-test-env.ps1 | 15 ++++++++++++--- tests/configure-test-env.sh | 9 +++++++-- 2 files changed, 19 insertions(+), 5 deletions(-) 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