From fc05009c2d2b655fbaacd0b7cd48d40e6c633719 Mon Sep 17 00:00:00 2001 From: Robert McLay Date: Fri, 21 Feb 2025 15:42:56 -0700 Subject: [PATCH] Issue #750: make "export -f funcName" and "complete ..." appear last in output to bash/zsh/sh shells --- rt/complete/err.txt | 2 +- rt/complete/out.txt | 2 +- rt/set_alias/out.txt | 2 +- rt/sh_to_mf/out.txt | 12 ++++++------ shells/BaseShell.lua | 20 ++++++++++++++++---- shells/Bash.lua | 2 +- src/lmod.in.lua | 2 +- 7 files changed, 27 insertions(+), 15 deletions(-) diff --git a/rt/complete/err.txt b/rt/complete/err.txt index 31e5910eb..f9762a464 100644 --- a/rt/complete/err.txt +++ b/rt/complete/err.txt @@ -2,7 +2,7 @@ step 1 lua ProjectDIR/src/lmod.in.lua shell --regression_testing --version =========================== -Modules based on Lua: Version 8.7.32 2023-08-28 12:42 -05:00 +Modules based on Lua: Version 8.7.56 2025-02-04 18:12 -07:00 by Robert McLay mclay@tacc.utexas.edu =========================== step 2 diff --git a/rt/complete/out.txt b/rt/complete/out.txt index 1c84d2750..2c827176d 100644 --- a/rt/complete/out.txt +++ b/rt/complete/out.txt @@ -142,8 +142,8 @@ _LMFILES_=ProjectDIR/rt/complete/mf/foo/1.0.lua; export _LMFILES_; _ModuleTable_='_ModuleTable_={MTversion=3,depthT={},family={},mT={foo={fn="ProjectDIR/rt/complete/mf/foo/1.0.lua",fullName="foo/1.0",loadOrder=1,propT={},stackDepth=0,status="active",userName="foo/1.0",wV="000000001.*zfinal",},},mpathA={"ProjectDIR/rt/complete/mf",},systemBaseMPATH="ProjectDIR/rt/complete/mf",}'; export _ModuleTable_; -[[ -n "${BASH_VERSION:-}" ]] && complete -o default -F foo_completion foo; foo () { echo foo; }; foo_completion () { COMPREPLY=(bar baz); }; +[[ -n "${BASH_VERSION:-}" ]] && complete -o default -F foo_completion foo; diff --git a/rt/set_alias/out.txt b/rt/set_alias/out.txt index 0822e0217..8fb5ab6da 100644 --- a/rt/set_alias/out.txt +++ b/rt/set_alias/out.txt @@ -112,7 +112,7 @@ _some_spack_func () { echo found; fi; }; -[[ -n "${BASH_VERSION:-}" ]] && export -f _some_spack_func; +[[ -n "${BASH_VERSION:-}" ]] && export -f _some_spack_func 2> /dev/null || true; =========================== step 9 lua ProjectDIR/src/lmod.in.lua shell --regression_testing unload spack diff --git a/rt/sh_to_mf/out.txt b/rt/sh_to_mf/out.txt index a7686100d..539b740d8 100644 --- a/rt/sh_to_mf/out.txt +++ b/rt/sh_to_mf/out.txt @@ -147,7 +147,6 @@ banner () { local NONE='\\033[0m'; echo "${RED}${str}${NONE}"; }; -[[ -n "${BASH_VERSION:-}" ]] && complete -F _xyz123 xyz123; alias fooAlias=foobin\ -q\ -l; fooFunc () { echo "arg1: $1"; @@ -167,6 +166,7 @@ save_args () { done; echo " "; }; +[[ -n "${BASH_VERSION:-}" ]] && complete -F _xyz123 xyz123; =========================== step 11 lua ProjectDIR/src/lmod.in.lua shell --regression_testing show wrapperSh/1.0 @@ -189,7 +189,6 @@ banner () { local NONE='\\033[0m'; echo "${RED}${str}${NONE}"; }; -[[ -n "${BASH_VERSION:-}" ]] && complete -F _xyz123 xyz123; alias fooAlias=foobin\ -q\ -l; fooFunc () { echo "arg1: $1"; @@ -209,6 +208,7 @@ save_args () { done; echo " "; }; +[[ -n "${BASH_VERSION:-}" ]] && complete -F _xyz123 xyz123; =========================== step 13 lua ProjectDIR/src/lmod.in.lua shell --regression_testing list @@ -236,12 +236,12 @@ unset _LMFILES_; _ModuleTable_='_ModuleTable_={MTversion=3,depthT={},family={},mT={},mpathA={"ProjectDIR/rt/sh_to_mf/mf",},systemBaseMPATH="ProjectDIR/rt/sh_to_mf/mf",}'; export _ModuleTable_; unset -f banner 2> /dev/null || true; -[[ -n "${BASH_VERSION:-}" ]] && complete -r xyz123; unalias fooAlias 2> /dev/null || true; unset -f fooFunc 2> /dev/null || true; unset -f junk 2> /dev/null || true; unset -f my_help 2> /dev/null || true; unset -f save_args 2> /dev/null || true; +[[ -n "${BASH_VERSION:-}" ]] && complete -r xyz123; =========================== step 16 lua ProjectDIR/src/lmod.in.lua shell --regression_testing load brokenSh/1.0 @@ -273,7 +273,6 @@ banner () { local NONE='\\033[0m'; echo "${RED}${str}${NONE}"; }; -[[ -n "${BASH_VERSION:-}" ]] && complete -F _xyz123 xyz123; alias fooAlias=foobin\ -q\ -l; fooFunc () { echo "arg1: $1"; @@ -293,6 +292,7 @@ save_args () { done; echo " "; }; +[[ -n "${BASH_VERSION:-}" ]] && complete -F _xyz123 xyz123; =========================== step 18 lua ProjectDIR/src/lmod.in.lua shell --regression_testing save @@ -325,7 +325,6 @@ banner () { local NONE='\\033[0m'; echo "${RED}${str}${NONE}"; }; -[[ -n "${BASH_VERSION:-}" ]] && complete -F _xyz123 xyz123; alias fooAlias=foobin\ -q\ -l; fooFunc () { echo "arg1: $1"; @@ -345,6 +344,7 @@ save_args () { done; echo " "; }; +[[ -n "${BASH_VERSION:-}" ]] && complete -F _xyz123 xyz123; =========================== step 20 lua ProjectDIR/src/lmod.in.lua shell --regression_testing --mt @@ -369,7 +369,6 @@ banner () { local NONE='\\033[0m'; echo "${RED}${str}${NONE}"; }; -[[ -n "${BASH_VERSION:-}" ]] && complete -F _xyz123 xyz123; alias fooAlias=foobin\ -q\ -l; fooFunc () { echo "arg1: $1"; @@ -389,6 +388,7 @@ save_args () { done; echo " "; }; +[[ -n "${BASH_VERSION:-}" ]] && complete -F _xyz123 xyz123; =========================== step 22 lua ProjectDIR/src/lmod.in.lua shell --regression_testing list diff --git a/shells/BaseShell.lua b/shells/BaseShell.lua index 6e6a5de49..7b44df30b 100644 --- a/shells/BaseShell.lua +++ b/shells/BaseShell.lua @@ -149,6 +149,9 @@ function M.expand(self, tbl) local init = false + local delayT = {} + + for k,v in pairsByKeys(tbl) do if (not init) then self:initialize() @@ -178,10 +181,8 @@ function M.expand(self, tbl) self:alias(k,vstr) elseif (vType == "shell_function") then self:shellFunc(k,vstr) - elseif (vType == "complete") then - self:complete(k,vstr) - elseif (vType == "export_shell_function") then - self:export_shell_function(k,vstr) + elseif (vType == "complete" or vType == "export_shell_function") then + delayT[k] = { vType = vType, vstr = vstr } elseif (not vstr) then self:unset(k, vType) elseif (k == "_ModuleTable_") then @@ -192,6 +193,17 @@ function M.expand(self, tbl) end end end + if (next(delayT) ~= nil) then + for k, v in pairsByKeys(delayT) do + local vType = v.vType + if (vType == "complete") then + self:complete(k,v.vstr) + elseif (vType == "export_shell_function") then + self:export_shell_function(k,v.vstr) + end + end + end + if (init) then self:finalize() end diff --git a/shells/Bash.lua b/shells/Bash.lua index bf010d53f..f717e3bcc 100644 --- a/shells/Bash.lua +++ b/shells/Bash.lua @@ -162,7 +162,7 @@ function Bash.export_shell_function(self, n, value) if (value) then lineA[#lineA + 1] = "[[ -n \"${BASH_VERSION:-}\" ]] && export -f " lineA[#lineA + 1] = name - lineA[#lineA + 1] = ";\n" + lineA[#lineA + 1] = " 2> /dev/null || true;\n" else lineA[#lineA + 1] = "[[ -n \"${BASH_VERSION:-}\" ]] && unset -f " lineA[#lineA + 1] = name diff --git a/src/lmod.in.lua b/src/lmod.in.lua index e59562769..91d6e57c2 100644 --- a/src/lmod.in.lua +++ b/src/lmod.in.lua @@ -565,7 +565,7 @@ function main() local vPATH = varT["PATH"] if (vPATH) then - vPATH:prt(); + vPATH:prt() end hook.apply("finalize", cmdName)