Skip to content

Commit 4e4b199

Browse files
lib/spack/env/cc: tolerate trailing / in elements of $PATH (spack#25733)
Fixes removal of SPACK_ENV_PATH from PATH in the presence of trailing slashes in the elements of PATH: The compiler wrapper has to ensure that it is not called nested like it would happen when gcc's collect2 uses PATH to call the linker ld, or else the compilation fails. To prevent nested calls, the compiler wrapper removes the elements of SPACK_ENV_PATH from PATH. Sadly, the autotest framework appends a slash to each element of PATH when adding AUTOTEST_PATH to the PATH for the tests, and some tests like those of GNU bison run cc inside the test. Thus, ensure that PATH cleanup works even with trailing slashes. This fixes the autotest suite of bison, compiling hundreds of bison-generated test cases in a autotest-generated testsuite. Co-authored-by: Harmen Stoppels <harmenstoppels@gmail.com>
1 parent 0fb5a39 commit 4e4b199

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-1
lines changed

lib/spack/env/cc

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ export PATH=""
247247
for dir in "${env_path[@]}"; do
248248
addpath=true
249249
for env_dir in "${spack_env_dirs[@]}"; do
250-
if [[ "$dir" == "$env_dir" ]]; then
250+
if [[ "${dir%%/}" == "$env_dir" ]]; then
251251
addpath=false
252252
break
253253
fi
@@ -616,6 +616,9 @@ if [[ $SPACK_TEST_COMMAND == dump-args ]]; then
616616
IFS="
617617
" && echo "${full_command[*]}"
618618
exit
619+
elif [[ $SPACK_TEST_COMMAND =~ dump-env-* ]]; then
620+
var=${SPACK_TEST_COMMAND#dump-env-}
621+
echo "$0: $var: ${!var}"
619622
elif [[ -n $SPACK_TEST_COMMAND ]]; then
620623
die "ERROR: Unknown test command"
621624
fi

lib/spack/spack/test/cc.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,17 @@ def check_args(cc, args, expected):
141141
assert expected == cc_modified_args
142142

143143

144+
def check_env_var(executable, var, expected):
145+
"""Check environment variables updated by the passed compiler wrapper
146+
147+
This assumes that cc will print debug output when it's environment
148+
contains SPACK_TEST_COMMAND=dump-env-<variable-to-debug>
149+
"""
150+
with set_env(SPACK_TEST_COMMAND='dump-env-' + var):
151+
output = executable(*test_args, output=str).strip()
152+
assert output == executable.path + ': ' + var + ': ' + expected
153+
154+
144155
def dump_mode(cc, args):
145156
"""Make cc dump the mode it detects, and return it."""
146157
with set_env(SPACK_TEST_COMMAND='dump-mode'):
@@ -274,6 +285,26 @@ def test_dep_include():
274285
test_args_without_paths)
275286

276287

288+
def test_system_path_cleanup():
289+
"""Ensure SPACK_ENV_PATH is removed from PATH, even with trailing /
290+
291+
The compiler wrapper has to ensure that it is not called nested
292+
like it would happen when gcc's collect2 looks in PATH for ld.
293+
294+
To prevent nested calls, the compiler wrapper removes the elements
295+
of SPACK_ENV_PATH from PATH. Autotest's generated testsuite appends
296+
a / to each element of PATH when adding AUTOTEST_PATH.
297+
Thus, ensure that PATH cleanup works even with trailing /.
298+
"""
299+
system_path = '/bin:/usr/bin:/usr/local/bin'
300+
cc_dir = os.path.dirname(cc.path)
301+
with set_env(SPACK_ENV_PATH=cc_dir, SPACK_CC='true'):
302+
with set_env(PATH=cc_dir + ':' + system_path):
303+
check_env_var(cc, 'PATH', system_path)
304+
with set_env(PATH=cc_dir + '/:' + system_path):
305+
check_env_var(cc, 'PATH', system_path)
306+
307+
277308
def test_dep_lib():
278309
"""Ensure a single dependency RPATH is added."""
279310
with set_env(SPACK_LINK_DIRS='x',

0 commit comments

Comments
 (0)