From 4245a6897a1869e341412b0dc02f46e7c17f776d Mon Sep 17 00:00:00 2001 From: Xavier Delaruelle Date: Thu, 19 Nov 2026 17:50:42 -0600 Subject: [PATCH 1/2] Add --ignore option to sh-to-mod sub-command Let sh-to-mod withdraw some kinds of environment change made by the evaluated shell script, like the --ignore option of the source-sh modulefile command does. Both now share the same argument parsing. When set before the sub-command name, the option and its value are moved right after this name. When set after the shell argument, they are left in place as they are then arguments for the script. Fixes #532 Assisted-by: Claude:claude-fable-5-2 Signed-off-by: Xavier Delaruelle --- NEWS.rst | 4 + doc/source/changes.rst | 5 + .../source-shell-script-in-modulefile.rst | 4 + doc/source/module.rst | 23 ++- init/Makefile | 2 + init/bash_completion.in | 12 +- init/fish_completion | 1 + init/tcsh_completion.in | 2 +- init/zsh-functions/_module.in | 5 + tcl/main.tcl.in | 33 ++++- tcl/mfcmd.tcl | 24 ++-- tcl/report.tcl.in | 5 +- tcl/subcmd.tcl.in | 6 +- testsuite/completion.00-init/021-bash.exp | 10 ++ testsuite/completion.00-init/031-zsh.exp | 5 + .../modules.70-maint/312-sh-to-mod-ignore.exp | 136 ++++++++++++++++++ 16 files changed, 259 insertions(+), 18 deletions(-) create mode 100644 testsuite/modules.70-maint/312-sh-to-mod-ignore.exp diff --git a/NEWS.rst b/NEWS.rst index a22456e56..c27d2b21d 100644 --- a/NEWS.rst +++ b/NEWS.rst @@ -95,6 +95,10 @@ Modules 5.8.0 (not yet released) evaluated with :mfcmd:`source-sh` modulefile command or :subcmd:`sh-to-mod` sub-command. ``umask`` element is added to the ``--ignore`` option of :mfcmd:`source-sh`. +* Add the :option:`--ignore` option to the :subcmd:`sh-to-mod` sub-command + to ignore the changes made by the evaluated shell script on specified shell + elements, like done by the ``--ignore`` option of :mfcmd:`source-sh` + modulefile command. (fix issue #532) .. _5.7 release notes: diff --git a/doc/source/changes.rst b/doc/source/changes.rst index 4d551512e..029fdc729 100644 --- a/doc/source/changes.rst +++ b/doc/source/changes.rst @@ -886,6 +886,8 @@ The following command line switches appeared on Modules 5. | 5.7 | :option:`-p`, :option:`-P`, | | | :option:`--no-modulepath-ignore` | +------------+-----------------------------------------------------------------+ +| 5.8 | :option:`--ignore` | ++------------+-----------------------------------------------------------------+ :option:`--output` @@ -1009,6 +1011,9 @@ The following module sub-commands appeared on Modules 5. Shell mode ``bash-eval`` is introduced on Modules version ``5.4``. + Starting Modules 5.8, the :option:`--ignore` option is added to ignore the + changes made by shell script on specified shell elements. + :subcmd:`source` Starting Modules 5.2, this sub-command accepts modulefile specification as diff --git a/doc/source/design/source-shell-script-in-modulefile.rst b/doc/source/design/source-shell-script-in-modulefile.rst index dcea17d68..6922ddf8f 100644 --- a/doc/source/design/source-shell-script-in-modulefile.rst +++ b/doc/source/design/source-shell-script-in-modulefile.rst @@ -238,6 +238,10 @@ Specification - ``umask`` (added in version 5.8) - Shell elements changed by sourced script and listed on ``--ignore`` option are ignored (no modulefile command relative to these changes are produced) + - Option is also available on ``sh-to-mod`` sub-command starting version 5.8 + + - Set before the sub-command name, ``--ignore`` and its value are moved right after this name + - Set after the *shell* argument, they are left in place as they are then arguments for the script - **FUTURE**: this feature may be extended to translate environment changes made by tools like Spack, Lmod or pkg-config. It may provide this way bridges between different realms. diff --git a/doc/source/module.rst b/doc/source/module.rst index 0dc50f13d..6653519da 100644 --- a/doc/source/module.rst +++ b/doc/source/module.rst @@ -387,6 +387,18 @@ switches are accepted: search query string and module specification on all sub-commands and modulefile Tcl commands. +.. option:: --ignore LIST + + On :subcmd:`sh-to-mod` sub-command, ignore the changes made by the evaluated + shell script on the shell elements in *LIST*. *LIST* corresponds to the + concatenation of multiple shell elements separated by colon character + (``:``). Accepted shell elements are: ``envvar``, ``alias``, ``function``, + ``chdir``, ``complete`` and ``umask``. + + .. only:: html or latex + + .. versionadded:: 5.8 + .. option:: --ignore-cache Ignore module cache. @@ -2788,7 +2800,7 @@ Module Sub-Commands :option:`--terse`/:option:`-t` added to get one output line per *module-whatis* line -.. subcmd:: sh-to-mod shell script [arg...] +.. subcmd:: sh-to-mod [--ignore LIST] shell script [arg...] Evaluate with *shell* the designated *script* with defined *arguments* to find out the environment changes it does. Environment prior and after @@ -2811,6 +2823,12 @@ Module Sub-Commands is not sourced but the output resulting from its execution is evaluated to determine the environment changes it does. + The :option:`--ignore` option accepts a *LIST* of shell elements whose + changes made by *script* should be ignored, like the ``--ignore`` option of + :mfcmd:`source-sh` modulefile command. No modulefile command is produced for + the changes made on these elements. :option:`--ignore` and its value should + be set before *shell*, otherwise they are considered arguments of *script*. + .. note:: *shell* is run in non-interactive mode without loading any shell configuration or profile. As a result, script does not have access to transient definitions that are only available in interactive sessions or @@ -2838,6 +2856,9 @@ Module Sub-Commands .. versionchanged:: 5.8 Support for tracking file mode creation mask changes added + .. versionchanged:: 5.8 + Option :option:`--ignore` added + .. subcmd:: show modulefile... See :subcmd:`display`. diff --git a/init/Makefile b/init/Makefile index aa11f3f4b..cf70dbec8 100644 --- a/init/Makefile +++ b/init/Makefile @@ -145,6 +145,7 @@ comp_isavail_opts := -i --icase comp_lint_opts := -a -i --all --icase comp_modtosh_opts := --auto --no-auto --force -f --icase -i comp_purge_opts := --force -f +comp_shtomod_opts := --ignore comp_reload_opts := --force -f comp_reset_opts := --force -f comp_restore_opts := --force -f @@ -183,6 +184,7 @@ sed -e 's|@prefix@|$(prefix)|g' \ -e 's|@comp_isavail_opts@|$(comp_isavail_opts)|g' \ -e 's|@comp_lint_opts@|$(comp_lint_opts)|g' \ -e 's|@comp_modtosh_opts@|$(comp_modtosh_opts)|g' \ + -e 's|@comp_shtomod_opts@|$(comp_shtomod_opts)|g' \ -e 's|@comp_purge_opts@|$(comp_purge_opts)|g' \ -e 's|@comp_reload_opts@|$(comp_reload_opts)|g' \ -e 's|@comp_reset_opts@|$(comp_reset_opts)|g' \ diff --git a/init/bash_completion.in b/init/bash_completion.in index 138c0e2d1..8514bbc81 100644 --- a/init/bash_completion.in +++ b/init/bash_completion.in @@ -135,7 +135,11 @@ _module() { -*) COMPREPLY=( $(compgen -W "@comp_source_opts@" -- "$cur") );; *) ;; # let readline handle the completion esac;; - -h|--help|-V|--version|--dumpname|refresh|sh-to-mod|state|stashclear|cacheclear) + sh-to-mod) case "$cur" in + -*) _module_comgen_words "@comp_shtomod_opts@" "$cur";; + *) ;; # let readline handle the completion + esac;; + -h|--help|-V|--version|--dumpname|refresh|state|stashclear|cacheclear) ;; append-path|prepend-path) COMPREPLY=( $(compgen -W "@comp_path_opts@" -- "$cur") );; @@ -211,7 +215,11 @@ if type -t ml >/dev/null; then -*) COMPREPLY=( $(compgen -W "@comp_source_opts@" -- "$cur") );; *) ;; # let readline handle the completion esac;; - -h|--help|-V|--version|--dumpname|refresh|sh-to-mod|state|stashclear|cacheclear) + sh-to-mod) case "$cur" in + -*) _module_comgen_words "@comp_shtomod_opts@" "$cur";; + *) ;; # let readline handle the completion + esac;; + -h|--help|-V|--version|--dumpname|refresh|state|stashclear|cacheclear) ;; append-path|prepend-path) COMPREPLY=( $(compgen -W "@comp_path_opts@" -- "$cur") );; diff --git a/init/fish_completion b/init/fish_completion index 7d6f032ab..ec68bfb4f 100644 --- a/init/fish_completion +++ b/init/fish_completion @@ -168,6 +168,7 @@ complete -c module -l no-modulepath-ignore --description 'Do not apply modulepat complete -c module -l append --description 'Append directory to MODULEPATH' complete -c module -l prepend --description 'Prepend directory to MODULEPATH' complete -c module -l priority --description 'Set priority of path element' +complete -c module -l ignore --description 'Ignore kinds of environment change made by script' complete -c module -l timer --description 'Report execution times' complete -c module -l redirect --description 'Send output to stdout' complete -c module -l no-redirect --description 'Send output to stderr' diff --git a/init/tcsh_completion.in b/init/tcsh_completion.in index 6e72ae3dd..f84bf37ec 100644 --- a/init/tcsh_completion.in +++ b/init/tcsh_completion.in @@ -81,7 +81,7 @@ complete module 'C,/*,f,' 'C,.*,f,' 'C,~/*,f,' \ 'n/paths/`_module_avail; echo "@comp_mfile_opts@"`/' \ 'n/path/`_module_avail; echo "@comp_mfile_opts@"`/' \ 'n/edit/`_module_avail`/' \ - 'n/sh-to-mod/n/' \ + "n/sh-to-mod/(@comp_shtomod_opts@)/" \ 'n/source/f/' \ "n/append-path/(@comp_path_opts@)/" \ "n/prepend-path/(@comp_path_opts@)/" \ diff --git a/init/zsh-functions/_module.in b/init/zsh-functions/_module.in index 6c907d0af..16f398d78 100644 --- a/init/zsh-functions/_module.in +++ b/init/zsh-functions/_module.in @@ -432,6 +432,11 @@ _module() { '(-i --icase)'{-i,--icase}'[Case insensitive match]' \ "*::modulefile:{_module_notloaded_mods $cur}" && ret=0 ;; + (sh-to-mod) + _arguments \ + '--ignore[Ignore kinds of environment change made by script]' \ + && ret=0 + ;; (source) _arguments \ '--auto[Enable automated module handling mode]' \ diff --git a/tcl/main.tcl.in b/tcl/main.tcl.in index 0ec1d93e2..c0fc016f0 100644 --- a/tcl/main.tcl.in +++ b/tcl/main.tcl.in @@ -280,6 +280,17 @@ proc parseModuleCommandArgs {topcall cmd ignerr ml_spec args} { --output { knerror "Missing value for '$arg' option" } + --ignore { + # switch and its value are parsed by sh-to-mod sub-command + if {$cmd eq {sh-to-mod}} { + lappend otherargs $arg + } else { + if {!$ignerr} { + knerror "Unsupported option '$arg' on $cmd sub-command" + } + set ignore_next_arg 1 + } + } default { lappend otherargs $arg } @@ -675,6 +686,7 @@ if {[catch { # extract options and command switches from other args set otherargv {} set extraargv {} + set firstargv {} set ddelimarg 0 # split first arg if multi-word string detected for compat with previous # doc on module usage with scripting language: module('load mod1 mod2') @@ -690,6 +702,9 @@ if {[catch { } elseif {[info exists nextargisotherargv]} { lappend otherargv $arg unset nextargisotherargv + } elseif {[info exists nextargisfirstargv]} { + lappend firstargv $arg + unset nextargisfirstargv } elseif {[info exists nextargisval]} { ##nagelfar vartype nextargisval varName set $nextargisval $arg @@ -808,6 +823,18 @@ if {[catch { lappend otherargv $arg set nextargisotherargv 1 } + --ignore { + # set before sub-command name, switch and its value are moved + # right after this name. set after, they are kept in place as + # they may be arguments of the script passed to sh-to-mod + if {[llength $otherargv]} { + lappend otherargv $arg + set nextargisotherargv 1 + } else { + lappend firstargv $arg + set nextargisfirstargv 1 + } + } append-path - prepend-path - remove-path { # detect *-path commands to say -d means --delim, not --default set ddelimarg 1 @@ -882,7 +909,8 @@ if {[catch { } } - if {[info exists nextargisextraargv]} { + if {[info exists nextargisextraargv] || [info exists\ + nextargisfirstargv]} { reportErrorAndExit "Missing value for '$prevarg' option\nTry 'module\ --help' for more information." } @@ -900,7 +928,8 @@ if {[catch { } setState subcmd [lindex $otherargv 0] - set otherargv [list {*}[lreplace $otherargv 0 0] {*}$extraargv] + set otherargv [list {*}$firstargv {*}[lreplace $otherargv 0 0]\ + {*}$extraargv] setState subcmd_args $otherargv # call ml frontend if it is asked command if {[getState subcmd] eq {ml}} { diff --git a/tcl/mfcmd.tcl b/tcl/mfcmd.tcl index 26f752cc7..88aeadb3d 100644 --- a/tcl/mfcmd.tcl +++ b/tcl/mfcmd.tcl @@ -1980,7 +1980,9 @@ proc sh-to-mod {elt_ignored_list args} { return $modcontent } -proc parseSourceShArgs {args} { +# parse arguments of source-sh modulefile command or sh-to-mod sub-command, +# which is designated by cmd +proc parseSourceShArgs {cmd args} { set elt_ignored_list {} set i 0 @@ -2013,8 +2015,12 @@ proc parseSourceShArgs {args} { } if {![info exists shell] || $i == [llength $args]} { - knerror "wrong # args: should be \"source-sh ?--ignore? ?eltlist? shell\ - script ?arg ...?\"" + if {$cmd eq {sh-to-mod}} { + knerrorModule 1 "Unexpected number of args for '$cmd' command" + } else { + knerror "wrong # args: should be \"source-sh ?--ignore? ?eltlist?\ + shell script ?arg ...?\"" + } } set script_args [lassign [lrange $args $i end] script] @@ -2034,8 +2040,8 @@ proc parseSourceShArgs {args} { } proc source-sh {args} { - lassign [parseSourceShArgs {*}$args] elt_ignored_list shell script\ - script_args + lassign [parseSourceShArgs source-sh {*}$args] elt_ignored_list shell\ + script script_args # evaluate script and get the environment changes it performs translated # into modulefile commands set shtomodargs [list $shell $script {*}$script_args] @@ -2056,8 +2062,8 @@ proc source-sh {args} { # undo source-sh in unload mode proc source-sh-un {args} { - lassign [parseSourceShArgs {*}$args] elt_ignored_list shell script\ - script_args + lassign [parseSourceShArgs source-sh {*}$args] elt_ignored_list shell\ + script script_args set shtomodargs [list $shell $script {*}$script_args] # find commands resulting from source-sh evaluation recorded in env set modcontent [getLoadedSourceShScriptContent [currentState modulename]\ @@ -2074,8 +2080,8 @@ proc source-sh-un {args} { # report underlying modulefile cmds in display mode proc source-sh-di {args} { - lassign [parseSourceShArgs {*}$args] elt_ignored_list shell script\ - script_args + lassign [parseSourceShArgs source-sh {*}$args] elt_ignored_list shell\ + script script_args set shtomodargs [list $shell $script {*}$script_args] # if module loaded, get as much content from environment as possible diff --git a/tcl/report.tcl.in b/tcl/report.tcl.in index 8a09f8d01..74588623a 100644 --- a/tcl/report.tcl.in +++ b/tcl/report.tcl.in @@ -2170,7 +2170,7 @@ Other commands: config [--dump-state|name [val]|--reset name] Display or set Modules configuration state [name] Display Modules state - sh-to-mod shell shellscript [arg ...] + sh-to-mod [--ignore LIST] shell shellscript [arg ...] Make modulefile from script env changes mod-to-sh [-f] [-i] [--auto|--no-auto] shell modulefile [...] Make shell code from modulefile env changes @@ -2209,6 +2209,9 @@ Switches: sub-commands) --priority N Set priority of path elements (on 'use', 'prepend-path' or 'append-path' sub-commands) + --ignore LIST Ignore kinds of environment change made by script on + 'sh-to-mod' sub-command (LIST is made of element names + separated by ':') --index Given value is an index in the variable's list, instead of a value to look up (on 'remove-path' sub-command) diff --git a/tcl/subcmd.tcl.in b/tcl/subcmd.tcl.in index 219013b7e..7bce7d5e0 100644 --- a/tcl/subcmd.tcl.in +++ b/tcl/subcmd.tcl.in @@ -2668,11 +2668,13 @@ proc cmdModuleConfig {dump_state args} { } proc cmdModuleShToMod {args} { - set scriptargs [lassign $args shell script] + lassign [parseSourceShArgs sh-to-mod {*}$args] elt_ignored_list shell\ + script script_args # evaluate script and get the environment changes it performs translated # into modulefile commands - set modcontent [sh-to-mod {} {*}$args] + set modcontent [sh-to-mod $elt_ignored_list $shell $script\ + {*}$script_args] # output resulting modulefile if {[llength $modcontent]} { diff --git a/testsuite/completion.00-init/021-bash.exp b/testsuite/completion.00-init/021-bash.exp index f5378e3aa..51da2fac0 100644 --- a/testsuite/completion.00-init/021-bash.exp +++ b/testsuite/completion.00-init/021-bash.exp @@ -327,6 +327,11 @@ foreach subcmd $help_subcmds { ## zero completions, not just an ambiguous one set got [completion_bash_inline "module $subcmd -"] completion_assert_eq $got "\a" + } elseif {[llength $expected] == 1} { + ## a single option is completed inline by the first Tab, so there is + ## no listing to get: check the text inserted after the typed '-' + set got [completion_bash_inline "module $subcmd -"] + completion_assert_eq -[string trim $got] [lindex $expected 0] } else { ## 'config' is the only sub-command whose every option is long-form ## only ('--dump-state'/'--reset', no '-x' short forms): from a @@ -363,6 +368,11 @@ foreach subcmd $help_subcmds { if {[llength $expected] == 0} { set got [completion_bash_inline "ml $subcmd -"] completion_assert_eq $got "\a" + } elseif {[llength $expected] == 1} { + ## a single option is completed inline by the first Tab, so there is + ## no listing to get: check the text inserted after the typed '-' + set got [completion_bash_inline "ml $subcmd -"] + completion_assert_eq -[string trim $got] [lindex $expected 0] } else { set dash [expr {$subcmd eq {config} ? {--} : {-}}] set got [completion_bash_list "ml $subcmd $dash"] diff --git a/testsuite/completion.00-init/031-zsh.exp b/testsuite/completion.00-init/031-zsh.exp index 1ba9a4764..1960ce970 100644 --- a/testsuite/completion.00-init/031-zsh.exp +++ b/testsuite/completion.00-init/031-zsh.exp @@ -317,6 +317,11 @@ foreach subcmd $help_subcmds { ## nothing to insert set got [completion_zsh_inline "module $subcmd -"] completion_assert_eq $got "\a" + } elseif {[llength $expected] == 1} { + ## a single option is completed inline by the first Tab, so there is + ## no listing to get: check the text inserted after the typed '-' + set got [completion_zsh_inline "module $subcmd -"] + completion_assert_eq -[string trim $got] [lindex $expected 0] } else { ## 'config' is the only sub-command whose every option is long-form ## only ('--dump-state'/'--reset', no '-x' short forms): from a diff --git a/testsuite/modules.70-maint/312-sh-to-mod-ignore.exp b/testsuite/modules.70-maint/312-sh-to-mod-ignore.exp new file mode 100644 index 000000000..deff018f5 --- /dev/null +++ b/testsuite/modules.70-maint/312-sh-to-mod-ignore.exp @@ -0,0 +1,136 @@ +############################################################################## +# Modules Revision 3.0 +# Providing a flexible user environment +# +# File: modules.70-maint/%M% +# Revision: %I% +# First Edition: 2026/11/19 +# Last Mod.: %U%, %G% +# +# Authors: Xavier Delaruelle, xavier.delaruelle@cea.fr +# +# Description: Testuite testsequence +# Command: +# Modulefiles: +# Sub-Command: sh-to-mod +# +# Comment: %C{ +# Check '--ignore' option of 'sh-to-mod' sub-command +# }C% +# +############################################################################## + +set script testsuite/example/mini-sh-to-mod.sh + +# sh from FreeBSD is not supported by sh-to-mod +if {[auto_execok bash] ne {}} { + set sh bash +} elseif {$os_name ne {freebsd}} { + set sh sh +} else { + send_user "\tskipping sh-to-mod tests as no supported shell found\n" + return +} + +setenv_path_var MODULEPATH $modpath.2 +setenv_var FOOPATH /path/to/dir + +set tserr_all "#%Module +prepend-path\tFOOPATH /path/to/mini +set-alias\talmini {echo mini} +setenv\t\tFOOMINI value" +set tserr_noenvvar "#%Module +set-alias\talmini {echo mini}" +set tserr_noalias "#%Module +prepend-path\tFOOPATH /path/to/mini +setenv\t\tFOOMINI value" + + +# +# option set after sub-command name +# + +testouterr_cmd sh "sh-to-mod $sh $script" OK $tserr_all +testouterr_cmd sh "sh-to-mod --ignore envvar $sh $script" OK $tserr_noenvvar +testouterr_cmd sh "sh-to-mod --ignore alias $sh $script" OK $tserr_noalias +testouterr_cmd sh "sh-to-mod --ignore envvar:alias $sh $script" OK {} +# elements not changed by script +testouterr_cmd sh "sh-to-mod --ignore function:chdir:complete:umask $sh\ + $script" OK $tserr_all +# last option set wins +testouterr_cmd sh "sh-to-mod --ignore envvar --ignore alias $sh $script" OK\ + $tserr_noalias + +# ml command +testouterr_cmd sh "ml sh-to-mod --ignore envvar $sh $script" OK\ + $tserr_noenvvar + + +# +# option set before sub-command name +# + +testouterr_cmd sh "--ignore envvar sh-to-mod $sh $script" OK $tserr_noenvvar +testouterr_cmd sh "--ignore alias --ignore-user-rc sh-to-mod $sh $script" OK $tserr_noalias + + +# +# option set after shell or script is an argument for script +# + +testouterr_cmd sh "sh-to-mod $sh $script --ignore envvar" OK $tserr_all +testouterr_cmd sh "sh-to-mod $sh $script --ignore" OK $tserr_all +testouterr_cmd sh "sh-to-mod $sh --ignore envvar" ERR "$error_msgs: Script\ + '--ignore' cannot be found" + + +# +# other shell element +# + +set cur_mask [exec sh -c umask] +set other_mask 0027 +if {$cur_mask eq $other_mask} { + set other_mask 0022 +} +setenv_var TESTSUITE_UMASK_VALUE $other_mask +set umask_script testsuite/example/umask-sh-to-mod + +testouterr_cmd sh "sh-to-mod $sh $umask_script" OK "#%Module +umask\t\t$other_mask" +testouterr_cmd sh "sh-to-mod --ignore umask $sh $umask_script" OK {} + +unsetenv_var TESTSUITE_UMASK_VALUE + + +# +# erroneous usage +# + +testouterr_cmd sh "sh-to-mod --ignore foo $sh $script" ERR "$error_msgs:\ + Invalid ignored element 'foo'" +testouterr_cmd sh "sh-to-mod --ignore envvar:foo $sh $script" ERR\ + "$error_msgs: Invalid ignored element 'foo'" + +# missing value or script +set tserr "$error_msgs: Unexpected number of args for 'sh-to-mod'\ + command\n$err_typehelp" +testouterr_cmd sh {sh-to-mod --ignore} ERR $tserr +testouterr_cmd sh {sh-to-mod --ignore envvar} ERR $tserr +testouterr_cmd sh "sh-to-mod --ignore envvar $sh" ERR $tserr +testouterr_cmd sh {--ignore} ERR "[err_misoptval --ignore]\n$err_typehelp" + +# option not supported on other sub-commands +testouterr_cmd sh {avail --ignore envvar} ERR [err_unsupportedopt --ignore\ + avail] +testouterr_cmd sh {--ignore envvar load foo} ERR [err_unsupportedopt\ + --ignore load] +testouterr_cmd sh {ml foo --ignore envvar} ERR [err_unsupportedopt --ignore\ + load] + + +# +# Cleanup +# + +reset_test_env From 9f4fbaa845f97305935429c80facfd304e7448bc Mon Sep 17 00:00:00 2001 From: Xavier Delaruelle Date: Thu, 19 Nov 2026 18:41:37 -0600 Subject: [PATCH 2/2] Fix ml when switch is set before sub-command name ml filters out the command line switches to find the sub-command name among its arguments, but then withdrew the first argument instead of this name to build the sub-command argument list. With a switch set before the name, like in "ml --tag=foo load bar", the switch was lost and the sub-command name was passed as an argument to itself. Withdraw the argument that has been recognized as the sub-command name wherever it stands. Assisted-by: Claude:claude-fable-5-2 Signed-off-by: Xavier Delaruelle --- NEWS.rst | 2 ++ tcl/main.tcl.in | 6 ++++-- testsuite/modules.70-maint/280-ml.exp | 16 ++++++++++++++++ .../modules.70-maint/312-sh-to-mod-ignore.exp | 2 ++ 4 files changed, 24 insertions(+), 2 deletions(-) diff --git a/NEWS.rst b/NEWS.rst index c27d2b21d..250c2271b 100644 --- a/NEWS.rst +++ b/NEWS.rst @@ -99,6 +99,8 @@ Modules 5.8.0 (not yet released) to ignore the changes made by the evaluated shell script on specified shell elements, like done by the ``--ignore`` option of :mfcmd:`source-sh` modulefile command. (fix issue #532) +* Fix :command:`ml` command to correctly call a sub-command when command + line switches like :option:`--tag` are set before the sub-command name. .. _5.7 release notes: diff --git a/tcl/main.tcl.in b/tcl/main.tcl.in index c0fc016f0..0bd66bb60 100644 --- a/tcl/main.tcl.in +++ b/tcl/main.tcl.in @@ -568,8 +568,10 @@ proc ml {args} { } set margs $args } else { - # first argument was command name - set margs [lrange $args 1 end] + # withdraw command name from argument list: it is not the first + # argument if some switches are set before it + set cmdidx [lsearch -exact $args [lindex $fargs 0]] + set margs [lreplace $args $cmdidx $cmdidx] } # directly call module procedure if sub-command spotted as first argument diff --git a/testsuite/modules.70-maint/280-ml.exp b/testsuite/modules.70-maint/280-ml.exp index 724cdf2b0..07980d6c2 100644 --- a/testsuite/modules.70-maint/280-ml.exp +++ b/testsuite/modules.70-maint/280-ml.exp @@ -45,6 +45,10 @@ testouterr_cmd sh {ml path extdfl} [list [list text $mp/extdfl/1.3.1]] {} testouterr_cmd sh {ml avail -t extdfl/1.3.1} OK "$mp:\nextdfl/1.3.1(default)" testouterr_cmd sh {ml av -t extdfl/1.3.1} OK "$mp:\nextdfl/1.3.1(default)" +# switch set before sub-command name +testouterr_cmd sh {ml --indepth path extdfl} [list [list text $mp/extdfl/1.3.1]] {} +testouterr_cmd sh {ml --no-indepth --indepth av -t extdfl/1.3.1} OK "$mp:\nextdfl/1.3.1(default)" + # try to load a module named load: sub-command call has higher priority set ans [list] lappend ans [list set __MODULES_LMALTNAME extdfl/1.3.1&extdfl/default&extdfl] @@ -194,6 +198,18 @@ lappend ans [list set _LMFILES_ $mp/bar/2:$mp/foo/1] lappend ans [list set LOADEDMODULES bar/2:foo/1] testouterr_cmd_re sh {ml bar foo/1 -v} $ans [msg_load bar/2]\n[msg_load foo/1] +# switch set before sub-command name +set ans [list] +if {[is_conf_enabled advversspec]} { + lappend ans [list set __MODULES_LMALTNAME bar/2&as|bar/default&as|bar/latest] +} +lappend ans [list set _LMFILES_ $mp/bar/2] +lappend ans [list set LOADEDMODULES bar/2] +lappend ans [list set __MODULES_LMEXTRATAG bar/2&foo] +lappend ans [list set __MODULES_LMTAG bar/2&foo] +testouterr_cmd sh {ml --tag=foo load bar} $ans {} +testouterr_cmd sh {ml --icase --tag=foo add bar} $ans {} + # unload only command testouterr_cmd sh {ml -v -bar} OK {} setenv_loaded_module [list foo/1 bar/2] [list $mp/foo/1 $mp/bar/2] diff --git a/testsuite/modules.70-maint/312-sh-to-mod-ignore.exp b/testsuite/modules.70-maint/312-sh-to-mod-ignore.exp index deff018f5..1577c94df 100644 --- a/testsuite/modules.70-maint/312-sh-to-mod-ignore.exp +++ b/testsuite/modules.70-maint/312-sh-to-mod-ignore.exp @@ -64,6 +64,8 @@ testouterr_cmd sh "sh-to-mod --ignore envvar --ignore alias $sh $script" OK\ # ml command testouterr_cmd sh "ml sh-to-mod --ignore envvar $sh $script" OK\ $tserr_noenvvar +testouterr_cmd sh "ml --ignore envvar sh-to-mod $sh $script" OK\ + $tserr_noenvvar #