Skip to content

Commit ffc8179

Browse files
committed
Resolve relative modulepath from cwd during modulefile evaluation
A relative path entry in MODULEPATH is made absolute by the procedure which uses the directory of the modulefile being evaluated as base when an evaluation is ongoing. A module loaded from within a modulefile, with a "module load", "prereq" or "depends-on" command, is thus searched in a wrong directory and reported as not found. Relative modulepath entries refer to the current working directory of the module command, whatever the evaluation context. Add an option to the path resolution procedure to force this base directory and use it when resolving modulepath entries. The "is-used" modulefile command resolves its arguments relatively to the modulefile directory, so a relative MODULEPATH entry passed as argument does not match the entry once resolved from the current working directory. The "unuse" sub-command compares its argument against the raw MODULEPATH content, so a relative entry given in a non-normalized form, like "./dir" or "dir/", is not recognized either. Introduce a procedure that returns the MODULEPATH entry corresponding to a given path, resolved as an absolute path from the modulefile directory and from the current working directory, and use it in both "is-used" and "unuse" to keep their matching logic aligned. Variable references in MODULEPATH entries are still compared as-is by "unuse", which passes the entry as registered to the path removal procedure. Assisted-by: Claude:claude-fable-5-1 Signed-off-by: Xavier Delaruelle <xavier.delaruelle@cea.fr>
1 parent be53971 commit ffc8179

8 files changed

Lines changed: 146 additions & 32 deletions

File tree

‎NEWS.rst‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,13 @@ Modules 5.7.0 (not yet released)
257257
by AISLE in partnership with Red Hat)
258258
* Accept abbreviated upper bounds in version ranges such as ``@3.20:3`` to
259259
select versions starting at ``3.20`` within major version ``3``.
260+
* Fix resolution of relative path entries in :envvar:`MODULEPATH` when a
261+
module is loaded during a modulefile evaluation. Such entry was resolved
262+
against the directory of the evaluating modulefile rather than against the
263+
current working directory. A relative *directory* argument of the
264+
:mfcmd:`is-used` modulefile command or of the :subcmd:`unuse` sub-command
265+
is also resolved against the current working directory to recognize such
266+
entry whatever its form.
260267

261268
.. _CVE-2026-85013: https://github.com/envmodules/modules/security/advisories/GHSA-8hrw-p88g-qhmg
262269

‎doc/source/modulefile.rst‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -449,10 +449,18 @@ the *modulefile* is being loaded.
449449
is enabled in case no argument is provided. If a list contains more than one
450450
*directory*, then each member acts as a boolean OR operation.
451451

452+
A relative *directory* is resolved against the directory of the modulefile
453+
being evaluated and also against the current working directory, as a
454+
relative path entry in :envvar:`MODULEPATH` refers to the latter.
455+
452456
.. only:: html or latex
453457

454458
.. versionadded:: 4.1
455459

460+
.. versionchanged:: 5.7
461+
A relative *directory* is also resolved against the current working
462+
directory
463+
456464
.. mfcmd:: lsb-release field
457465

458466
Provide certain Linux Standard Base (LSB) information. :mfcmd:`lsb-release`

