From ef95c7fb5558cf479e5b9d9b7b80ce0bbfcfe274 Mon Sep 17 00:00:00 2001 From: Xavier Delaruelle Date: Thu, 10 Sep 2020 07:46:16 +0200 Subject: [PATCH] unset env var when setting it to empty val on Win When an empty string is set to an environment variable on Windows platform, this variable is unset yet still seen defined by 'info exists' command on Tcl, which leads to errors when trying to access variable content. So when trying to set an empty string to an environment variable on Windows platform, call for variable unset instead to reflect underlying OS behavior. --- modulecmd.tcl.in | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/modulecmd.tcl.in b/modulecmd.tcl.in index be6a22b3..b76db909 100644 --- a/modulecmd.tcl.in +++ b/modulecmd.tcl.in @@ -1283,16 +1283,24 @@ proc get-env {var {valifunset {}}} { proc set-env {var val} { set mode [currentMode] reportDebug "$var=$val" - interp-sync-env set $var $val - # variable is not cleared anymore if set again - if {[info exists ::g_clearedEnvVars($var)]} { - unset ::g_clearedEnvVars($var) - } + # an empty string value means unsetting variable on Windows platform, so + # call unset-env to ensure variable will not be seen defined yet raising + # an error when trying to access it + if {[getState is_win] && $val eq {}} { + unset-env $var + } else { + interp-sync-env set $var $val - # propagate variable setup to shell environment on load and unload mode - if {$mode eq {load} || $mode eq {unload}} { - set ::g_stateEnvVars($var) new + # variable is not cleared anymore if set again + if {[info exists ::g_clearedEnvVars($var)]} { + unset ::g_clearedEnvVars($var) + } + + # propagate variable setup to shell environment on load and unload mode + if {$mode eq {load} || $mode eq {unload}} { + set ::g_stateEnvVars($var) new + } } }