Skip to content

Commit 11a9c60

Browse files
committed
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.
1 parent ac310a4 commit 11a9c60

1 file changed

Lines changed: 31 additions & 27 deletions

File tree

‎tests/configure-test-env.ps1‎

Lines changed: 31 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -7,38 +7,42 @@ try { $null = gcm rig -ea stop; $rig=$true } catch {
77
If ($rig) {
88
# rig defaults to admin mode, where installing R raises a UAC prompt. User
99
# mode installs R in the user profile instead; an explicit RIG_MODE wins.
10-
# The variable is removed again at the end so it does not leak into a
11-
# dot-sourcing shell. rig.exe does not need the mode later: rvenv.cfg
12-
# records the absolute path of the R it selected.
10+
# The variable is removed again in `finally`, even after Ctrl+C or an
11+
# error, so it does not leak into a dot-sourcing shell. rig.exe does not
12+
# need the mode later: rvenv.cfg records the absolute path of the R it
13+
# selected.
1314
$rigModeWasSet = [bool]$env:RIG_MODE
1415
If (-not $rigModeWasSet) { $env:RIG_MODE = 'user' }
15-
# In admin mode rig proj sync installs a missing R by re-running itself
16-
# elevated, which leaves the project library owned by the admin account.
17-
# Install R separately (rig add does nothing when it is already installed),
18-
# then sync as this user.
19-
$rLockVersion = (Select-String -Path rproj.lock -Pattern '^r_version = "(.+)"' | Select-Object -First 1).Matches[0].Groups[1].Value
20-
Write-Host -ForegroundColor green " > Installing R $rLockVersion if missing"
21-
rig add $rLockVersion
22-
If ($LASTEXITCODE -eq 0) {
23-
Write-Host -ForegroundColor green " > Syncing R packages from rproj.lock"
24-
rig proj sync --no-install-r
25-
}
26-
If ($LASTEXITCODE -ne 0) {
27-
Write-Host -ForegroundColor red "Installing R or rig proj sync failed - R tests will run without the locked packages"
28-
} Else {
29-
# Installs on top of the lock must target the project library and the
30-
# exact R the sync used; rig's default is the default R's user library.
31-
$rVersion = (Select-String -Path .rvenv/rvenv.cfg -Pattern '^r-version = (.+)$').Matches[0].Groups[1].Value.Trim()
32-
$rLib = (Resolve-Path .rvenv/lib).Path
33-
Write-Host -ForegroundColor green " > Installing dev knitr and rmarkdown"
34-
foreach ($dev in @(@('rmarkdown', 'https://rstudio.r-universe.dev'), @('knitr', 'https://yihui.r-universe.dev'))) {
35-
rig pkg install --r-version $rVersion --library $rLib --with-repos $dev[1] $dev[0]
36-
If ($LASTEXITCODE -ne 0) {
37-
Write-Host -ForegroundColor yellow "Dev $($dev[0]) not installed - tests use the locked version"
16+
try {
17+
# In admin mode rig proj sync installs a missing R by re-running itself
18+
# elevated, which leaves the project library owned by the admin account.
19+
# Install R separately (rig add does nothing when it is already installed),
20+
# then sync as this user.
21+
$rLockVersion = (Select-String -Path rproj.lock -Pattern '^r_version = "(.+)"' | Select-Object -First 1).Matches[0].Groups[1].Value
22+
Write-Host -ForegroundColor green " > Installing R $rLockVersion if missing"
23+
rig add $rLockVersion
24+
If ($LASTEXITCODE -eq 0) {
25+
Write-Host -ForegroundColor green " > Syncing R packages from rproj.lock"
26+
rig proj sync --no-install-r
27+
}
28+
If ($LASTEXITCODE -ne 0) {
29+
Write-Host -ForegroundColor red "Installing R or rig proj sync failed - R tests will run without the locked packages"
30+
} Else {
31+
# Installs on top of the lock must target the project library and the
32+
# exact R the sync used; rig's default is the default R's user library.
33+
$rVersion = (Select-String -Path .rvenv/rvenv.cfg -Pattern '^r-version = (.+)$').Matches[0].Groups[1].Value.Trim()
34+
$rLib = (Resolve-Path .rvenv/lib).Path
35+
Write-Host -ForegroundColor green " > Installing dev knitr and rmarkdown"
36+
foreach ($dev in @(@('rmarkdown', 'https://rstudio.r-universe.dev'), @('knitr', 'https://yihui.r-universe.dev'))) {
37+
rig pkg install --r-version $rVersion --library $rLib --with-repos $dev[1] $dev[0]
38+
If ($LASTEXITCODE -ne 0) {
39+
Write-Host -ForegroundColor yellow "Dev $($dev[0]) not installed - tests use the locked version"
40+
}
3841
}
3942
}
43+
} finally {
44+
If (-not $rigModeWasSet) { Remove-Item Env:RIG_MODE }
4045
}
41-
If (-not $rigModeWasSet) { Remove-Item Env:RIG_MODE }
4246
}
4347

4448
# Check python test environment ---

0 commit comments

Comments
 (0)