From f008973e85c343bb549b7c60bf7a3f4f48501c7c Mon Sep 17 00:00:00 2001 From: Xavier Delaruelle Date: Sat, 28 Nov 2026 17:28:26 +0100 Subject: [PATCH 01/28] Merge variant report branches of getVariantList Report modes 1, 5 and 6 on one side and 7 on the other were handled by two branches repeating the same boolean, shortcut and value variant distinction and the same tracking of the kind of variant reported to build output key. Only the shape of the element appended to the result differs between these modes. Handle them in a single branch, which determines variant name set and value once, then track variant kind with a single statement. Assisted-by: Claude:claude-fable-5-2 Signed-off-by: Xavier Delaruelle --- tcl/modfind.tcl.in | 88 ++++++++++++---------------------------------- 1 file changed, 22 insertions(+), 66 deletions(-) diff --git a/tcl/modfind.tcl.in b/tcl/modfind.tcl.in index 13c9ce2ce..97fb0f3dc 100644 --- a/tcl/modfind.tcl.in +++ b/tcl/modfind.tcl.in @@ -1022,80 +1022,36 @@ proc getVariantList {mod {report 0} {excl_dflval 0} {from 0}} { 3 {lappend ret $vrname [lindex $vrarr($vrname) 0] [lindex\ $vrarr($vrname) 1]} 2 {lappend ret [list $vrname 0 $vrisbool($vrname) $vrarr($vrname)]} - 1 - 5 - 6 { + 1 - 5 - 6 - 7 { if {$vrisbool($vrname)} { - if {$vrarr($vrname)} { - lappend ret +$vrname - # track if +var variant has been reported to build key - # unless if key should not be updated (report mode '6') - if {$report != 6 && ![info exists ::g_used_va(on)]} { - set ::g_used_va(on) 1 - } - } else { - lappend ret -$vrname - # track if -var variant has been reported to build key - # unless if key should not be updated (report mode '6') - if {$report != 6 && ![info exists ::g_used_va(off)]} { - set ::g_used_va(off) 1 - } - } + set vrset {} + lassign [expr {$vrarr($vrname) ? {+ on} : {- off}}] sign key + set vrval [list $sign$vrname] + set keyval 1 # use defined shortcut to report if any set for this variant # ignore shortcut if report=5 (when saving collection) } elseif {$report != 5 && [info exists\ ::g_variantShortcut($vrname)]} { - set sc $::g_variantShortcut($vrname) - lappend ret $sc$vrarr($vrname) - # track if variant shortcut has been reported to build key - # unless if key should not be updated (report mode '6') - if {$report != 6 && ![info exists ::g_used_va($sc)]} { - set ::g_used_va($sc) $vrname - } + set vrset $::g_variantShortcut($vrname) + set vrval $vrarr($vrname) + set key $vrset + set keyval $vrname } else { - lappend ret $vrname=$vrarr($vrname) - # track if var=val variant has been reported to build key - # unless if key should not be updated (report mode '6') - if {$report != 6 && ![info exists ::g_used_va(val)]} { - set ::g_used_va(val) 1 - } + set vrset $vrname= + set vrval $vrarr($vrname) + set key val + set keyval 1 } - } - 7 { - if {$vrisbool($vrname)} { - if {$vrarr($vrname)} { - lappend ret [list $vrname {} [list +$vrname]\ - $vrdflidxarr($vrname) $vrloidxarr($vrname) $loadedsgrkey] - # track if +var variant has been reported to build key - if {![info exists ::g_used_va(on)]} { - set ::g_used_va(on) 1 - } - } else { - lappend ret [list $vrname {} [list -$vrname]\ - $vrdflidxarr($vrname) $vrloidxarr($vrname) $loadedsgrkey] - # track if -var variant has been reported to build key - if {![info exists ::g_used_va(off)]} { - set ::g_used_va(off) 1 - } - } - # use defined shortcut to report if any set for this variant - # ignore shortcut if report=5 (when saving collection) - } elseif {$report != 5 && [info exists\ - ::g_variantShortcut($vrname)]} { - set sc $::g_variantShortcut($vrname) - lappend ret [list $vrname $sc $vrarr($vrname)\ - $vrdflidxarr($vrname) $vrloidxarr($vrname) $loadedsgrkey] - # track if variant shortcut has been reported to build key - # unless if key should not be updated (report mode '6') - if {$report != 6 && ![info exists ::g_used_va($sc)]} { - set ::g_used_va($sc) $vrname - } + if {$report == 7} { + lappend ret [list $vrname $vrset $vrval $vrdflidxarr($vrname)\ + $vrloidxarr($vrname) $loadedsgrkey] } else { - lappend ret [list $vrname $vrname= $vrarr($vrname)\ - $vrdflidxarr($vrname) $vrloidxarr($vrname) $loadedsgrkey] - # track if var=val variant has been reported to build key - # unless if key should not be updated (report mode '6') - if {$report != 6 && ![info exists ::g_used_va(val)]} { - set ::g_used_va(val) 1 - } + lappend ret $vrset$vrval + } + # track kind of variant reported to build key unless if key + # should not be updated (report mode '6') + if {$report != 6 && ![info exists ::g_used_va($key)]} { + set ::g_used_va($key) $keyval } } 0 {lappend ret $vrname $vrarr($vrname)} From e7f2e09d519bffe3a986f80a7be3916e81ca42e5 Mon Sep 17 00:00:00 2001 From: Xavier Delaruelle Date: Sat, 28 Nov 2026 17:28:26 +0100 Subject: [PATCH 02/28] Merge .modulerc and .version handling when walking modulepath Both kinds of rc file are evaluated and recorded the same way in findModulesFromDirsAndFiles. The .version file is just additionally skipped when found at a different depth level than search targets. Assisted-by: Claude:claude-fable-5-2 Signed-off-by: Xavier Delaruelle --- tcl/modfind.tcl.in | 20 +++++++------------- 1 file changed, 7 insertions(+), 13 deletions(-) diff --git a/tcl/modfind.tcl.in b/tcl/modfind.tcl.in index 97fb0f3dc..065285b45 100644 --- a/tcl/modfind.tcl.in +++ b/tcl/modfind.tcl.in @@ -3261,20 +3261,14 @@ proc findModulesFromDirsAndFiles {dir full_list depthlvl fetch_mtime\ } } else { switch -glob -- $tail { - .modulerc { - # rc file has normally been evaluated when its directory - # content was fetched: evaluate it here otherwise, when it - # is walked directly (limited access rc file from cache) - if {![info exists g_rcfilesSourced($element)]} { - execute-modulerc $element $modulename $modulename - set g_rcfilesSourced($element) 1 - } - set mod_list($modulename) [list modulerc] - } - .version { + .modulerc - .version { # skip .version file from different depth level than search - # targets if no in depth mode is enabled - if {$depthlvl == 0 || $moddepthlvl == $depthlvl} { + # targets if no in depth mode is enabled. rc file has normally + # been evaluated when its directory content was fetched: + # evaluate it here otherwise, when it is walked directly + # (limited access rc file from cache) + if {$tail eq {.modulerc} || $depthlvl == 0 || $moddepthlvl ==\ + $depthlvl} { if {![info exists g_rcfilesSourced($element)]} { execute-modulerc $element $modulename $modulename set g_rcfilesSourced($element) 1 From 60e38da3e6eff0a93b4b7f21d36397a00383d3c2 Mon Sep 17 00:00:00 2001 From: Xavier Delaruelle Date: Sat, 28 Nov 2026 17:28:26 +0100 Subject: [PATCH 03/28] Share code between display and test sub-commands cmdModuleDisplay and cmdModuleTest procedures were identical apart from the evaluation mode set and the title reported prior each module evaluation. Introduce displayModuleEvalList procedure to hold this common code. Assisted-by: Claude:claude-fable-5-2 Signed-off-by: Xavier Delaruelle --- tcl/subcmd.tcl.in | 31 +++++++++++-------------------- 1 file changed, 11 insertions(+), 20 deletions(-) diff --git a/tcl/subcmd.tcl.in b/tcl/subcmd.tcl.in index 47a593562..75e2c6939 100644 --- a/tcl/subcmd.tcl.in +++ b/tcl/subcmd.tcl.in @@ -133,10 +133,12 @@ proc cmdModuleList {show_oneperline show_mtime search_match args} { } } -proc cmdModuleDisplay {args} { - lappendState mode display +# evaluate modules in specified mode and report result of each evaluation +# under a title made of modulefile path +proc displayModuleEvalList {evalmode title_prefix mod_list} { + lappendState mode $evalmode set first_report 1 - foreach mod $args { + foreach mod $mod_list { lassign [getPathToModule $mod] modfile modname modnamevr if {$modfile ne {}} { # only one separator lines between 2 modules @@ -144,7 +146,7 @@ proc cmdModuleDisplay {args} { displaySeparatorLine set first_report 0 } - report [sgr hi $modfile]:\n + report "$title_prefix[sgr hi $modfile]:\n" execute-modulefile $modfile $modname modnamevr $mod 1 displaySeparatorLine } @@ -152,6 +154,10 @@ proc cmdModuleDisplay {args} { lpopState mode } +proc cmdModuleDisplay {args} { + displayModuleEvalList display {} $args +} + proc cmdModulePaths {mod} { set dir_list [getModulePathList exiterronundef] foreach dir $dir_list { @@ -2399,22 +2405,7 @@ proc cmdModuleResurface {cmd args} { } proc cmdModuleTest {args} { - lappendState mode test - set first_report 1 - foreach mod $args { - lassign [getPathToModule $mod] modfile modname modnamevr - if {$modfile ne {}} { - # only one separator lines between 2 modules - if {$first_report} { - displaySeparatorLine - set first_report 0 - } - report "Module Specific Test for [sgr hi $modfile]:\n" - execute-modulefile $modfile $modname modnamevr $mod 1 - displaySeparatorLine - } - } - lpopState mode + displayModuleEvalList test {Module Specific Test for } $args } proc cmdModuleClear {args} { From d925ca2254999d4cef58d549ae5afa4bd7e411ea Mon Sep 17 00:00:00 2001 From: Xavier Delaruelle Date: Sat, 28 Nov 2026 17:28:26 +0100 Subject: [PATCH 04/28] Simplify argument check of restore and saveshow sub-commands Number of arguments tells if a collection name has been provided and default collection name is obtained by appending it to the argument list. Assisted-by: Claude:claude-fable-5-2 Signed-off-by: Xavier Delaruelle --- tcl/subcmd.tcl.in | 24 ++++++++---------------- 1 file changed, 8 insertions(+), 16 deletions(-) diff --git a/tcl/subcmd.tcl.in b/tcl/subcmd.tcl.in index 75e2c6939..eef6570d6 100644 --- a/tcl/subcmd.tcl.in +++ b/tcl/subcmd.tcl.in @@ -701,14 +701,10 @@ proc cmdModuleSave {{coll default}} { } proc cmdModuleRestore {args} { - # distinguish between zero and one argument provided - if {![llength $args]} { - set arg_provided 0 - set coll default - } else { - set arg_provided 1 - set coll [lindex $args 0] - } + # distinguish between zero and one argument provided, default collection + # is targeted if no argument provided + set arg_provided [llength $args] + set coll [lindex [list {*}$args default] 0] # get corresponding collection, raise error if it does not exist unless # if no collection name has been provided or if __init__ @@ -821,14 +817,10 @@ proc cmdModuleSaverm {{coll default}} { } proc cmdModuleSaveshow {args} { - # distinguish between zero and one argument provided - if {![llength $args]} { - set arg_provided 0 - set coll default - } else { - set arg_provided 1 - set coll [lindex $args 0] - } + # distinguish between zero and one argument provided, default collection + # is targeted if no argument provided + set arg_provided [llength $args] + set coll [lindex [list {*}$args default] 0] # get corresponding collection, raise error if it does not exist unless # if no collection name has been provided or if __init__ From 98e14d9c4d3f6b1a5132daae0312ee7e1a9b1d32 Mon Sep 17 00:00:00 2001 From: Xavier Delaruelle Date: Sat, 28 Nov 2026 17:28:26 +0100 Subject: [PATCH 05/28] Merge bash and bash-eval setup in execShAndGetEnv Shell command and output parsing definitions were duplicated for bash and bash-eval shells, whereas only the way script is evaluated differs between them. Assisted-by: Claude:claude-fable-5-2 Signed-off-by: Xavier Delaruelle --- tcl/mfcmd.tcl | 28 +++++++--------------------- 1 file changed, 7 insertions(+), 21 deletions(-) diff --git a/tcl/mfcmd.tcl b/tcl/mfcmd.tcl index e73e754d6..58d912f06 100644 --- a/tcl/mfcmd.tcl +++ b/tcl/mfcmd.tcl @@ -1561,29 +1561,15 @@ proc execShAndGetEnv {elt_ignored_list shell script args} { set varvalmap [list {\"} \" \\\\ \\] set alvalmap [list {'\''} ' {'"'"'} '] } - bash { - ##nagelfar ignore +3 Found constant - set command "export -p; echo $sep; declare -f; echo $sep; alias;\ - echo $sep; complete; echo $sep; pwd; echo $sep; umask; echo $sep;\ - . [listTo shell $shdesc] 2>&1; echo $sep; export -p; echo $sep;\ - declare -f; echo $sep; alias; echo $sep; complete; echo $sep;\ - pwd; echo $sep; umask" - set varre {declare -x (\S+?)="(.*?)"$} - set funcre {(\S+?) \(\)\s?\n{\s?\n(.+?)\n}$} - set aliasre {alias (\S+?)='(.*?)'$} - set compre {complete (.+?) (\S+?)$} - set comprevar [list match value name] - set varvalmap [list {\"} \" \\\\ \\] - set alvalmap [list {'\''} '] - lappend shellopts --noprofile --norc - } - bash-eval { - ##nagelfar ignore +3 Found constant + bash - bash-eval { + ##nagelfar ignore Found constant + set shcmd [listTo shell $shdesc] + set srccmd [expr {[file tail $shell] eq {bash} ? ". $shcmd 2>&1" :\ + "eval \"\$($shcmd 2>/dev/null)\""}] set command "export -p; echo $sep; declare -f; echo $sep; alias;\ echo $sep; complete; echo $sep; pwd; echo $sep; umask; echo $sep;\ - eval \"\$([listTo shell $shdesc] 2>/dev/null)\"; echo $sep;\ - export -p; echo $sep; declare -f; echo $sep; alias; echo $sep;\ - complete; echo $sep; pwd; echo $sep; umask" + $srccmd; echo $sep; export -p; echo $sep; declare -f; echo $sep;\ + alias; echo $sep; complete; echo $sep; pwd; echo $sep; umask" set varre {declare -x (\S+?)="(.*?)"$} set funcre {(\S+?) \(\)\s?\n{\s?\n(.+?)\n}$} set aliasre {alias (\S+?)='(.*?)'$} From 8a1aed3a9c27e00a543c163983bd8907d9f7739a Mon Sep 17 00:00:00 2001 From: Xavier Delaruelle Date: Sun, 29 Nov 2026 08:01:55 +0100 Subject: [PATCH 06/28] Handle regular and NPO dependency structures with same code Dependency relations between loaded modules are recorded twice: once taking loading order into account and once in No Particular Order. Procedures setting and unsetting these relations repeated the same code for each set of structures. Loop over both sets instead. Assisted-by: Claude:claude-fable-5-2 Signed-off-by: Xavier Delaruelle --- tcl/modfind.tcl.in | 113 +++++++++++++++------------------------------ 1 file changed, 38 insertions(+), 75 deletions(-) diff --git a/tcl/modfind.tcl.in b/tcl/modfind.tcl.in index 065285b45..e0141f886 100644 --- a/tcl/modfind.tcl.in +++ b/tcl/modfind.tcl.in @@ -2338,45 +2338,27 @@ proc setModuleDependency {mod} { } } - switch -- [llength $lmprelist] { - 0 { + # build 'is depended by' relations, first taking loading order into + # account then not taking it (No Particular Order) + ##nagelfar ignore +2 Found constant + foreach {npo lmlist dep_list_vn vio_vn hash_vn} [list {} $lmprelist\ + deplist g_prereqViolation g_dependHash {NPO } $lmnpolist depnpolist\ + g_prereqNPOViolation g_dependNPOHash] { + ##nagelfar vartype dep_list_vn varName + upvar #0 $vio_vn vio_arr $hash_vn hash_arr + if {![llength $lmlist]} { # prereq not satisfied - reportDebug "set prereq violation state for '$mod'" - lappend ::g_prereqViolation($mod) $prereq - } - 1 { - set lmmod [lindex $lmprelist 0] - lappend deplist [list $lmmod] - # set 'is depended by' relations - lappend ::g_dependHash($lmmod) [list $mod] - } - default { - lappend deplist $lmprelist + reportDebug "set ${npo}prereq violation state for '$mod'" + lappend vio_arr($mod) $prereq + } else { + lappend $dep_list_vn $lmlist # many modules in prereq list, means they all set an optional dep - foreach lmmod $lmprelist { - lappend ::g_dependHash($lmmod) [list $mod 1] + set hashdep [list $mod] + if {[llength $lmlist] > 1} { + lappend hashdep 1 } - } - } - - # build 'is depended by' relations not taking loading order into account - switch -- [llength $lmnpolist] { - 0 { - # even on No Particular Order mode, prereq is not satisfied - reportDebug "set NPO prereq violation state for '$mod'" - lappend ::g_prereqNPOViolation($mod) $prereq - } - 1 { - set lmmod [lindex $lmnpolist 0] - lappend depnpolist [list $lmmod] - # set 'is depended by' relations - lappend ::g_dependNPOHash($lmmod) [list $mod] - } - default { - lappend depnpolist $lmnpolist - # many modules in prereq list, means they all set an optional dep - foreach lmmod $lmnpolist { - lappend ::g_dependNPOHash($lmmod) [list $mod 1] + foreach lmmod $lmlist { + lappend hash_arr($lmmod) $hashdep } } } @@ -2464,49 +2446,30 @@ proc setModuleDependency {mod} { # update dependency chain when unloading module proc unsetModuleDependency {mod} { - foreach lmmodlist $::g_moduleDepend($mod) { - set manymod [expr {[llength $lmmodlist] > 1}] - - # unset 'is depended by' mod relations - foreach lmmod $lmmodlist { - if {[info exists ::g_dependHash($lmmod)]} { - if {$manymod} { - set hashdep [list $mod 1] - } else { - set hashdep [list $mod] - } - lrem ::g_dependHash($lmmod) $hashdep - if {![llength $::g_dependHash($lmmod)]} { - unset ::g_dependHash($lmmod) - } - } - } - } - # unset mod's 'depends on' relation - reportDebug "unset requirements of '$mod'" - unset ::g_moduleDepend($mod) - - foreach lmmodlist $::g_moduleNPODepend($mod) { - set manymod [expr {[llength $lmmodlist] > 1}] - - # unset 'is depended by' mod relations - foreach lmmod $lmmodlist { - if {[info exists ::g_dependNPOHash($lmmod)]} { - if {$manymod} { - set hashdep [list $mod 1] - } else { - set hashdep [list $mod] - } - lrem ::g_dependNPOHash($lmmod) $hashdep - if {![llength $::g_dependNPOHash($lmmod)]} { - unset ::g_dependNPOHash($lmmod) + # process dependency structures taking loading order into account then + # those not taking it (No Particular Order) + foreach {npo dep_vn hash_vn} [list {} g_moduleDepend g_dependHash {NPO }\ + g_moduleNPODepend g_dependNPOHash] { + upvar #0 $dep_vn dep_arr $hash_vn hash_arr + foreach lmmodlist $dep_arr($mod) { + set hashdep [list $mod] + if {[llength $lmmodlist] > 1} { + lappend hashdep 1 + } + # unset 'is depended by' mod relations + foreach lmmod $lmmodlist { + if {[info exists hash_arr($lmmod)]} { + lrem hash_arr($lmmod) $hashdep + if {![llength $hash_arr($lmmod)]} { + unset hash_arr($lmmod) + } } } } + # unset mod's 'depends on' relation + reportDebug "unset ${npo}requirements of '$mod'" + unset dep_arr($mod) } - # unset mod's No Particular Order 'depends on' relation - reportDebug "unset NPO requirements of '$mod'" - unset ::g_moduleNPODepend($mod) # unset eventual violation states if {[info exists ::g_prereqViolation($mod)]} { From 69eaba3f84ed3c1528cd1a45405401ffc7ebd447 Mon Sep 17 00:00:00 2001 From: Xavier Delaruelle Date: Sun, 29 Nov 2026 08:01:55 +0100 Subject: [PATCH 07/28] Share sub-interpreter setup between evaluation kinds Evaluation of modulefile, modulerc and cache file each embedded a copy of the code creating the sub-interpreter at first use, dumping its initial state then resetting it prior each evaluation. Definition of the variables to set in the interpreter, including those coming from siteconfig, was also duplicated between modulefile and modulerc. Move this code into two procedures working from the name prefix of the global structures of each interpreter kind. Assisted-by: Claude:claude-fable-5-2 Signed-off-by: Xavier Delaruelle --- tcl/cache.tcl.in | 24 ++------ tcl/interp.tcl.in | 152 ++++++++++++++++++++-------------------------- 2 files changed, 69 insertions(+), 107 deletions(-) diff --git a/tcl/cache.tcl.in b/tcl/cache.tcl.in index 32c5e058b..b9b9bd956 100644 --- a/tcl/cache.tcl.in +++ b/tcl/cache.tcl.in @@ -272,26 +272,10 @@ proc execute-cachefile {cachefile modpath} { set itrp __cachefile reportTrace '$cachefile' {Evaluate cache file} - # create cachefile interpreter at first interpretation - if {![interp exists $itrp]} { - createInterp $itrp - - # dump initial interpreter state to restore it before each cachefile - # interpretation. - dumpInterpState $itrp g_cachefileVars g_cachefileArrayVars\ - g_cachefileUntrackVars g_cachefileProcs - - # interp has just been created - set fresh 1 - } else { - set fresh 0 - } - - # reset interp state command before each interpretation - resetInterpState $itrp $fresh g_cachefileVars g_cachefileArrayVars\ - g_cachefileUntrackVars g_cachefileProcs g_cachefileAliases\ - g_cachefileAliasesPassArg g_cachefileAliasesTraces\ - g_cachefileRenameCmds g_cachefileCommands + # create cachefile interpreter at first interpretation and reset its state + # before each interpretation + setupInterp $itrp g_cachefile g_cachefileAliases\ + g_cachefileAliasesPassArg g_cachefileAliasesTraces g_cachefileCommands # reset modulefile-specific variable before each interpretation interp eval $itrp set ::ModulesCurrentCachefile "{$cachefile}" diff --git a/tcl/interp.tcl.in b/tcl/interp.tcl.in index 3786f8b7f..2b293ff07 100644 --- a/tcl/interp.tcl.in +++ b/tcl/interp.tcl.in @@ -264,52 +264,13 @@ proc execute-modulefile {modfile modname modnamevrvar modspec requested\ # variable to define in modulefile interp if {![info exists ::g_modfileBaseVars]} { - # record module tool properties - set ::g_modfileBaseVars [list ModuleTool Modules ModuleToolVersion\ - [getState modules_release]] - - if {[info exists ::modulefile_extra_vars]} { - if {([llength $::modulefile_extra_vars] % 2) != 0} { - knerror "Invalid value '$::modulefile_extra_vars' (list must have\ - an even number of elements)\nfor siteconfig variable\ - 'modulefile_extra_vars'" - } - foreach {var val} $::modulefile_extra_vars { - if {[string first { } $var] != -1} { - knerror "Invalid variable name '$var'\ndefined in siteconfig\ - variable 'modulefile_extra_vars'" - } - } - lappend ::g_modfileBaseVars {*}$::modulefile_extra_vars - } - } - - # create modulefile interpreter at first interpretation - if {![interp exists $itrp]} { - createInterp $itrp - - # initialize global static variables for modulefile interp - foreach {var val} $::g_modfileBaseVars { - interp eval $itrp set ::$var "{$val}" - } - - # dump initial interpreter state to restore it before each modulefile - # interpretation. use same dump state for all modes/levels - if {![info exists ::g_modfileVars]} { - dumpInterpState $itrp g_modfileVars g_modfileArrayVars\ - g_modfileUntrackVars g_modfileProcs - } - - # interp has just been created - set fresh 1 - } else { - set fresh 0 + initInterpBaseVars g_modfileBaseVars modulefile_extra_vars } - # reset interp state command before each interpretation - resetInterpState $itrp $fresh g_modfileVars g_modfileArrayVars\ - g_modfileUntrackVars g_modfileProcs $aliasesVN $aliasesPassArgVN\ - $tracesVN g_modfileRenameCmds $dumpCommandsVN + # create modulefile interpreter at first interpretation and reset its + # state before each interpretation + setupInterp $itrp g_modfile $aliasesVN $aliasesPassArgVN $tracesVN\ + $dumpCommandsVN runHooks before-modulefile-eval $modfile $modname $modnamevr $modspec\ $mode $requested @@ -602,23 +563,7 @@ proc execute-modulerc {modfile modname modspec} { array set ::g_modrcAliasesTraces [list] # variable to define in modulerc interp - set ::g_modrcBaseVars [list ModuleTool Modules ModuleToolVersion\ - [getState modules_release]] - - if {[info exists ::modulerc_extra_vars]} { - if {([llength $::modulerc_extra_vars] % 2) != 0} { - knerror "Invalid value '$::modulerc_extra_vars' (list must have\ - an even number of elements)\nfor siteconfig variable\ - 'modulerc_extra_vars'" - } - foreach {var val} $::modulerc_extra_vars { - if {[string first { } $var] != -1} { - knerror "Invalid variable name '$var'\ndefined in siteconfig\ - variable 'modulerc_extra_vars'" - } - } - lappend ::g_modrcBaseVars {*}$::modulerc_extra_vars - } + initInterpBaseVars g_modrcBaseVars modulerc_extra_vars } # dedicate an interpreter per level of interpretation to have in case of @@ -626,32 +571,10 @@ proc execute-modulerc {modfile modname modspec} { set itrp __modrc_[depthState modulename] reportTrace '$modfile' {Evaluate modulerc} - # create modulerc interpreter at first interpretation - if {![interp exists $itrp]} { - createInterp $itrp - - # initialize global static variables for modulerc interp - foreach {var val} $::g_modrcBaseVars { - interp eval $itrp set ::$var "{$val}" - } - - # dump initial interpreter state to restore it before each modulerc - # interpretation. use same dump state for all levels - if {![info exists ::g_modrcVars]} { - dumpInterpState $itrp g_modrcVars g_modrcArrayVars\ - g_modrcUntrackVars g_modrcProcs - } - - # interp has just been created - set fresh 1 - } else { - set fresh 0 - } - - # reset interp state command before each interpretation - resetInterpState $itrp $fresh g_modrcVars g_modrcArrayVars\ - g_modrcUntrackVars g_modrcProcs g_modrcAliases g_modrcAliasesPassArg\ - g_modrcAliasesTraces g_modrcRenameCmds g_modrcCommands + # create modulerc interpreter at first interpretation and reset its state + # before each interpretation + setupInterp $itrp g_modrc g_modrcAliases g_modrcAliasesPassArg\ + g_modrcAliasesTraces g_modrcCommands runHooks before-modulerc-eval $modfile $modname @@ -732,6 +655,61 @@ proc formatInterpErrStackTrace {itrp modfile} { commands]]] } +# Define in global variable named base_vars_vn the variables to set in a kind +# of sub interpreter: module tool properties and variables defined through +# the siteconfig variable named extra_vars_name. +proc initInterpBaseVars {base_vars_vn extra_vars_name} { + upvar #0 $base_vars_vn base_vars $extra_vars_name extra_vars + # record module tool properties + set base_vars [list ModuleTool Modules ModuleToolVersion [getState\ + modules_release]] + + if {[info exists extra_vars]} { + if {([llength $extra_vars] % 2) != 0} { + knerror "Invalid value '$extra_vars' (list must have an even number\ + of elements)\nfor siteconfig variable '$extra_vars_name'" + } + foreach {var val} $extra_vars { + if {[string first { } $var] != -1} { + knerror "Invalid variable name '$var'\ndefined in siteconfig\ + variable '$extra_vars_name'" + } + } + lappend base_vars {*}$extra_vars + } +} + +# Create sub interpreter at first interpretation and reset its state before +# each interpretation. Names of the global structures holding initial state +# of interpreter start with prefix (g_modfile, g_modrc or g_cachefile). +proc setupInterp {itrp prefix aliasesVN aliasesPassArgVN tracesVN\ + dumpCommandsVN} { + upvar #0 ${prefix}BaseVars base_vars ${prefix}Vars dump_vars + set fresh [expr {![interp exists $itrp]}] + if {$fresh} { + createInterp $itrp + + # initialize global static variables of interp + if {[info exists base_vars]} { + foreach {var val} $base_vars { + interp eval $itrp set ::$var "{$val}" + } + } + + # dump initial interpreter state to restore it before each + # interpretation. use same dump state for all modes/levels + if {![info exists dump_vars]} { + dumpInterpState $itrp ${prefix}Vars ${prefix}ArrayVars\ + ${prefix}UntrackVars ${prefix}Procs + } + } + + # reset interp state command before each interpretation + resetInterpState $itrp $fresh ${prefix}Vars ${prefix}ArrayVars\ + ${prefix}UntrackVars ${prefix}Procs $aliasesVN $aliasesPassArgVN\ + $tracesVN ${prefix}RenameCmds $dumpCommandsVN +} + # Save list of the defined procedure and the global variables with their # associated values set in sub interpreter passed as argument. Global # structures are used to save these information and the name of these From b9550bc3584c68d36568fcc5df930fb884dea405 Mon Sep 17 00:00:00 2001 From: Xavier Delaruelle Date: Sun, 29 Nov 2026 08:01:56 +0100 Subject: [PATCH 08/28] Share option parsing of module-forbid, module-hide and module-warn These three modulefile commands parsed their specific options through similar loops, each one reporting the same invalid option and missing value errors. Introduce a procedure that parses arguments from a description of the options supported by the calling command. module-tag keeps its own loop as its first argument that is not an option is the tag to set rather a module specification. Assisted-by: Claude:claude-fable-5-2 Signed-off-by: Xavier Delaruelle --- tcl/mfcmd.tcl | 129 ++++++++++++++++---------------------------------- 1 file changed, 41 insertions(+), 88 deletions(-) diff --git a/tcl/mfcmd.tcl b/tcl/mfcmd.tcl index 58d912f06..6fec0dce3 100644 --- a/tcl/mfcmd.tcl +++ b/tcl/mfcmd.tcl @@ -417,47 +417,50 @@ proc setModspecTag {modspec tag {props {}} {is_regexp 0}} { } } -proc module-forbid {args} { - # parse application criteria arguments to determine if command apply - lassign [parseApplicationCriteriaArgs 1 [expr {[getConf\ - nearly_forbidden_days] * 86400}] {*}$args] apply isnearly after\ - otherargs is_regexp - - # parse remaining argument list, do it even if command does not apply to - # raise any command specification error - foreach arg $otherargs { +# Parse arguments of a module-* modulefile command left once application +# criteria are extracted. optspec associates each option of command to either +# the name of the variable of calling context that receives option value or +# to this variable name and the value to set it to when option takes no +# value. Return the other arguments, which are the module specifications. +proc parseModspecCommandArgs {is_regexp optspec args} { + array set optarr $optspec + set modarglist {} + foreach arg $args { if {[info exists nextargisval]} { - ##nagelfar vartype nextargisval varName - set $nextargisval $arg + uplevel 1 [list set $nextargisval $arg] unset nextargisval - } else { - switch -glob -- $arg { - --nearly-message { - set nextargisval nearlymessage - } - --message { - set nextargisval message - } - -* { - # a regular expression may start with a dash character - if {!$is_regexp} { - knerror "Invalid option '$arg'" - } - lappend modarglist $arg - } - default { - lappend modarglist $arg - } + } elseif {[info exists optarr($arg)]} { + if {[llength $optarr($arg)] > 1} { + uplevel 1 [list set {*}$optarr($arg)] + } else { + set nextargisval $optarr($arg) + set prevarg $arg } - set prevarg $arg + # a regular expression may start with a dash character + } elseif {[string index $arg 0] eq {-} && !$is_regexp} { + knerror "Invalid option '$arg'" + } else { + lappend modarglist $arg } } if {[info exists nextargisval]} { knerror "Missing value for '$prevarg' option" } + return $modarglist +} - if {![info exists modarglist]} { +proc module-forbid {args} { + # parse application criteria arguments to determine if command apply + lassign [parseApplicationCriteriaArgs 1 [expr {[getConf\ + nearly_forbidden_days] * 86400}] {*}$args] apply isnearly after\ + otherargs is_regexp + + # parse remaining argument list, do it even if command does not apply to + # raise any command specification error + set modarglist [parseModspecCommandArgs $is_regexp [list --nearly-message\ + nearlymessage --message message] {*}$otherargs] + if {![llength $modarglist]} { knerror {No module specified in argument} } @@ -499,34 +502,10 @@ proc module-hide {args} { # parse remaining argument list, do it even if command does not apply to # raise any command specification error - foreach arg $otherargs { - switch -glob -- $arg { - --hard { - # hardened stealth - set hidinglvl 2 - } - --soft { - # soften level of camouflage - set hidinglvl 0 - } - --hidden-loaded { - # module should stay hidden once being loaded - set hiddenloaded 1 - } - -* { - # a regular expression may start with a dash character - if {!$is_regexp} { - knerror "Invalid option '$arg'" - } - lappend modarglist $arg - } - default { - lappend modarglist $arg - } - } - } - - if {![info exists modarglist]} { + set modarglist [parseModspecCommandArgs $is_regexp [list --hard\ + {hidinglvl 2} --soft {hidinglvl 0} --hidden-loaded {hiddenloaded 1}]\ + {*}$otherargs] + if {![llength $modarglist]} { knerror {No module specified in argument} } @@ -2703,38 +2682,12 @@ proc module-warn {args} { # parse remaining argument list, do it even if command does not apply to # raise any command specification error - foreach arg $otherargs { - if {[info exists nextargisval]} { - ##nagelfar vartype nextargisval varName - set $nextargisval $arg - unset nextargisval - } else { - switch -glob -- $arg { - --message { - set nextargisval message - } - -* { - # a regular expression may start with a dash character - if {!$is_regexp} { - knerror "Invalid option '$arg'" - } - lappend modarglist $arg - } - default { - lappend modarglist $arg - } - } - set prevarg $arg - } - } - - if {[info exists nextargisval]} { - knerror "Missing value for '$prevarg' option" - } + set modarglist [parseModspecCommandArgs $is_regexp [list --message\ + message] {*}$otherargs] if {![info exists message]} { knerror {No message specified in argument} } - if {![info exists modarglist]} { + if {![llength $modarglist]} { knerror {No module specified in argument} } From 8360d5826a5d96760c8ced43df5fd1ea4717d819 Mon Sep 17 00:00:00 2001 From: Xavier Delaruelle Date: Sun, 29 Nov 2026 08:01:56 +0100 Subject: [PATCH 09/28] Evaluate modules on help sub-command like on display and test Help sub-command held a copy of the loop shared by display and test sub-commands, with its own evaluation mode and title. Assisted-by: Claude:claude-fable-5-2 Signed-off-by: Xavier Delaruelle --- tcl/subcmd.tcl.in | 18 +----------------- 1 file changed, 1 insertion(+), 17 deletions(-) diff --git a/tcl/subcmd.tcl.in b/tcl/subcmd.tcl.in index eef6570d6..5e88f0551 100644 --- a/tcl/subcmd.tcl.in +++ b/tcl/subcmd.tcl.in @@ -2737,23 +2737,7 @@ proc cmdModuleRefresh {} { } proc cmdModuleHelp {args} { - lappendState mode help - set first_report 1 - foreach arg $args { - lassign [getPathToModule $arg] modfile modname modnamevr - - if {$modfile ne {}} { - # only one separator lines between 2 modules - if {$first_report} { - displaySeparatorLine - set first_report 0 - } - report "Module Specific Help for [sgr hi $modfile]:\n" - execute-modulefile $modfile $modname modnamevr $arg 1 - displaySeparatorLine - } - } - lpopState mode + displayModuleEvalList help {Module Specific Help for } $args if {![llength $args]} { reportUsage } From 8299a9c2763bf2ec225b5fcb8ff654382e0cfa8c Mon Sep 17 00:00:00 2001 From: Xavier Delaruelle Date: Sun, 29 Nov 2026 08:01:56 +0100 Subject: [PATCH 10/28] Render true and false values with same procedure Procedures rendering a true and a false value were made of the same switch over shell kinds, where only the rendered value differs. Define both values in a single switch and pick the one to render. Assisted-by: Claude:claude-fable-5-2 Signed-off-by: Xavier Delaruelle --- tcl/envmngt.tcl.in | 84 +++++++++++++++------------------------------- 1 file changed, 27 insertions(+), 57 deletions(-) diff --git a/tcl/envmngt.tcl.in b/tcl/envmngt.tcl.in index 3c6770e56..6cfbdd74e 100644 --- a/tcl/envmngt.tcl.in +++ b/tcl/envmngt.tcl.in @@ -43,90 +43,60 @@ proc renderFalse {} { # setup flag to render only once setState false_rendered 1 - - # render a false value most of the time through a variable assignment - # that will be looked at in the shell module function calling - # modulecmd.tcl to return in turns a boolean status. Except for python - # and cmake, the value assigned to variable is also returned as the - # entire rendering status - switch -- [getState shelltype] { - sh - csh - fish { - # no need to set a variable on real shells as last statement - # result can easily be checked - lappend ::g_shcode_out {test 0 = 1;} - } - tcl { - lappend ::g_shcode_out {set _mlstatus 0;} - } - cmd { - lappend ::g_shcode_out {set errorlevel=1} - } - perl { - lappend ::g_shcode_out {{ no strict 'vars'; $_mlstatus = 0; }} - } - python { - lappend ::g_shcode_out {_mlstatus = False} - } - ruby { - lappend ::g_shcode_out {_mlstatus = false} - } - lisp { - lappend ::g_shcode_out {nil} - } - cmake { - lappend ::g_shcode_out {set(_mlstatus FALSE)} - } - r { - lappend ::g_shcode_out {mlstatus <- FALSE} - } - pwsh { - lappend ::g_shcode_out {Set-Variable -Name "_mlstatus" -Value $false\ - -Scope Global} - } - } + renderBool 0 } proc renderTrue {} { - # render a true value most of the time through a variable assignment that - # will be looked at in the shell module function calling modulecmd.tcl to - # return in turns a boolean status. Except for python, cmake, and pwsh, - # the value assigned to variable is also returned as the full rendering - # status + renderBool 1 +} + +# render a boolean value most of the time through a variable assignment that +# will be looked at in the shell module function calling modulecmd.tcl to +# return in turns a boolean status. Except for python, cmake and pwsh, the +# value assigned to variable is also returned as the entire rendering status +proc renderBool {bool} { + # code rendering false value then code rendering true value switch -- [getState shelltype] { sh - csh - fish { # no need to set a variable on real shells as last statement # result can easily be checked - lappend ::g_shcode_out {test 0;} + set code_list [list {test 0 = 1;} {test 0;}] } tcl { - lappend ::g_shcode_out {set _mlstatus 1;} + set code_list [list {set _mlstatus 0;} {set _mlstatus 1;}] } cmd { - lappend ::g_shcode_out {set errorlevel=0} + set code_list [list {set errorlevel=1} {set errorlevel=0}] } perl { - lappend ::g_shcode_out {{ no strict 'vars'; $_mlstatus = 1; }} + set code_list [list {{ no strict 'vars'; $_mlstatus = 0; }} {{ no\ + strict 'vars'; $_mlstatus = 1; }}] } python { - lappend ::g_shcode_out {_mlstatus = True} + set code_list [list {_mlstatus = False} {_mlstatus = True}] } ruby { - lappend ::g_shcode_out {_mlstatus = true} + set code_list [list {_mlstatus = false} {_mlstatus = true}] } lisp { - lappend ::g_shcode_out {t} + set code_list [list {nil} {t}] } cmake { - lappend ::g_shcode_out {set(_mlstatus TRUE)} + set code_list [list {set(_mlstatus FALSE)} {set(_mlstatus TRUE)}] } r { - lappend ::g_shcode_out {mlstatus <- TRUE} + set code_list [list {mlstatus <- FALSE} {mlstatus <- TRUE}] } pwsh { - lappend ::g_shcode_out {Set-Variable -Name "_mlstatus" -Value\ - $true -Scope Global} + set code_list [list {Set-Variable -Name "_mlstatus" -Value $false\ + -Scope Global} {Set-Variable -Name "_mlstatus" -Value $true\ + -Scope Global}] + } + default { + return } } + lappend ::g_shcode_out [lindex $code_list $bool] } proc renderText {text} { From 076ee43cea88737d424f26b47e972ab28e99f305 Mon Sep 17 00:00:00 2001 From: Xavier Delaruelle Date: Sun, 29 Nov 2026 08:01:56 +0100 Subject: [PATCH 11/28] Render X resource set and unset with same procedure An X resource string is unset by merging it with an empty value, so shell code rendered to unset a resource was a copy of the code setting it, value excepted. Render both through a single procedure. Resource string is still rendered without escaping when unset on sh, csh and fish shells. Assisted-by: Claude:claude-fable-5-2 Signed-off-by: Xavier Delaruelle --- tcl/envmngt.tcl.in | 165 +++++++++++++++------------------------------ 1 file changed, 55 insertions(+), 110 deletions(-) diff --git a/tcl/envmngt.tcl.in b/tcl/envmngt.tcl.in index 6cfbdd74e..3f20968dc 100644 --- a/tcl/envmngt.tcl.in +++ b/tcl/envmngt.tcl.in @@ -144,6 +144,58 @@ proc renderText {text} { } } +# Render code merging a string into X resource database. This string is made +# of resource name var, a colon and val, which is either resource value +# preceded by a space or an empty string to unset resource. +proc renderXResourceMerge {xrdb var val} { + set res $var:$val + switch -- [getState shelltype] { + sh - csh - fish { + # resource string is rendered unescaped when resource is unset + if {$val ne {}} { + set res [charEscaped $res \"] + } + lappend ::g_shcode_out "echo \"$res\" | $xrdb -merge;" + } + tcl { + set res [charEscaped $res \"] + lappend ::g_shcode_out "set XRDBPIPE \[open \"|$xrdb -merge\" r+\];" + lappend ::g_shcode_out "puts \$XRDBPIPE \"$res\";" + lappend ::g_shcode_out {close $XRDBPIPE;} + lappend ::g_shcode_out {unset XRDBPIPE;} + } + perl { + set res [charEscaped $res \"] + lappend ::g_shcode_out "open(XRDBPIPE, \"|$xrdb -merge\");" + lappend ::g_shcode_out "print XRDBPIPE \"$res\\n\";" + lappend ::g_shcode_out {close XRDBPIPE;} + } + python { + set res [charEscaped $res '] + lappend ::g_shcode_out "subprocess.Popen(\['$xrdb', '-merge'\],\ + stdin=subprocess.PIPE).communicate(input='$res\\n')" + } + ruby { + set res [charEscaped $res '] + lappend ::g_shcode_out "Open3.popen2('$xrdb -merge') {|i,o,t|\ + i.puts '$res'}" + } + lisp { + lappend ::g_shcode_out "(shell-command-to-string \"echo $res |\ + $xrdb -merge\")" + } + cmake { + set res [charEscaped $res \"] + lappend ::g_shcode_out "execute_process(COMMAND echo \"$res\"\ + COMMAND $xrdb -merge)" + } + r { + set res [charEscaped $res {\\'}] + lappend ::g_shcode_out "system('$xrdb -merge', input='$res')" + } + } +} + proc renderSettings {} { global g_stateEnvVars g_stateAliases g_stateFunctions g_stateCompletes\ g_newXResources g_delXResources g_shcode_out @@ -512,59 +564,7 @@ proc renderSettings {} { } } } else { - switch -- [getState shelltype] { - sh - csh - fish { - set var [charEscaped $var \"] - set val [charEscaped $val \"] - lappend g_shcode_out "echo \"$var: $val\" | $xrdb -merge;" - } - tcl { - lappend g_shcode_out "set XRDBPIPE \[open \"|$xrdb -merge\"\ - r+\];" - set var [charEscaped $var \"] - set val [charEscaped $val \"] - lappend g_shcode_out "puts \$XRDBPIPE \"$var: $val\";" - lappend g_shcode_out {close $XRDBPIPE;} - lappend g_shcode_out {unset XRDBPIPE;} - } - perl { - lappend g_shcode_out "open(XRDBPIPE, \"|$xrdb -merge\");" - set var [charEscaped $var \"] - set val [charEscaped $val \"] - lappend g_shcode_out "print XRDBPIPE \"$var: $val\\n\";" - lappend g_shcode_out {close XRDBPIPE;} - } - python { - set var [charEscaped $var '] - set val [charEscaped $val '] - lappend g_shcode_out "subprocess.Popen(\['$xrdb',\ - '-merge'\],\ - stdin=subprocess.PIPE).communicate(input='$var:\ - $val\\n')" - } - ruby { - set var [charEscaped $var '] - set val [charEscaped $val '] - lappend g_shcode_out "Open3.popen2('$xrdb -merge') {|i,o,t|\ - i.puts '$var: $val'}" - } - lisp { - lappend g_shcode_out "(shell-command-to-string \"echo $var:\ - $val | $xrdb -merge\")" - } - cmake { - set var [charEscaped $var \"] - set val [charEscaped $val \"] - lappend g_shcode_out "execute_process(COMMAND echo \"$var:\ - $val\" COMMAND $xrdb -merge)" - } - r { - set var [charEscaped $var {\\'}] - set val [charEscaped $val {\\'}] - lappend g_shcode_out "system('$xrdb -merge', input='$var:\ - $val')" - } - } + renderXResourceMerge $xrdb $var " $val" } } } @@ -588,63 +588,8 @@ proc renderSettings {} { # xresource strings are unset by emptying their value since there # is no command of xrdb that can properly remove one property - switch -- [getState shelltype] { - sh - csh - fish { - foreach var $xres_to_del { - lappend g_shcode_out "echo \"$var:\" | $xrdb -merge;" - } - } - tcl { - foreach var $xres_to_del { - lappend g_shcode_out "set XRDBPIPE \[open \"|$xrdb -merge\"\ - r+\];" - set var [charEscaped $var \"] - lappend g_shcode_out "puts \$XRDBPIPE \"$var:\";" - lappend g_shcode_out {close $XRDBPIPE;} - lappend g_shcode_out {unset XRDBPIPE;} - } - } - perl { - foreach var $xres_to_del { - lappend g_shcode_out "open(XRDBPIPE, \"|$xrdb -merge\");" - set var [charEscaped $var \"] - lappend g_shcode_out "print XRDBPIPE \"$var:\\n\";" - lappend g_shcode_out {close XRDBPIPE;} - } - } - python { - foreach var $xres_to_del { - set var [charEscaped $var '] - lappend g_shcode_out "subprocess.Popen(\['$xrdb', '-merge'\],\ - stdin=subprocess.PIPE).communicate(input='$var:\\n')" - } - } - ruby { - foreach var $xres_to_del { - set var [charEscaped $var '] - lappend g_shcode_out "Open3.popen2('$xrdb -merge') {|i,o,t|\ - i.puts '$var:'}" - } - } - lisp { - foreach var $xres_to_del { - lappend g_shcode_out "(shell-command-to-string \"echo $var: |\ - $xrdb -merge\")" - } - } - cmake { - foreach var $xres_to_del { - set var [charEscaped $var \"] - lappend g_shcode_out "execute_process(COMMAND echo \"$var:\"\ - COMMAND $xrdb -merge)" - } - } - r { - foreach var $xres_to_del { - set var [charEscaped $var {\\'}] - lappend g_shcode_out "system('$xrdb -merge', input='$var:')" - } - } + foreach var $xres_to_del { + renderXResourceMerge $xrdb $var {} } } From 5557e389d569e7c1f741356414c9fa305b86cb30 Mon Sep 17 00:00:00 2001 From: Xavier Delaruelle Date: Sun, 29 Nov 2026 08:01:56 +0100 Subject: [PATCH 12/28] Unset loaded module properties without testing their existence Rely on the unset command option that ignores undefined variables rather testing variable existence beforehand. Assisted-by: Claude:claude-fable-5-2 Signed-off-by: Xavier Delaruelle --- tcl/modfind.tcl.in | 35 ++++++++--------------------------- 1 file changed, 8 insertions(+), 27 deletions(-) diff --git a/tcl/modfind.tcl.in b/tcl/modfind.tcl.in index e0141f886..6b37e65b9 100644 --- a/tcl/modfind.tcl.in +++ b/tcl/modfind.tcl.in @@ -1684,9 +1684,7 @@ proc setLoadedSourceSh {mod args} { } proc unsetLoadedSourceSh {mod} { - if {[info exists ::g_loadedModuleSourceSh($mod)]} { - unset ::g_loadedModuleSourceSh($mod) - } + unset -nocomplain ::g_loadedModuleSourceSh($mod) } proc getLoadedSourceSh {mod} { @@ -1721,9 +1719,7 @@ proc setLoadedConflictByModroot {mod args} { } proc unsetLoadedConflict {mod} { - if {[info exists ::g_loadedModuleConflict($mod)]} { - unset ::g_loadedModuleConflict($mod) - } + unset -nocomplain ::g_loadedModuleConflict($mod) unsetLoadedConflictByModroot $mod } @@ -1809,9 +1805,7 @@ proc setLoadedPrereq {mod args} { } proc unsetLoadedPrereq {mod} { - if {[info exists ::g_loadedModulePrereq($mod)]} { - unset ::g_loadedModulePrereq($mod) - } + unset -nocomplain ::g_loadedModulePrereq($mod) } proc getLoadedPrereq {mod} { @@ -1840,9 +1834,7 @@ proc setLoadedPrereqPath {mod args} { } proc unsetLoadedPrereqPath {mod} { - if {[info exists ::g_loadedModulePrereqPath($mod)]} { - unset ::g_loadedModulePrereqPath($mod) - } + unset -nocomplain ::g_loadedModulePrereqPath($mod) } proc getLoadedPrereqPath {mod {prereq {}}} { @@ -1875,15 +1867,8 @@ proc setLoadedAltname {mod args} { } proc unsetLoadedAltname {mod} { - if {[info exists ::g_loadedModuleAltname($mod)]} { - unset ::g_loadedModuleAltname($mod) - } - if {[info exists ::g_loadedModuleAliasAltname($mod)]} { - unset ::g_loadedModuleAliasAltname($mod) - } - if {[info exists ::g_loadedModuleAutoAltname($mod)]} { - unset ::g_loadedModuleAutoAltname($mod) - } + unset -nocomplain ::g_loadedModuleAltname($mod)\ + ::g_loadedModuleAliasAltname($mod) ::g_loadedModuleAutoAltname($mod) } proc getLoadedAltname {mod {typelist {sym alias autosym}} {with_type 0}} { @@ -1970,9 +1955,7 @@ proc setLoadedVariant {mod args} { } proc unsetLoadedVariant {mod} { - if {[info exists ::g_loadedModuleVariant($mod)]} { - unset ::g_loadedModuleVariant($mod) - } + unset -nocomplain ::g_loadedModuleVariant($mod) } proc getLoadedVariant {mod} { @@ -2030,9 +2013,7 @@ proc setLoadedUse {mod args} { } proc unsetLoadedUse {mod} { - if {[info exists ::g_loadedModuleUse($mod)]} { - unset ::g_loadedModuleUse($mod) - } + unset -nocomplain ::g_loadedModuleUse($mod) } proc getLoadedUse {mod} { From 9c1eec076afa1c6e990d8fb9e11abe6d6da7b705 Mon Sep 17 00:00:00 2001 From: Xavier Delaruelle Date: Sun, 29 Nov 2026 08:01:56 +0100 Subject: [PATCH 13/28] Build loaded module and variant designation in one statement Variant list is expanded into the designation whether it is empty or not, so there is no need to test it first. Assisted-by: Claude:claude-fable-5-2 Signed-off-by: Xavier Delaruelle --- tcl/modfind.tcl.in | 13 ++----------- 1 file changed, 2 insertions(+), 11 deletions(-) diff --git a/tcl/modfind.tcl.in b/tcl/modfind.tcl.in index 6b37e65b9..29d0784e8 100644 --- a/tcl/modfind.tcl.in +++ b/tcl/modfind.tcl.in @@ -1967,12 +1967,7 @@ proc getLoadedVariant {mod} { proc getLoadedModuleWithVariantList {} { set modvrlist [list] foreach mod [getEnvLoadedModulePropertyParsedList name] { - set modvr [list $mod] - set vrlist [getVariantList $mod 5] - if {[llength $vrlist]} { - lappend modvr {*}$vrlist - } - lappend modvrlist $modvr + lappend modvrlist [list $mod {*}[getVariantList $mod 5]] } return $modvrlist } @@ -1980,11 +1975,7 @@ proc getLoadedModuleWithVariantList {} { proc getLoadedModuleWithVariantSaveTagArrayList {} { array set tag_arr {} foreach mod [getEnvLoadedModulePropertyParsedList name] { - set modvr [list $mod] - set vrlist [getVariantList $mod 1] - if {[llength $vrlist]} { - lappend modvr {*}$vrlist - } + set modvr [list $mod {*}[getVariantList $mod 1]] # create entry in array only if tags set set tag_list [getSaveTagList $mod] if {[llength $tag_list]} { From 5558f1b7548ac5b5f5af28d467cb4b743c4434ce Mon Sep 17 00:00:00 2001 From: Xavier Delaruelle Date: Sun, 29 Nov 2026 08:01:56 +0100 Subject: [PATCH 14/28] Test other variant of module is loaded or loading with same code Procedures testing if a module is loaded or is loading with other variants than those specified only differed by the loaded or loading state they look at. Keep one procedure with an argument to select this state. Assisted-by: Claude:claude-fable-5-2 Signed-off-by: Xavier Delaruelle --- tcl/modfind.tcl.in | 17 ++++++----------- tcl/subcmd.tcl.in | 2 +- 2 files changed, 7 insertions(+), 12 deletions(-) diff --git a/tcl/modfind.tcl.in b/tcl/modfind.tcl.in index 29d0784e8..5684839f8 100644 --- a/tcl/modfind.tcl.in +++ b/tcl/modfind.tcl.in @@ -1061,20 +1061,15 @@ proc getVariantList {mod {report 0} {excl_dflval 0} {from 0}} { return $ret } -proc isOtherVariantOfModuleLoading {mod} { +# is module loaded, or loading if loading is enabled, with other variants +# than those specified +proc isOtherVariantOfModuleLoaded {mod {loading 0}} { set mod_name [getModuleNameAndVersFromVersSpec $mod] set mod_has_variant [expr {$mod ne $mod_name}] - return [expr {$mod_has_variant && [isModuleLoading $mod_name] && ![string\ - length [getLoadedMatchingName $mod {} 1]]}] -} - -proc isOtherVariantOfModuleLoaded {mod} { - set mod_name [getModuleNameAndVersFromVersSpec $mod] - set mod_has_variant [expr {$mod ne $mod_name}] - - return [expr {$mod_has_variant && [isModuleLoaded $mod_name] && ![string\ - length [getLoadedMatchingName $mod]]}] + return [expr {$mod_has_variant && ($loading ? [isModuleLoading $mod_name]\ + : [isModuleLoaded $mod_name]) && ![string length\ + [getLoadedMatchingName $mod {} $loading]]}] } proc isModuleDotHidden {mod} { diff --git a/tcl/subcmd.tcl.in b/tcl/subcmd.tcl.in index 5e88f0551..2f0cde607 100644 --- a/tcl/subcmd.tcl.in +++ b/tcl/subcmd.tcl.in @@ -1209,7 +1209,7 @@ proc cmdModuleLoad {context uasked tryload loadany tag_list modulepath_list\ } # stop if same mod is loaded but with a different set of variants } elseif {[isOtherVariantOfModuleLoaded $modnamevr] ||\ - [isOtherVariantOfModuleLoading $modnamevr]} { + [isOtherVariantOfModuleLoaded $modnamevr 1]} { # no error if ConUn mechanism handles unload of this module if {![getConf auto_handling] || ![getConf conflict_unload] ||\ $isloading} { From adf80e6e8dbd351e73475f656d7507c443867cf8 Mon Sep 17 00:00:00 2001 From: Xavier Delaruelle Date: Sun, 29 Nov 2026 08:01:56 +0100 Subject: [PATCH 15/28] Remove unused argument of tag matching procedure List of tags already known for a module was passed to the procedure collecting the tags matching this module, which appended to this list but never read it. Assisted-by: Claude:claude-fable-5-2 Signed-off-by: Xavier Delaruelle --- tcl/modfind.tcl.in | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/tcl/modfind.tcl.in b/tcl/modfind.tcl.in index 5684839f8..8cda9de20 100644 --- a/tcl/modfind.tcl.in +++ b/tcl/modfind.tcl.in @@ -679,18 +679,13 @@ proc getModuleTagProp {mod fpmod tag prop} { # Gather all tags applying to a given module proc collectModuleTags {mod} { - if {[info exists ::g_tagHash($mod)]} { - set known_tag_list $::g_tagHash($mod) - } else { - set known_tag_list {} - } - set tag_list [getMatchingTagList $mod $known_tag_list] + set tag_list [getMatchingTagList $mod] if {[llength $tag_list]} { setModuleTag $mod {*}$tag_list } } -proc getMatchingTagList {mod {known_tag_list {}}} { +proc getMatchingTagList {mod} { set tag_list {} set modroot [getModuleRootFromVersSpec $mod] # look if mod matches one of the module specs applying to mod root @@ -702,7 +697,6 @@ proc getMatchingTagList {mod {known_tag_list {}}} { $::g_moduleTagRootSpec($modroot) $i] { # add tag if one modspec matches mod if {[modEq $tmodspec $mod eqstart 1 0 1]} { - lappend known_tag_list $tag lappend tag_list $tag break } From 355ba3256e8d7e00fbabef1a12372bca55e31ac2 Mon Sep 17 00:00:00 2001 From: Xavier Delaruelle Date: Sun, 29 Nov 2026 08:01:56 +0100 Subject: [PATCH 16/28] Cache tags and extra tags of loaded modules with same code Tags and extra tags recorded in environment for loaded modules were cached by two loops only differing by the property read and the procedure called to record it. Assisted-by: Claude:claude-fable-5-2 Signed-off-by: Xavier Delaruelle --- tcl/modfind.tcl.in | 26 ++++++++++---------------- 1 file changed, 10 insertions(+), 16 deletions(-) diff --git a/tcl/modfind.tcl.in b/tcl/modfind.tcl.in index 8cda9de20..d4e6f4f63 100644 --- a/tcl/modfind.tcl.in +++ b/tcl/modfind.tcl.in @@ -2627,23 +2627,17 @@ proc cacheCurrentModules {} { } # cache declared tags of loaded modules - foreach modtag [getEnvLoadedModulePropertyParsedList tag] { - set tag_list [lassign $modtag mod] - if {[info exists modvrarr($mod)]} { - set mod_with_vr $modvrarr($mod) - } else { - set mod_with_vr {} - } - setModuleAndVariantsTag $mod $mod_with_vr {*}$tag_list - } - foreach modtag [getEnvLoadedModulePropertyParsedList extratag] { - set tag_list [lassign $modtag mod] - if {[info exists modvrarr($mod)]} { - set mod_with_vr $modvrarr($mod) - } else { - set mod_with_vr {} + foreach {prop setproc} [list tag setModuleAndVariantsTag extratag\ + setModuleAndVariantsExtraTag] { + foreach modtag [getEnvLoadedModulePropertyParsedList $prop] { + set tag_list [lassign $modtag mod] + if {[info exists modvrarr($mod)]} { + set mod_with_vr $modvrarr($mod) + } else { + set mod_with_vr {} + } + $setproc $mod $mod_with_vr {*}$tag_list } - setModuleAndVariantsExtraTag $mod $mod_with_vr {*}$tag_list } foreach modstickyrule [getEnvLoadedModulePropertyParsedList\ stickyrule] { From 78f97390ee93a831d19fe202106b93b462506e6c Mon Sep 17 00:00:00 2001 From: Xavier Delaruelle Date: Sun, 29 Nov 2026 08:01:56 +0100 Subject: [PATCH 17/28] Read initial environment like other collections Both callers of the procedure reading collection content handled the specific case of the initial environment, read from an environment variable rather a file, then checked collection read was valid. Move these two steps into this procedure. Assisted-by: Claude:claude-fable-5-2 Signed-off-by: Xavier Delaruelle --- tcl/coll.tcl.in | 31 +++++++++++++++---------------- tcl/subcmd.tcl.in | 16 ++-------------- 2 files changed, 17 insertions(+), 30 deletions(-) diff --git a/tcl/coll.tcl.in b/tcl/coll.tcl.in index d79455d59..992e9c3b8 100644 --- a/tcl/coll.tcl.in +++ b/tcl/coll.tcl.in @@ -256,6 +256,12 @@ proc formatCollectionContent {path_list mod_list tag_arrser header {sgr 0}} { # read given collection file and return the path and module lists it defines proc readCollectionContent {collfile colldesc} { + # read specific __init__ collection from __MODULES_LMINIT env var + if {$collfile eq {__init__}} { + return [parseCollectionContent [getEnvLoadedModulePropertyParsedList\ + init]] + } + # read file if {[catch { set fdata [split [readFile $collfile] \n] @@ -263,7 +269,13 @@ proc readCollectionContent {collfile colldesc} { reportErrorAndExit "Collection $colldesc cannot be read.\n$errMsg" } - return [parseCollectionContent $fdata] + # collection should at least define a path or a mod, but initial env may be + # totally empty + set content [parseCollectionContent $fdata] + if {![llength [lindex $content 0]] && ![llength [lindex $content 1]]} { + reportErrorAndExit "$colldesc is not a valid collection" + } + return $content } proc parseCollectionContent {fdata} { @@ -335,14 +347,8 @@ proc parseCollectionContent {fdata} { # return specified collection content and differences compared to currently # defined environment proc getDiffBetweenCurEnvAndColl {collfile colldesc} { - # read specific __init__ collection from __MODULES_LMINIT env var - if {$collfile eq {__init__}} { - lassign [parseCollectionContent [getEnvLoadedModulePropertyParsedList\ - init]] coll_path_list coll_mod_list coll_tag_arrser - } else { - lassign [readCollectionContent $collfile $colldesc] coll_path_list\ - coll_mod_list coll_tag_arrser - } + lassign [readCollectionContent $collfile $colldesc] coll_path_list\ + coll_mod_list coll_tag_arrser # build list of module tagged auto-loaded in collection array set coll_tag_arr $coll_tag_arrser @@ -353,13 +359,6 @@ proc getDiffBetweenCurEnvAndColl {collfile colldesc} { } } - # collection should at least define a path or a mod, but initial env may be - # totally empty - if {$collfile ne {__init__} && ![llength $coll_path_list] && ![llength\ - $coll_mod_list]} { - reportErrorAndExit "$colldesc is not a valid collection" - } - # fetch what is currently loaded set curr_path_list [getModulePathList returnempty 0] # get current loaded module list diff --git a/tcl/subcmd.tcl.in b/tcl/subcmd.tcl.in index 2f0cde607..510c70d3a 100644 --- a/tcl/subcmd.tcl.in +++ b/tcl/subcmd.tcl.in @@ -827,22 +827,10 @@ proc cmdModuleSaveshow {args} { lassign [findCollections $coll exact [expr {!$arg_provided}]\ $arg_provided] collfile colldesc - # read specific __init__ collection from __MODULES_LMINIT env var + lassign [readCollectionContent $collfile $colldesc] coll_path_list\ + coll_mod_list coll_tag_arrser if {$collfile eq {__init__}} { - lassign [parseCollectionContent [getEnvLoadedModulePropertyParsedList\ - init]] coll_path_list coll_mod_list coll_tag_arrser set collfile {initial environment} - set coll __init__ - } else { - lassign [readCollectionContent $collfile $colldesc] coll_path_list\ - coll_mod_list coll_tag_arrser - } - - # collection should at least define a path or a mod, but initial env may be - # totally empty - if {$coll ne {__init__} && ![llength $coll_path_list] && ![llength\ - $coll_mod_list]} { - reportErrorAndExit "$colldesc is not a valid collection" } displaySeparatorLine From 3ed173a980d34ea64a4df86408f176491efe8bea Mon Sep 17 00:00:00 2001 From: Xavier Delaruelle Date: Sun, 29 Nov 2026 08:01:56 +0100 Subject: [PATCH 18/28] Simplify argument parsing of getenv and getvariant commands Arguments were dispatched through a switch on their number, with nested tests on the first one. The only thing varying is the --return-value option that may be set first: detect it then assign remaining arguments. Assisted-by: Claude:claude-fable-5-2 Signed-off-by: Xavier Delaruelle --- tcl/mfcmd.tcl | 37 +++++-------------------------------- 1 file changed, 5 insertions(+), 32 deletions(-) diff --git a/tcl/mfcmd.tcl b/tcl/mfcmd.tcl index 6fec0dce3..f1e99dbce 100644 --- a/tcl/mfcmd.tcl +++ b/tcl/mfcmd.tcl @@ -675,40 +675,13 @@ proc setenv-wh {args} { # parse arguments sent to the getenv modulefile command proc parseGetenvCommandArgs {cmd args} { - set returnval 0 - set valifundef {} - switch -- [llength $args] { - 1 { - set var [lindex $args 0] - } - 2 { - switch -- [lindex $args 0] { - --return-value { - set returnval 1 - set var [lindex $args 1] - } - default { - set var [lindex $args 0] - set valifundef [lindex $args 1] - } - } - } - 3 { - if {[lindex $args 0] ne {--return-value}} { - set wrongargs 1 - } else { - set returnval 1 - set var [lindex $args 1] - set valifundef [lindex $args 2] - } - } - default { - set wrongargs 1 - } - } + # option may be set prior the one or two arguments expected + set returnval [expr {[llength $args] > 1 && [lindex $args 0] eq\ + {--return-value}}] + lassign [lrange $args $returnval end] var valifundef set argname [expr {$cmd eq {getenv} ? {variable} : {name}}] - if {[info exists wrongargs]} { + if {[llength $args] - $returnval ni {1 2}} { knerror "wrong # args: should be \"$cmd ?--return-value? $argname\ ?valifundef?\"" } From 8a7e6c9732e26e1b4c0a50f02ed7e42d8fa20e70 Mon Sep 17 00:00:00 2001 From: Xavier Delaruelle Date: Sun, 29 Nov 2026 08:01:56 +0100 Subject: [PATCH 19/28] Select puts channel with same code whether newline is asked or not Channel targeted by puts modulefile command was determined by two switches, one per number of arguments, the second one being dedicated to the -nonewline option. Detect this option first then use a single switch to select channel. Assisted-by: Claude:claude-fable-5-2 Signed-off-by: Xavier Delaruelle --- tcl/mfcmd.tcl | 61 +++++++++++++++++---------------------------------- 1 file changed, 20 insertions(+), 41 deletions(-) diff --git a/tcl/mfcmd.tcl b/tcl/mfcmd.tcl index f1e99dbce..b52f306ae 100644 --- a/tcl/mfcmd.tcl +++ b/tcl/mfcmd.tcl @@ -939,51 +939,31 @@ proc setModulesVersion {val} { # enabled) or postpone content channel send after rendering on stdout the # relative environment changes required by the modulefile proc putsModfileCmd {itrp args} { + # option may be set prior the one or two arguments expected + set nonewline [expr {[llength $args] > 1 && [lindex $args 0] eq\ + {-nonewline}}] + set msg [lindex $args end] # determine if puts call targets the stdout or stderr channel - switch -- [llength $args] { + switch -- [expr {[llength $args] - $nonewline}] { 1 { - # create struct with newline status and message to output - set deferPuts [list 1 [lindex $args 0]] + set defer_var ::g_stdoutPuts } 2 { - switch -- [lindex $args 0] { - -nonewline { - set deferPuts [list 0 [lindex $args 1]] - } + switch -- [lindex $args end-1] { stdout { - set deferPuts [list 1 [lindex $args 1]] + set defer_var ::g_stdoutPuts } prestdout { - set deferPrePuts [list 1 [lindex $args 1]] + set defer_var ::g_prestdoutPuts } stderr { - set reportArgs [list [lindex $args 1]] + set to_stderr 1 } log { - set logPuts [lindex $args 1] + set to_log 1 } } } - 3 { - if {[lindex $args 0] eq {-nonewline}} { - switch -- [lindex $args 1] { - stdout { - set deferPuts [list 0 [lindex $args 2]] - } - prestdout { - set deferPrePuts [list 0 [lindex $args 2]] - } - stderr { - set reportArgs [list [lindex $args 2] 1] - } - log { - set logPuts [lindex $args 2] - } - } - } else { - set wrongNumArgs 1 - } - } default { set wrongNumArgs 1 } @@ -994,21 +974,20 @@ proc putsModfileCmd {itrp args} { if {[info exists wrongNumArgs]} { knerror {wrong # args: should be "puts ?-nonewline? ?channelId? string"} # send content to log system - } elseif {[info exists logPuts]} { - log $logPuts - # defer puts if it targets stdout (see renderSettings) - } elseif {[info exists deferPuts]} { + } elseif {[info exists to_log]} { + log $msg + # defer puts if it targets stdout (see renderSettings): record newline + # status and message to output + } elseif {[info exists defer_var]} { # current module is qualified for refresh evaluation lappendState -nodup refresh_qualified [currentState modulename] - lappend ::g_stdoutPuts {*}$deferPuts - } elseif {[info exists deferPrePuts]} { - lappendState -nodup refresh_qualified [currentState modulename] - lappend ::g_prestdoutPuts {*}$deferPrePuts + ##nagelfar vartype defer_var varName + lappend $defer_var [expr {!$nonewline}] $msg # if it targets stderr call report, which knows what channel to use - } elseif {[info exists reportArgs]} { + } elseif {[info exists to_stderr]} { # report message only if not silent if {[isVerbosityLevel concise]} { - report {*}$reportArgs + report $msg $nonewline } # pass to real puts command if not related to stdout and do that in modfile # interpreter context to get access to eventual specific channel From 2af9a367c44837d6c4d966bee7e0aa19bd43cddf Mon Sep 17 00:00:00 2001 From: Xavier Delaruelle Date: Sun, 29 Nov 2026 08:01:56 +0100 Subject: [PATCH 20/28] Merge identical arms of module-info command shell and shelltype actions only differ by the state queried, which is named after the action. trace and tracepat return the same value. Assisted-by: Claude:claude-fable-5-2 Signed-off-by: Xavier Delaruelle --- tcl/mfcmd.tcl | 18 ++++-------------- 1 file changed, 4 insertions(+), 14 deletions(-) diff --git a/tcl/mfcmd.tcl b/tcl/mfcmd.tcl index b52f306ae..4b1c19f31 100644 --- a/tcl/mfcmd.tcl +++ b/tcl/mfcmd.tcl @@ -80,11 +80,11 @@ proc module-info {what {more {}}} { specified { return [currentState specifiedname] } - shell { + shell - shelltype { if {$more ne {}} { - return [expr {[getState shell] eq $more}] + return [expr {[getState $what] eq $more}] } else { - return [getState shell] + return [getState $what] } } flags { @@ -92,13 +92,6 @@ proc module-info {what {more {}}} { # a zero integer value to avoid breaking modulefiles using it return 0 } - shelltype { - if {$more ne {}} { - return [expr {[getState shelltype] eq $more}] - } else { - return [getState shelltype] - } - } user { # C-version specific option, not relevant for Tcl-version but return # an empty value or false to avoid breaking modulefiles using it @@ -116,10 +109,7 @@ proc module-info {what {more {}}} { return {} } } - trace { - return {} - } - tracepat { + trace - tracepat { return {} } type { From f4b93fdaa9385566fb3abf812b9dd990f49b7688 Mon Sep 17 00:00:00 2001 From: Xavier Delaruelle Date: Sun, 29 Nov 2026 08:01:56 +0100 Subject: [PATCH 21/28] Share env var declaration code of whatis and scan modes Modulefile commands changing an environment variable do not alter environment when evaluated in whatis or scan mode. Their procedures dedicated to these modes all indicated variable name to configured traces then defined variable if undefined. Do both in the procedure defining the variable, renamed accordingly. Assisted-by: Claude:claude-fable-5-2 Signed-off-by: Xavier Delaruelle --- tcl/envmngt.tcl.in | 8 ++++++-- tcl/mfcmd.tcl | 20 ++++---------------- tcl/modscan.tcl | 20 ++++---------------- 3 files changed, 14 insertions(+), 34 deletions(-) diff --git a/tcl/envmngt.tcl.in b/tcl/envmngt.tcl.in index 3f20968dc..e78974ef6 100644 --- a/tcl/envmngt.tcl.in +++ b/tcl/envmngt.tcl.in @@ -1392,9 +1392,13 @@ proc getQuarantineVarList {{report_bad 0}} { return $var_list } -proc setEnvVarIfUndefined {var_name value} { +# Indicate env var name to configured traces and define this variable with +# an empty value if undefined, to get it known when referred afterward. For +# modulefile commands evaluated in a mode that does not change environment. +proc declareEnvVar {var_name} { + setState current_envvar $var_name if {![isEnvVarDefined $var_name]} { - set ::env($var_name) $value + set ::env($var_name) {} } } diff --git a/tcl/mfcmd.tcl b/tcl/mfcmd.tcl index 4b1c19f31..f6da7b4c1 100644 --- a/tcl/mfcmd.tcl +++ b/tcl/mfcmd.tcl @@ -656,10 +656,7 @@ proc setenv-un {args} { proc setenv-wh {args} { lassign [parseSetenvCommandArgs load set {*}$args] bhv var val - # indicate env var name to configured traces - setState current_envvar $var - - setEnvVarIfUndefined $var {} + declareEnvVar $var return {} } @@ -792,10 +789,7 @@ proc unsetenv-un {args} { proc unsetenv-wh {args} { lassign [parseUnsetenvCommandArgs load noop {*}$args] bhv var val - # indicate env var name to configured traces - setState current_envvar $var - - setEnvVarIfUndefined $var {} + declareEnvVar $var return {} } @@ -1047,10 +1041,7 @@ proc edit-path-wh {cmd args} { idx_val ign_refcount val_set_is_delim glob_match bhv var path_list\ priority - # indicate env var name to configured traces - setState current_envvar $var - - setEnvVarIfUndefined $var {} + declareEnvVar $var return {} } @@ -2399,10 +2390,7 @@ proc pushenv-un {var val} { # optimized pushenv for whatis mode (same approach than setenv-wh) proc pushenv-wh {var val} { - # indicate env var name to configured traces - setState current_envvar $var - - setEnvVarIfUndefined $var {} + declareEnvVar $var return {} } diff --git a/tcl/modscan.tcl b/tcl/modscan.tcl index a6f065dbe..f8e0f3e57 100644 --- a/tcl/modscan.tcl +++ b/tcl/modscan.tcl @@ -47,12 +47,9 @@ proc variant-sc {itrp args} { proc setenv-sc {args} { lassign [parseSetenvCommandArgs load set {*}$args] bhv var val - # indicate env var name to configured traces - setState current_envvar $var - recordScanModuleElt $var setenv envvar - setEnvVarIfUndefined $var {} + declareEnvVar $var return {} } @@ -61,9 +58,6 @@ proc edit-path-sc {cmd args} { idx_val ign_refcount val_set_is_delim glob_match bhv var path_list\ priority - # indicate env var name to configured traces - setState current_envvar $var - recordScanModuleElt $var $cmd envvar # record MODULEPATH edition as "module use" command @@ -71,29 +65,23 @@ proc edit-path-sc {cmd args} { recordScanModuleUsePathList $path_list } - setEnvVarIfUndefined $var {} + declareEnvVar $var return {} } proc pushenv-sc {var val} { recordScanModuleElt $var pushenv envvar - # indicate env var name to configured traces - setState current_envvar $var - - setEnvVarIfUndefined $var {} + declareEnvVar $var return {} } proc unsetenv-sc {args} { lassign [parseUnsetenvCommandArgs load noop {*}$args] bhv var val - # indicate env var name to configured traces - setState current_envvar $var - recordScanModuleElt $var unsetenv envvar - setEnvVarIfUndefined $var {} + declareEnvVar $var return {} } From adefa0471407c8d54675801b3f38489382217888 Mon Sep 17 00:00:00 2001 From: Xavier Delaruelle Date: Sun, 29 Nov 2026 08:01:56 +0100 Subject: [PATCH 22/28] Reuse list difference test to check module is unloadable Loop testing that all modules depending on a module are part of the unloading list is what the procedure testing if a list has elements not found in another list does. Assisted-by: Claude:claude-fable-5-2 Signed-off-by: Xavier Delaruelle --- tcl/modeval.tcl | 23 +++++------------------ 1 file changed, 5 insertions(+), 18 deletions(-) diff --git a/tcl/modeval.tcl b/tcl/modeval.tcl index 2d24edec6..657c31574 100644 --- a/tcl/modeval.tcl +++ b/tcl/modeval.tcl @@ -527,25 +527,12 @@ proc getDependentLoadedModuleList {modlist {strong 1} {direct 1} {nporeq 0}\ # may given module be automatically unloaded # unmodlist: pass a list of modules that are going to be unloaded proc isModuleUnloadable {mod {unmodlist {}}} { - set ret 1 - # module may be unloaded if it has been automatically loaded, it is not - # tagged keep-loaded and it is not sticky. - if {[isModuleTagged $mod loaded 1] || [isModuleTagged $mod keep-loaded\ - 1] || [isModuleSticky $mod]} { - set ret 0 - # there should no one requiring module or these dependent should be part of - # the unloaded/unloading list - } else { - foreach depmod [getDirectDependentList $mod] { - if {$depmod ni $unmodlist} { - set ret 0 - break - } - } - } - - return $ret + # tagged keep-loaded and it is not sticky. there should no one requiring + # module or these dependent should be part of the unloaded/unloading list + return [expr {![isModuleTagged $mod loaded 1] && ![isModuleTagged $mod\ + keep-loaded 1] && ![isModuleSticky $mod] && ![isDiffBetweenList\ + [getDirectDependentList $mod] $unmodlist]}] } # gets the list of all loaded modules which are required by passed mod_list From 38c37fcb17d0abc34c013ffaab5c317d126cf9d0 Mon Sep 17 00:00:00 2001 From: Xavier Delaruelle Date: Sun, 29 Nov 2026 08:01:56 +0100 Subject: [PATCH 23/28] Pad message lines with join rather loops Lines of an error message and lines of a recorded message block were padded by loops tracking first line to treat it differently. Joining lines with a padded newline does the same. Assisted-by: Claude:claude-fable-5-2 Signed-off-by: Xavier Delaruelle --- tcl/report.tcl.in | 25 ++++--------------------- 1 file changed, 4 insertions(+), 21 deletions(-) diff --git a/tcl/report.tcl.in b/tcl/report.tcl.in index 88e125366..8a3844bb6 100644 --- a/tcl/report.tcl.in +++ b/tcl/report.tcl.in @@ -266,17 +266,9 @@ proc reportMsgRecord {header {hidden 0}} { } } - # display each line - set first 1 - foreach line $linelist { - append dispmsg \n - if {$first} { - set first 0 - } else { - append dispmsg $padding - } - append dispmsg $padding$line - } + # display each line, those after first one are padded twice. a + # recorded message always holds text, so line list is not empty + append dispmsg \n$padding[join $linelist \n$padding$padding] } reportSeparateNextContent report $dispmsg @@ -605,16 +597,7 @@ proc __report {message {nonewline 0} {immed 0} {padnl 0}} { } # prefix msg lines after first one with 2 spaces if {$padnl} { - set first 1 - foreach line [split $message \n] { - if {$first} { - set first 0 - } else { - append padmsg "\n " - } - append padmsg $line - } - set message $padmsg + set message [join [split $message \n] "\n "] } # protect from issue with fd, just ignore it From 8a6ea66fdbe3986f9c27d92e6d1492278372cc82 Mon Sep 17 00:00:00 2001 From: Xavier Delaruelle Date: Sun, 29 Nov 2026 08:01:56 +0100 Subject: [PATCH 24/28] Set linter message properties in one statement Each arm of the switch on message severity set the same three variables. Assisted-by: Claude:claude-fable-5-2 Signed-off-by: Xavier Delaruelle --- tcl/report.tcl.in | 23 +++++------------------ 1 file changed, 5 insertions(+), 18 deletions(-) diff --git a/tcl/report.tcl.in b/tcl/report.tcl.in index 8a3844bb6..89679f288 100644 --- a/tcl/report.tcl.in +++ b/tcl/report.tcl.in @@ -2006,24 +2006,11 @@ proc displayLinterOutput {linter output} { set colidx [string first : $line] set linenum [string trimleft [string range $line 5\ $colidx-1]] - set severity [string index $line $colidx+2] - switch -- $severity { - W { - set severity WARNING - set sgrkey wa - set raisecnt 0 - } - E { - set severity ERROR - set sgrkey er - set raisecnt 1 - } - default { - set severity NOTICE - set sgrkey in - set raisecnt 0 - } - } + lassign [switch -- [string index $line $colidx+2] { + W {list WARNING wa 0} + E {list ERROR er 1} + default {list NOTICE in 0} + }] severity sgrkey raisecnt set msg [string range $line $colidx+4 end] # start recorded message properties From 226cb0f6d5a412e8e55410740183dc425c7295e5 Mon Sep 17 00:00:00 2001 From: Xavier Delaruelle Date: Sun, 29 Nov 2026 08:01:57 +0100 Subject: [PATCH 25/28] Merge modulefile and virtual arms of search sub-command Both kinds of element were handled by the same code, the path of the file to evaluate excepted. Assisted-by: Claude:claude-fable-5-2 Signed-off-by: Xavier Delaruelle --- tcl/subcmd.tcl.in | 26 +++++++++----------------- 1 file changed, 9 insertions(+), 17 deletions(-) diff --git a/tcl/subcmd.tcl.in b/tcl/subcmd.tcl.in index 510c70d3a..ea82140db 100644 --- a/tcl/subcmd.tcl.in +++ b/tcl/subcmd.tcl.in @@ -257,30 +257,22 @@ proc cmdModuleSearch {args} { # build list of modulefile to interpret foreach elt [array names mod_list] { switch -- [lindex $mod_list($elt) 0] { - modulefile { - if {[isModuleTagged $elt forbidden 0 $dir/$elt]} { - # register any error occurring on element matching search - if {[modEqAny $mod_pattern_list $elt]} { - set err_list($elt) [list accesserr [getForbiddenMsg\ - $elt $dir/$elt]] - } + modulefile - virtual { + if {[lindex $mod_list($elt) 0] eq {virtual}} { + set eltfile [lindex $mod_list($elt) 2] } else { - set interp_list($elt) $dir/$elt - # register module name in a global list (shared across - # modulepaths) to get hints when solving aliases/version - set full_list($elt) 1 + set eltfile $dir/$elt } - } - virtual { - if {[isModuleTagged $elt forbidden 0 [lindex $mod_list($elt)\ - 2]]} { + if {[isModuleTagged $elt forbidden 0 $eltfile]} { # register any error occurring on element matching search if {[modEqAny $mod_pattern_list $elt]} { set err_list($elt) [list accesserr [getForbiddenMsg\ - $elt [lindex $mod_list($elt) 2]]] + $elt $eltfile]] } } else { - set interp_list($elt) [lindex $mod_list($elt) 2] + set interp_list($elt) $eltfile + # register module name in a global list (shared across + # modulepaths) to get hints when solving aliases/version set full_list($elt) 1 } } From 176b95006fd69b77fbf484f1580d355769a6a347 Mon Sep 17 00:00:00 2001 From: Xavier Delaruelle Date: Sun, 29 Nov 2026 08:01:57 +0100 Subject: [PATCH 26/28] Clear match caches without testing their existence Unsetting an array that does not exist is not an error. Assisted-by: Claude:claude-fable-5-2 Signed-off-by: Xavier Delaruelle --- tcl/subcmd.tcl.in | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/tcl/subcmd.tcl.in b/tcl/subcmd.tcl.in index ea82140db..ceb49e421 100644 --- a/tcl/subcmd.tcl.in +++ b/tcl/subcmd.tcl.in @@ -1688,15 +1688,9 @@ proc cmdModuleUnload {context match auto force onlyureq args} { # update cache arrays unsetLoadedModule $modname $modfile - if {[array exists ::g_isLoadedMatchSpecificPathMemCache]} { - array unset ::g_isLoadedMatchSpecificPathMemCache - } - if {[array exists ::g_isLoadingMatchSpecificPathMemCache]} { - array unset ::g_isLoadingMatchSpecificPathMemCache - } - if {[array exists ::g_loadRequirementModuleListMemCache]} { - array unset ::g_loadRequirementModuleListMemCache - } + array unset ::g_isLoadedMatchSpecificPathMemCache + array unset ::g_isLoadingMatchSpecificPathMemCache + array unset ::g_loadRequirementModuleListMemCache # unregister declared source-sh unsetEnvLoadedModuleProperty $modname sourcesh From 76076717ccabf6b87bb74f6cfe34da628553ee89 Mon Sep 17 00:00:00 2001 From: Xavier Delaruelle Date: Sun, 29 Nov 2026 08:01:57 +0100 Subject: [PATCH 27/28] Share code of procedures selecting alternative definitions The five procedures that select, depending on configuration, the alternative definition of a procedure all renamed it into its generic name the same way: previous alternative gets its name back, debug trace is moved and selected alternative is recorded. Move this code into one procedure. Assisted-by: Claude:claude-fable-5-2 Signed-off-by: Xavier Delaruelle --- tcl/modspec.tcl | 107 ++++++++++++++++-------------------------------- 1 file changed, 35 insertions(+), 72 deletions(-) diff --git a/tcl/modspec.tcl b/tcl/modspec.tcl index 213640c64..d4101ba24 100644 --- a/tcl/modspec.tcl +++ b/tcl/modspec.tcl @@ -18,6 +18,29 @@ ########################################################################## +# Rename alternative procedure procname into the generic procedure name it +# implements, unless already done. Alternative procedure previously renamed +# this way gets its own name back. Return 1 if an alternative procedure +# defined for another context has been replaced, 0 otherwise. +proc defineProcFromAlt {name procname} { + upvar #0 g_${name}_proc curprocname + set replaced 0 + # define proc if not done yet or if it was defined for another context + if {[info procs $name] eq {} || $curprocname ne $procname} { + if {[info exists curprocname]} { + # remove existing debug trace if any + initProcReportTrace remove $name + rename ::$name ::$curprocname + set replaced 1 + } + rename ::$procname ::$name + # set report traces if some debug mode enabled + initProcReportTrace add $name + set curprocname $procname + } + return $replaced +} + # Define procedure to get how many parts between passed name and mod are equal # Adapt procedure code whether icase is enabled or disabled proc defineModStartNbProc {icase} { @@ -25,19 +48,8 @@ proc defineModStartNbProc {icase} { if {$icase} { append procname Icase } - # define proc if not done yet or if it was defined for another context - if {[info procs modStartNb] eq {} || $::g_modStartNb_proc ne $procname} { - if {[info exists ::g_modStartNb_proc]} { - # remove existing debug trace if any - initProcReportTrace remove modStartNb - rename ::modStartNb ::$::g_modStartNb_proc - } - ##nagelfar syntax modStartNb x x - rename ::$procname ::modStartNb - # set report traces if some debug mode enabled - initProcReportTrace add modStartNb - set ::g_modStartNb_proc $procname - } + ##nagelfar syntax modStartNb x x + defineProcFromAlt modStartNb $procname } # alternative definitions of modStartNb proc @@ -94,21 +106,8 @@ proc defineGetEqArrayKeyProc {impdfl} { if {$impdfl} { append procname Impdfl } - - # define proc if not done yet or if it was defined for another context - if {[info procs getEqArrayKey] eq {} || $::g_getEqArrayKey_proc ne\ - $procname} { - if {[info exists ::g_getEqArrayKey_proc]} { - # remove existing debug trace if any - initProcReportTrace remove getEqArrayKey - rename ::getEqArrayKey ::$::g_getEqArrayKey_proc - } - ##nagelfar syntax getEqArrayKey x x - rename ::$procname ::getEqArrayKey - # set report traces if some debug mode enabled - initProcReportTrace add getEqArrayKey - set ::g_getEqArrayKey_proc $procname - } + ##nagelfar syntax getEqArrayKey x x + defineProcFromAlt getEqArrayKey $procname } # alternative definitions of getEqArrayKey proc @@ -255,20 +254,8 @@ proc defineModVersCmpProc {icase extdfl} { if {$extdfl} { append procname Extdfl } - - # define proc if not done yet or if it was defined for another context - if {[info procs modVersCmp] eq {} || $::g_modVersCmp_proc ne $procname} { - if {[info exists ::g_modVersCmp_proc]} { - # remove existing debug trace if any - initProcReportTrace remove modVersCmp - rename ::modVersCmp ::$::g_modVersCmp_proc - } - ##nagelfar syntax modVersCmp x x x x x? - rename ::$procname ::modVersCmp - # set report traces if some debug mode enabled - initProcReportTrace add modVersCmp - set ::g_modVersCmp_proc $procname - } + ##nagelfar syntax modVersCmp x x x x x? + defineProcFromAlt modVersCmp $procname } # alternative definitions of modVersCmp proc @@ -600,21 +587,10 @@ proc defineModEqProc {icase extdfl} { if {$extdfl} { append procname Extdfl } - - # define proc if not done yet or if it was defined for another context - if {[info procs modEq] eq {} || $::g_modEq_proc ne $procname} { - if {[info exists ::g_modEq_proc]} { - # remove existing debug trace if any - initProcReportTrace remove modEq - rename ::modEq ::$::g_modEq_proc - # drop existing mem cache - array unset ::g_modEqMemCache - } - ##nagelfar syntax modEq x x x? x? x? x? x? x? - rename ::$procname ::modEq - # set report traces if some debug mode enabled - initProcReportTrace add modEq - set ::g_modEq_proc $procname + ##nagelfar syntax modEq x x x? x? x? x? x? x? + if {[defineProcFromAlt modEq $procname]} { + # drop existing mem cache + array unset ::g_modEqMemCache } } @@ -1275,21 +1251,8 @@ proc defineParseModuleSpecificationProc {advverspec} { # resolved configured variant shortcut getConf variant_shortcut } - # define proc if not done yet or if it was defined for another context - if {[info procs parseModuleSpecification] eq {} ||\ - $::g_parseModuleSpecification_proc ne $procname} { - if {[info exists ::g_parseModuleSpecification_proc]} { - # remove existing debug trace if any - initProcReportTrace remove parseModuleSpecification - rename ::parseModuleSpecification\ - ::$::g_parseModuleSpecification_proc - } - ##nagelfar syntax parseModuleSpecification x x x x x* - rename ::$procname ::parseModuleSpecification - # set report traces if some debug mode enabled - initProcReportTrace add parseModuleSpecification - set ::g_parseModuleSpecification_proc $procname - } + ##nagelfar syntax parseModuleSpecification x x x x x* + defineProcFromAlt parseModuleSpecification $procname } # when advanced_version_spec option is enabled, parse argument list to set in From 7a8d3e08d425a61c7e42f1c58ace5ea1dddc385d Mon Sep 17 00:00:00 2001 From: Xavier Delaruelle Date: Sun, 29 Nov 2026 08:01:57 +0100 Subject: [PATCH 28/28] Merge -a and --all option arms on command-line parsing -a is a synonym of --all on search and listing sub-commands. Both arms held a copy of this sub-command list. Assisted-by: Claude:claude-fable-5-2 Signed-off-by: Xavier Delaruelle --- tcl/main.tcl.in | 16 ++++------------ 1 file changed, 4 insertions(+), 12 deletions(-) diff --git a/tcl/main.tcl.in b/tcl/main.tcl.in index 4f85467e8..358296f25 100644 --- a/tcl/main.tcl.in +++ b/tcl/main.tcl.in @@ -216,19 +216,11 @@ proc parseModuleCommandArgs {topcall cmd ignerr ml_spec args} { lappend otherargs $arg } } - --all - --show_hidden { - # include hidden modules only on a limited set of command - if {$cmd in [list avail aliases search whatis ml list lint\ - savelist spider]} { - set ::asked_hiding_threshold 2 - } else { - lappend otherargs $arg - } - } - -a { + -a - --all - --show_hidden { # -a option has a different meaning whether sub-command is use - # or one of the search/listing sub-commands - if {$cmd eq {use}} { + # or one of the search/listing sub-commands. include hidden + # modules only on a limited set of command + if {$arg eq {-a} && $cmd eq {use}} { set addpath_pos append } elseif {$cmd in [list avail aliases search whatis ml list\ lint savelist spider]} {