‎tcl/mfcmd.tcl‎

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1335,17 +1335,13 @@ proc is-saved {args} {
13351335

13361336
# test at least one of the directories passed as argument is set in MODULEPATH
13371337
proc is-used {args} {
1338-
set modpathlist [getModulePathList]
13391338
foreach path $args {
1340-
# transform given path in an absolute path to compare with dirs
1341-
# registered in the MODULEPATH env var which are returned absolute.
1342-
set abspath [getAbsolutePath $path]
1343-
if {$abspath in $modpathlist} {
1339+
if {[getUsedModulePathEntry $path] ne {}} {
13441340
return 1
13451341
}
13461342
}
13471343
# is something used whatever it is?
1348-
return [expr {![llength $args] && [llength $modpathlist]}]
1344+
return [expr {![llength $args] && [llength [getModulePathList]]}]
13491345
}
13501346

13511347
# test at least one of the modulefiles passed as argument exists

‎tcl/modfind.tcl.in‎

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -495,9 +495,7 @@ proc getLoadedModulePropertyEnvVarName {prop} {
495495
# is set. by default an empty list is returned if no MODULEPATH set
496496
# resolv_var param tells if environment variable references in path elements
497497
# should be resolved or passed as-is in result list
498-
# set_abs param applies an absolute path name conversion to path elements
499-
# if enabled
500-
proc getModulePathList {{behavior returnempty} {resolv_var 1} {set_abs 1}} {
498+
proc getModulePathList {{behavior returnempty} {resolv_var 1}} {
501499
if {![isEnvVarDefined MODULEPATH] && $behavior eq {exiterronundef}} {
502500
reportErrorAndExit {No module path defined}
503501
}
@@ -507,14 +505,34 @@ proc getModulePathList {{behavior returnempty} {resolv_var 1} {set_abs 1}} {
507505
if {$resolv_var} {
508506
set modpath [resolvStringWithEnv $modpath]
509507
}
510-
if {$set_abs} {
511-
set modpath [getAbsolutePath $modpath]
512-
}
513-
lappendNoDup modpath_list $modpath
508+
# relative modulepath refers to current working directory of module
509+
# command, not to the directory of the modulefile being evaluated
510+
lappendNoDup modpath_list [getAbsolutePath $modpath 1]
514511
}
515512
return $modpath_list
516513
}
517514

515+
# return the MODULEPATH entry, as registered in environment variable, that
516+
# corresponds to given path. path is resolved as an absolute path from the
517+
# directory of the modulefile being evaluated and from the current working
518+
# directory, as a relative MODULEPATH entry refers to the latter. an empty
519+
# string is returned if path is not found in MODULEPATH
520+
# resolv_var param tells if environment variable references in MODULEPATH
521+
# entries should be resolved or compared as-is
522+
proc getUsedModulePathEntry {path {resolv_var 1}} {
523+
set abspath [getAbsolutePath $path]
524+
set abspath_from_cwd [getAbsolutePath $path 1]
525+
foreach modpath [getEnvLoadedModulePropertyParsedList modulepath] {
526+
set absmodpath [expr {$resolv_var ? [resolvStringWithEnv $modpath] :\
527+
$modpath}]
528+
set absmodpath [getAbsolutePath $absmodpath 1]
529+
if {$absmodpath eq $abspath || $absmodpath eq $abspath_from_cwd} {
530+
return $modpath
531+
}
532+
}
533+
return {}
534+
}
535+
518536
proc getModulepathLabel {modpath} {
519537
if {[info exists ::g_modulepathLabel($modpath)]} {
520538
return $::g_modulepathLabel($modpath)

‎tcl/subcmd.tcl.in‎

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1952,11 +1952,6 @@ proc runModuleUse {cmd mode pos args} {
19521952
if {$args eq {}} {
19531953
showModulePath
19541954
} else {
1955-
if {$pos eq {remove}} {
1956-
# get current module path list
1957-
set modpathlist [getModulePathList returnempty 0 0]
1958-
}
1959-
19601955
foreach path $args {
19611956
switch -glob -- $path {
19621957
--remove-on-unload - --append-on-unload - --prepend-on-unload -\
@@ -1978,18 +1973,13 @@ proc runModuleUse {cmd mode pos args} {
19781973
}
19791974
default {
19801975
if {$pos eq {remove}} {
1981-
if {$path in $modpathlist} {
1982-
lappend pathlist $path
1983-
# transform given path in an absolute path which should have
1984-
# been registered in the MODULEPATH env var. however for
1985-
# compatibility with previous behavior where relative paths
1986-
# were registered in MODULEPATH given path is first checked
1987-
# against current path list
1988-
} elseif {[set abspath [getAbsolutePath $path]] in\
1989-
$modpathlist} {
1990-
lappend pathlist $abspath
1991-
# even if not found, transmit this path to remove-path in
1992-
# case several path elements have been joined as one string
1976+
# transmit path as registered in MODULEPATH env var, where
1977+
# variable references are kept as-is. even if not found,
1978+
# transmit given path to remove-path in case several path
1979+
# elements have been joined as one string
1980+
if {[set usedpath [getUsedModulePathEntry $path 0]] ne\
1981+
{}} {
1982+
lappend pathlist $usedpath
19931983
} else {
19941984
lappend pathlist $path
19951985
}

‎tcl/util.tcl‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,9 +94,10 @@ proc runCommand {cmd args} {
9494
}
9595
}
9696

97-
proc getAbsolutePath {path} {
97+
proc getAbsolutePath {path {from_cwd 0}} {
9898
# currently executing a modulefile or rc, so get the directory of this file
99-
if {[currentState modulefile] ne {}} {
99+
# unless resolution from current working directory is requested
100+
if {!$from_cwd && [currentState modulefile] ne {}} {
100101
set curdir [file dirname [currentState modulefile]]
101102
# elsewhere get module command current working directory
102103
} else {

‎testsuite/modulefiles.4/bar/1‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,3 +78,23 @@ if {[info exists env(TESTSUITE_LCOMPAT)]} {
7878
}
7979
}
8080
}
81+
if {[info exists env(TESTSUITE_RELATIVE_MODULEPATH)]} {
82+
switch -- $env(TESTSUITE_RELATIVE_MODULEPATH) {
83+
load1 {
84+
module load foo/1.0
85+
}
86+
prereq1 {
87+
prereq foo/1.0
88+
}
89+
depon1 {
90+
depends-on foo/1.0
91+
}
92+
isused1 {
93+
setenv TS_ISUSED_RAW [is-used $env(MODULEPATH)]
94+
setenv TS_ISUSED_DOT [is-used ./$env(MODULEPATH)]
95+
setenv TS_ISUSED_SLASH [is-used $env(MODULEPATH)/]
96+
setenv TS_ISUSED_REL [is-used ..]
97+
setenv TS_ISUSED_UNK [is-used [file tail $env(MODULEPATH)]]
98+
}
99+
}
100+
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
##############################################################################
2+
# Modules Revision 3.0
3+
# Providing a flexible user environment
4+
#
5+
# File: modules.50-cmds/%M%
6+
# Revision: %I%
7+
# First Edition: 2026/09/15
8+
# Last Mod.: %U%, %G%
9+
#
10+
# Authors: Xavier Delaruelle, xavier.delaruelle@cea.fr
11+
#
12+
# Description: Testuite testsequence
13+
# Command: load
14+
# Modulefiles: bar, foo
15+
# Sub-Command: is-used
16+
#
17+
# Comment: %C{
18+
# Test module load made during a modulefile evaluation when
19+
# modulepath is set as a relative path
20+
# }C%
21+
#
22+
##############################################################################
23+
24+
skip_if_quick_mode
25+
26+
set mp $modpath.4
27+
28+
# modulepath relative to current working directory of module command
29+
set relmp [file tail $env(TESTSUITEDIR)]/[file tail $mp]
30+
31+
setenv_var MODULES_AUTO_HANDLING 1
32+
33+
set ans [list]
34+
lappend ans [list set __MODULES_LMPREREQ bar/1&foo/1.0]
35+
lappend ans [list set _LMFILES_ $mp/foo/1.0:$mp/bar/1]
36+
lappend ans [list set LOADEDMODULES foo/1.0:bar/1]
37+
lappend ans [list set __MODULES_LMTAG foo/1.0&auto-loaded]
38+
set tserr [msg_top_load bar/1 {} foo/1.0 {}]
39+
40+
# is-used given the relative entry (raw or in a non-normalized form) or a path
41+
# relative to the modulefile directory, which is not the current working
42+
# directory
43+
set ans2 [list]
44+
lappend ans2 [list set TS_ISUSED_UNK 0]
45+
lappend ans2 [list set TS_ISUSED_RAW 1]
46+
lappend ans2 [list set TS_ISUSED_REL 1]
47+
lappend ans2 [list set TS_ISUSED_SLASH 1]
48+
lappend ans2 [list set TS_ISUSED_DOT 1]
49+
lappend ans2 [list set _LMFILES_ $mp/bar/1]
50+
lappend ans2 [list set LOADEDMODULES bar/1]
51+
52+
foreach modulepath [list $relmp ./$relmp] {
53+
setenv_path_var MODULEPATH $modulepath
54+
foreach tscase {load1 prereq1 depon1} {
55+
setenv_var TESTSUITE_RELATIVE_MODULEPATH $tscase
56+
testouterr_cmd bash {load bar/1} $ans $tserr
57+
}
58+
59+
setenv_var TESTSUITE_RELATIVE_MODULEPATH isused1
60+
testouterr_cmd bash {load bar/1} $ans2 {}
61+
62+
# unuse given the relative entry raw or in a non-normalized form
63+
foreach unusepath [list $relmp ./$relmp $relmp/ $mp] {
64+
testouterr_cmd bash "unuse $unusepath" [list [list unset MODULEPATH]]\
65+
{}
66+
}
67+
}
68+
69+
70+
#
71+
# Cleanup
72+
#
73+
74+
reset_test_env

0 commit comments

Comments
 (0)