Skip to content

Commit

Permalink
Added explicit reset of quarantied variables in pwsh
Browse files Browse the repository at this point in the history
  • Loading branch information
ACSimon33 authored and xdelaruelle committed Sep 11, 2024
1 parent 8d48345 commit d590de8
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 24 deletions.
60 changes: 54 additions & 6 deletions init/pwsh.ps1.in
Original file line number Diff line number Diff line change
Expand Up @@ -17,26 +17,40 @@ if ($IsWindows) {
$cmd_is_query = "$allargs" -match '(is-loaded|is-avail|is-used|is-saved)'

# setup quarantine if defined
$_mlre = ''
$_mlre_set = ''
$_mlre_reset = ''
if ($env:MODULES_RUN_QUARANTINE) {
foreach ($_mlv in $env:MODULES_RUN_QUARANTINE -split ' ') {
if ($_mlv -match '^[A-Za-z_][A-Za-z0-9_]*$') {
$_mlv_value = [System.Environment]::GetEnvironmentVariable($_mlv)
if ($_mlv_value) {
$_mlre += "`$env:__MODULES_QUAR_${_mlv}='$_mlv_value';"
$_mlre_set += "`$env:__MODULES_QUAR_${_mlv}='$_mlv_value';"
$_mlre_reset = "Remove-Item -Path env:__MODULES_QUAR_${_mlv};" + $_mlre_reset
}
$_mlrv = "MODULES_RUNENV_$_mlv"
$_mlrv_value = [System.Environment]::GetEnvironmentVariable($_mlrv)
$_mlre += "`$env:$_mlv='$_mlrv_value';"
$_mlre_set += "`$env:$_mlv='$_mlrv_value';"
$_mlre_reset = "`$env:$_mlv=`"`$env:__MODULES_QUAR_${_mlv}`";" + $_mlre_reset
}
}
if ($_mlre -ne '') {
$_mlre += "`$env:__MODULES_QUARANTINE_SET=1"
Invoke-Expression "$_mlre"
if ($_mlre_set -ne '') {
$_mlre_set += "`$env:__MODULES_QUARANTINE_SET=1"
$_mlre_reset += "Remove-Item -Path env:__MODULES_QUARANTINE_SET"
}
}

if ($_mlre_set -ne '') {
Invoke-Expression "$_mlre_set"
Remove-Variable "_mlre_set"
}

$output = & tclsh "$env:MODULES_CMD" pwsh $allargs 2>&1

if ($_mlre_reset -ne '') {
Invoke-Expression "$_mlre_reset"
Remove-Variable "_mlre_reset"
}

$outmsg = ($output | ? {$_.gettype().Name -ne "ErrorRecord"}) -join "`n"
$errmsg = ($output | ? {$_.gettype().Name -eq "ErrorRecord"}) -join "`n"
$errmsg = $errmsg.replace(
Expand Down Expand Up @@ -125,7 +139,41 @@ if ($IsWindows) {
# enable a default modulepath directory
& envmodule use "$env:MODULESHOME/modulefiles"
} else {
$_mlre_set = ''
$_mlre_reset = ''

# setup quarantine if defined
if ($env:MODULES_RUN_QUARANTINE) {
foreach ($_mlv in $env:MODULES_RUN_QUARANTINE -split ' ') {
if ($_mlv -match '^[A-Za-z_][A-Za-z0-9_]*$') {
$_mlv_value = [System.Environment]::GetEnvironmentVariable($_mlv)
if ($_mlv_value) {
$_mlre_set += "`$env:__MODULES_QUAR_${_mlv}='$_mlv_value';"
$_mlre_reset = "Remove-Item -Path env:__MODULES_QUAR_${_mlv};" + $_mlre_reset
}
$_mlrv = "MODULES_RUNENV_$_mlv"
$_mlrv_value = [System.Environment]::GetEnvironmentVariable($_mlrv)
$_mlre_set += "`$env:$_mlv='$_mlrv_value';"
$_mlre_reset = "`$env:$_mlv=`"`$env:__MODULES_QUAR_${_mlv}`";" + $_mlre_reset
}
}
if ($_mlre_set -ne '') {
$_mlre_set += "`$env:__MODULES_QUARANTINE_SET=1"
$_mlre_reset += "Remove-Item -Path env:__MODULES_QUARANTINE_SET"
}
}

if ($_mlre_set -ne '') {
Invoke-Expression "$_mlre_set"
Remove-Variable "_mlre_set"
}

# define module command and surrounding initial environment (default value
# for MODULESHOME, MODULEPATH, LOADEDMODULES and parse of init config files)
(& "@TCLSH@" "@libexecdir@/modulecmd.tcl" pwsh autoinit) -join "`n" | Invoke-Expression

if ($_mlre_reset -ne '') {
Invoke-Expression "$_mlre_reset"
Remove-Variable "_mlre_reset"
}
}
35 changes: 26 additions & 9 deletions tcl/envmngt.tcl.in
Original file line number Diff line number Diff line change
Expand Up @@ -1295,28 +1295,45 @@ ml <- function(...){
$cmd_is_query = "$allargs" -match '(is-loaded|is-avail|is-used|is-saved)'}
if {[getConf quarantine_support]} {
append fdef {
$_mlre_set = ''
$_mlre_reset = ''
if ($env:MODULES_RUN_QUARANTINE) {
$_mlre = ''
foreach ($_mlv in $env:MODULES_RUN_QUARANTINE -split ' ') {
if ($_mlv -match '^[A-Za-z_][A-Za-z0-9_]*$') {
$_mlv_val = [System.Environment]::GetEnvironmentVariable($_mlv)
if ($_mlv_val) {
$_mlre += "`$env:__MODULES_QUAR_${_mlv}='$_mlv_val';"
$_mlv_value = [System.Environment]::GetEnvironmentVariable($_mlv)
if ($_mlv_value) {
$_mlre_set += "`$env:__MODULES_QUAR_${_mlv}='$_mlv_value';"
$_mlre_reset = "Remove-Item -Path env:__MODULES_QUAR_${_mlv};"\
+ $_mlre_reset
}
$_mlrv = "MODULES_RUNENV_$_mlv"
$_mlrv_val = [System.Environment]::GetEnvironmentVariable($_mlrv)
$_mlre += "`$env:$_mlv='$_mlrv_val';"
$_mlrv_value =\
[System.Environment]::GetEnvironmentVariable($_mlrv)
$_mlre_set += "`$env:$_mlv='$_mlrv_value';"
$_mlre_reset = "`$env:$_mlv=`"`$env:__MODULES_QUAR_${_mlv}`";"\
+ $_mlre_reset
}
}
if ($_mlre -ne '') {
$_mlre += "`$env:__MODULES_QUARANTINE_SET=1"
Invoke-Expression "$_mlre"
if ($_mlre_set -ne '') {
$_mlre_set += "`$env:__MODULES_QUARANTINE_SET=1"
$_mlre_reset += "Remove-Item -Path env:__MODULES_QUARANTINE_SET"
}
}
if ($_mlre_set -ne '') {
Invoke-Expression "$_mlre_set"
Remove-Variable "_mlre_set"
}}
}
append fdef {
$output = }
append fdef "& \"$tclshbin\" \"$::argv0\" pwsh \$allargs 2>&1"
if {[getConf quarantine_support]} {
append fdef {
if ($_mlre_reset -ne '') {
Invoke-Expression "$_mlre_reset"
Remove-Variable "_mlre_reset"
}}
}
append fdef {
$outmsg = ($output | ? {$_.gettype().Name -ne "ErrorRecord"}) -join "`n"
$errmsg = ($output | ? {$_.gettype().Name -eq "ErrorRecord"}) -join "`n"
Expand Down
29 changes: 20 additions & 9 deletions testsuite/modules.70-maint/120-autoinit.exp
Original file line number Diff line number Diff line change
Expand Up @@ -479,25 +479,36 @@ set func_pwsh "function global:_envmodule_common {
\\s*param\\\(\\\[Parameter\\\(ValueFromRemainingArguments\\\)\\\] \\\[string\\\[\\\]\\\] \\\$allargs\\\)
\\s*\\\$global:_mlstatus = \\\$true
\\s*\\\$cmd_is_query = \"\\\$allargs\" -match '\\\(is-loaded\\\|is-avail\\\|is-used\\\|is-saved\\\)'
\\s*\\\$_mlre_set = ''
\\s*\\\$_mlre_reset = ''
\\s*if \\\(\\\$env:MODULES_RUN_QUARANTINE\\\) {
\\s*\\\$_mlre = ''
\\s*foreach \\\(\\\$_mlv in \\\$env:MODULES_RUN_QUARANTINE -split ' '\\\) {
\\s*if \\\(\\\$_mlv -match '\\\^\\\[A-Za-z_\\\]\\\[A-Za-z0-9_\\\]\\\*\\\$'\\\) {
\\s*\\\$_mlv_val = \\\[System.Environment\\\]::GetEnvironmentVariable\\\(\\\$_mlv\\\)
\\s*if \\\(\\\$_mlv_val\\\) {
\\s*\\\$_mlre \\\+= \"`\\\$env:__MODULES_QUAR_\\\${_mlv}='\\\$_mlv_val';\"
\\s*\\\$_mlv_value = \\\[System.Environment\\\]::GetEnvironmentVariable\\\(\\\$_mlv\\\)
\\s*if \\\(\\\$_mlv_value\\\) {
\\s*\\\$_mlre_set \\\+= \"`\\\$env:__MODULES_QUAR_\\\${_mlv}='\\\$_mlv_value';\"
\\s*\\\$_mlre_reset = \"Remove-Item -Path env:__MODULES_QUAR_\\\${_mlv};\" \\\+ \\\$_mlre_reset
\\s*}
\\s*\\\$_mlrv = \"MODULES_RUNENV_\\\$_mlv\"
\\s*\\\$_mlrv_val = \\\[System.Environment\\\]::GetEnvironmentVariable\\\(\\\$_mlrv\\\)
\\s*\\\$_mlre \\\+= \"`\\\$env:\\\$_mlv='\\\$_mlrv_val';\"
\\s*\\\$_mlrv_value = \\\[System.Environment\\\]::GetEnvironmentVariable\\\(\\\$_mlrv\\\)
\\s*\\\$_mlre_set \\\+= \"`\\\$env:\\\$_mlv='\\\$_mlrv_value';\"
\\s*\\\$_mlre_reset = \"`\\\$env:\\\$_mlv=`\\\"`\\\$env:__MODULES_QUAR_\\\${_mlv}`\\\";\" \\\+ \\\$_mlre_reset
\\s*}
\\s*}
\\s*if \\\(\\\$_mlre -ne ''\\\) {
\\s*\\\$_mlre \\\+= \"`\\\$env:__MODULES_QUARANTINE_SET=1\"
\\s*Invoke-Expression \"\\\$_mlre\"
\\s*if \\\(\\\$_mlre_set -ne ''\\\) {
\\s*\\\$_mlre_set \\\+= \"`\\\$env:__MODULES_QUARANTINE_SET=1\"
\\s*\\\$_mlre_reset \\\+= \"Remove-Item -Path env:__MODULES_QUARANTINE_SET\"
\\s*}
\\s*}
\\s*if \\\(\\\$_mlre_set -ne ''\\\) {
\\s*Invoke-Expression \"\\\$_mlre_set\"
\\s*Remove-Variable \"_mlre_set\"
\\s*}
\\s*\\\$output = & \"$tclshlocre\" \"$modulecmd_pathre\" pwsh \\\$allargs 2>&1
\\s*if \\\(\\\$_mlre_reset -ne ''\\\) {
\\s*Invoke-Expression \"\\\$_mlre_reset\"
\\s*Remove-Variable \"_mlre_reset\"
\\s*}
\\s*\\\$outmsg = \\\(\\\$output \\\| \\\? {\\\$_.gettype\\\(\\\).Name -ne \"ErrorRecord\"}\\\) -join \"`n\"
\\s*\\\$errmsg = \\\(\\\$output \\\| \\\? {\\\$_.gettype\\\(\\\).Name -eq \"ErrorRecord\"}\\\) -join \"`n\"
\\s*\\\$errmsg = \\\$errmsg.replace\\\(
Expand Down

0 comments on commit d590de8

Please sign in to comment.