Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use more robust mechanism for unsetting environment variables #4685

Merged
merged 2 commits into from
Oct 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions easybuild/tools/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,8 +217,19 @@ def create_cmd_scripts(cmd_str, work_dir, env, tmpdir, out_file, err_file):
with open(env_fp, 'w') as fid:
# unset all environment variables in current environment first to start from a clean slate;
# we need to be careful to filter out functions definitions, so first undefine those
fid.write("unset -f $(env | grep '%=' | cut -f1 -d'%' | sed 's/BASH_FUNC_//g')\n")
fid.write("unset $(env | cut -f1 -d=)\n")
fid.write('\n'.join([
'for var in $(compgen -e); do',
' unset "$var"',
'done',
]) + '\n')
# also unset any bash functions
fid.write('\n'.join([
'for func in $(compgen -A function); do',
' if [[ $func != _* ]]; then',
' unset -f "$func"',
' fi',
'done',
]) + '\n')

# excludes bash functions (environment variables ending with %)
fid.write('\n'.join(f'export {key}={shlex.quote(value)}' for key, value in sorted(env.items())
Expand Down
3 changes: 2 additions & 1 deletion test/framework/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,8 @@ def test_run_shell_cmd_env(self):
env_script = os.path.join(cmd_tmpdir, 'env.sh')
self.assertExists(env_script)
env_script_txt = read_file(env_script)
self.assertTrue(env_script_txt.startswith('unset -f $('))
self.assertIn('unset "$var"', env_script_txt)
self.assertIn('unset -f "$func"', env_script_txt)
self.assertIn('\nexport FOOBAR=foobar\nexport PATH', env_script_txt)

cmd_script = os.path.join(cmd_tmpdir, 'cmd.sh')
Expand Down
Loading