From 1dcc67535a5868c5e40a198420de8b2920c14765 Mon Sep 17 00:00:00 2001 From: Xavier Delaruelle Date: Mon, 26 Jun 2023 20:21:46 +0200 Subject: modules: ignore more Modules variables in from_sourcing_file (#38455) Update list of excluded variables in `from_sourcing_file` function to cover all variables specific to Environment Modules or Lmod. Add specifically variables relative to the definition of `module()`, `ml()` and `_module_raw()` Bash functions. Fixes #13504 --- lib/spack/spack/test/data/sourceme_modules.sh | 17 +++++++++++++++ lib/spack/spack/test/environment_modifications.py | 25 ++++++++++++++++++++++- lib/spack/spack/util/environment.py | 15 +++++++++----- 3 files changed, 51 insertions(+), 6 deletions(-) create mode 100644 lib/spack/spack/test/data/sourceme_modules.sh (limited to 'lib') diff --git a/lib/spack/spack/test/data/sourceme_modules.sh b/lib/spack/spack/test/data/sourceme_modules.sh new file mode 100644 index 0000000000..ee76b4a2aa --- /dev/null +++ b/lib/spack/spack/test/data/sourceme_modules.sh @@ -0,0 +1,17 @@ +#!/usr/bin/env bash +# +# Copyright 2013-2023 Lawrence Livermore National Security, LLC and other +# Spack Project Developers. See the top-level COPYRIGHT file for details. +# +# SPDX-License-Identifier: (Apache-2.0 OR MIT) + +_module_raw() { return 1; }; +module() { return 1; }; +ml() { return 1; }; +export -f _module_raw; +export -f module; +export -f ml; + +export MODULES_AUTO_HANDLING=1 +export __MODULES_LMCONFLICT=bar&foo +export NEW_VAR=new diff --git a/lib/spack/spack/test/environment_modifications.py b/lib/spack/spack/test/environment_modifications.py index d4e1eded46..359a57a79d 100644 --- a/lib/spack/spack/test/environment_modifications.py +++ b/lib/spack/spack/test/environment_modifications.py @@ -400,7 +400,7 @@ def test_sanitize_literals(env, exclude, include): ({"SHLVL": "1"}, ["SH.*"], [], [], ["SHLVL"]), # Check we can include using a regex ({"SHLVL": "1"}, ["SH.*"], ["SH.*"], ["SHLVL"], []), - # Check regex to exclude Modules v4 related vars + # Check regex to exclude Environment Modules related vars ( {"MODULES_LMALTNAME": "1", "MODULES_LMCONFLICT": "2"}, ["MODULES_(.*)"], @@ -415,6 +415,13 @@ def test_sanitize_literals(env, exclude, include): [], ["A_modquar", "b_modquar", "C_modshare"], ), + ( + {"__MODULES_LMTAG": "1", "__MODULES_LMPREREQ": "2"}, + ["__MODULES_(.*)"], + [], + [], + ["__MODULES_LMTAG", "__MODULES_LMPREREQ"], + ), ], ) def test_sanitize_regex(env, exclude, include, expected, deleted): @@ -489,3 +496,19 @@ def test_exclude_lmod_variables(): # Check that variables related to lmod are not in there modifications = env.group_by_name() assert not any(x.startswith("LMOD_") for x in modifications) + + +@pytest.mark.skipif(sys.platform == "win32", reason="Not supported on Windows (yet)") +@pytest.mark.regression("13504") +def test_exclude_modules_variables(): + # Construct the list of environment modifications + file = os.path.join(datadir, "sourceme_modules.sh") + env = EnvironmentModifications.from_sourcing_file(file) + + # Check that variables related to modules are not in there + modifications = env.group_by_name() + assert not any(x.startswith("MODULES_") for x in modifications) + assert not any(x.startswith("__MODULES_") for x in modifications) + assert not any(x.startswith("BASH_FUNC_ml") for x in modifications) + assert not any(x.startswith("BASH_FUNC_module") for x in modifications) + assert not any(x.startswith("BASH_FUNC__module_raw") for x in modifications) diff --git a/lib/spack/spack/util/environment.py b/lib/spack/spack/util/environment.py index bde7ce3780..3c63543551 100644 --- a/lib/spack/spack/util/environment.py +++ b/lib/spack/spack/util/environment.py @@ -776,16 +776,21 @@ class EnvironmentModifications: "PS1", "PS2", "ENV", - # Environment modules v4 + # Environment Modules or Lmod "LOADEDMODULES", "_LMFILES_", - "BASH_FUNC_module()", "MODULEPATH", - "MODULES_(.*)", + "MODULERCFILE", + "BASH_FUNC_ml()", + "BASH_FUNC_module()", + # Environment Modules-specific configuration + "MODULESHOME", + "BASH_FUNC__module_raw()", + r"MODULES_(.*)", + r"__MODULES_(.*)", r"(\w*)_mod(quar|share)", - # Lmod configuration + # Lmod-specific configuration r"LMOD_(.*)", - "MODULERCFILE", ] ) -- cgit v1.2.3-60-g2f50