From c1c8a4f96badc223474d4df66f84d8613e8738a0 Mon Sep 17 00:00:00 2001 From: Christophe Dervieux Date: Tue, 10 Nov 2026 17:31:23 +0100 Subject: [PATCH] Restore RIG_MODE in finally in configure-test-env.ps1 configure-test-env.ps1 sets RIG_MODE=user when it is unset and removed it only at the end of the R block. Interrupting `rig add` or `rig proj sync` (Ctrl+C during a long R download) or any terminating error skipped that line, so RIG_MODE=user stayed in the calling shell. run-tests.ps1 dot-sources the script, and environment variables are process-wide in PowerShell anyway. The removal now runs in a finally block, which PowerShell also runs on Ctrl+C. An explicitly set RIG_MODE is still left untouched. Most of the diff is re-indentation of the R block. Checked on Windows by shadowing rig with a function that throws on `rig add`: before, RIG_MODE was left as "user"; after, it is unset, and a preset RIG_MODE=admin is kept. The R section run with the real rig still installs R 4.6.2 in user mode, syncs the lock and installs dev knitr and rmarkdown, and leaves RIG_MODE unset. --- tests/configure-test-env.ps1 | 58 +++++++++++++++++++----------------- 1 file changed, 31 insertions(+), 27 deletions(-) diff --git a/tests/configure-test-env.ps1 b/tests/configure-test-env.ps1 index f329bf797b9..92343c3acd7 100644 --- a/tests/configure-test-env.ps1 +++ b/tests/configure-test-env.ps1 @@ -7,38 +7,42 @@ try { $null = gcm rig -ea stop; $rig=$true } catch { If ($rig) { # rig defaults to admin mode, where installing R raises a UAC prompt. User # mode installs R in the user profile instead; an explicit RIG_MODE wins. - # The variable is removed again at the end so it does not leak into a - # dot-sourcing shell. rig.exe does not need the mode later: rvenv.cfg - # records the absolute path of the R it selected. + # The variable is removed again in `finally`, even after Ctrl+C or an + # error, so it does not leak into a dot-sourcing shell. rig.exe does not + # need the mode later: rvenv.cfg records the absolute path of the R it + # selected. $rigModeWasSet = [bool]$env:RIG_MODE If (-not $rigModeWasSet) { $env:RIG_MODE = 'user' } - # 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 "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. - $rVersion = (Select-String -Path .rvenv/rvenv.cfg -Pattern '^r-version = (.+)$').Matches[0].Groups[1].Value.Trim() - $rLib = (Resolve-Path .rvenv/lib).Path - Write-Host -ForegroundColor green " > Installing dev knitr and rmarkdown" - foreach ($dev in @(@('rmarkdown', 'https://rstudio.r-universe.dev'), @('knitr', 'https://yihui.r-universe.dev'))) { - rig pkg install --r-version $rVersion --library $rLib --with-repos $dev[1] $dev[0] - If ($LASTEXITCODE -ne 0) { - Write-Host -ForegroundColor yellow "Dev $($dev[0]) not installed - tests use the locked version" + try { + # 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 "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. + $rVersion = (Select-String -Path .rvenv/rvenv.cfg -Pattern '^r-version = (.+)$').Matches[0].Groups[1].Value.Trim() + $rLib = (Resolve-Path .rvenv/lib).Path + Write-Host -ForegroundColor green " > Installing dev knitr and rmarkdown" + foreach ($dev in @(@('rmarkdown', 'https://rstudio.r-universe.dev'), @('knitr', 'https://yihui.r-universe.dev'))) { + rig pkg install --r-version $rVersion --library $rLib --with-repos $dev[1] $dev[0] + If ($LASTEXITCODE -ne 0) { + Write-Host -ForegroundColor yellow "Dev $($dev[0]) not installed - tests use the locked version" + } } } + } finally { + If (-not $rigModeWasSet) { Remove-Item Env:RIG_MODE } } - If (-not $rigModeWasSet) { Remove-Item Env:RIG_MODE } } # Check python test environment ---